Spring-boot: Getting 404 instead of 200 with integration tests when updating from Spring Boot 2.0.X to 2.1.0

Created on 1 Nov 2018  路  5Comments  路  Source: spring-projects/spring-boot

Hi,

I have 2 integration tests passing with Spring Boot (2.0.X, probably with version 1.X as well) but when I use the latest version 2.1.0, the first one is failing, it returns 404 instead of 200...

Any idea? I could not find any breaking change about this in the release note.

Thanks,

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class MockMvcExampleTests {

    @Autowired
    private MockMvc mvc;

    @Test
    public void testHomeUrl() throws Exception {
        this.mvc.perform(get("/home")).andExpect(status().isOk())
                .andExpect(content().string("Hello World"));
    }

    @Test
    public void testAdminHomeUrl() throws Exception {
        this.mvc.perform(get("/admin/home")).andExpect(status().isUnauthorized());
    }

    @Configuration
    public static class MyTestConfiguration extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                    .authorizeRequests().antMatchers("/admin/**").hasRole("USER").and()
                    .httpBasic();
        }
    }

    @Controller
    public static class TestController {

        @RequestMapping(path = "/home")
        public ResponseEntity<String> home() {
            return ResponseEntity.ok("Hello World");
        }

        @RequestMapping(path = "/admin/home")
        public ResponseEntity<String> adminHome() {
            return ResponseEntity.ok("Hello World Secured");
        }

    }

}
external-project invalid

Most helpful comment

@singhpradeepkumar
I'm also facing a similar issue where @WebMvcTest(MyController.class) isn't enough to wire in MyController.class - which is what I think it's supposed to do - causing the 404. Adding @Import(MyController.class) makes everything work. I'll see if I can create a sample app reproducing this behavior.

All 5 comments

In Boot 2.1, your TestController isn't part of the application context but in Boot 2.0 it is. When the controller's missing, the test for /admin/home works due to your security configuration and it being rejected with a 401. The test for /home fails as the controller isn't found so there's nothing in the context to handle the request.

The change in behaviour is due to a change in Spring Framework. If you drop back to Boot 2.0.6 but override the Framework version to 5.1, you'll see the same failure. I believe this is due to the changes made in SPR-17206.

You can cause TestController to be picked up by annotating it with @TestConfiguration.

Yes, it is working now. Thanks a lot!

I was facing same problem with Spring Boot 2.1 but @TestConfiguration does not seems to work in my case.

Please find the working code below.

@WebMvcTest(ReportController.class)
@ComponentScan(basePackages = "com.pkg")
public class ReportControllerTest {
    @Autowired
     private MockMvc mockMvc;
}

After adding @ComponentScan(basePackages = "com.pkg") issue has been resolved.

@singhpradeepkumar
I'm also facing a similar issue where @WebMvcTest(MyController.class) isn't enough to wire in MyController.class - which is what I think it's supposed to do - causing the 404. Adding @Import(MyController.class) makes everything work. I'll see if I can create a sample app reproducing this behavior.

In my case I had to combine some of the above steps to finally get my tests working:

On my controller to be tested:

@RestController
@RequestMapping(value = "/rest")
@TestConfiguration
public class SomeControllerImpl implements SomeController {

On my test class:

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = SomeControllerImpl.class)
@Import(SomeControllerImpl.class)
public class SomeControllerImplTest {
Was this page helpful?
0 / 5 - 0 ratings