Status expected:<200> but was:<404> in spring test Status expected:<200> but was:<404> in spring test spring spring

Status expected:<200> but was:<404> in spring test


Your test setup is wrong you aren't initializing the MockMvc correctly and that is al clearly in the reference guide. FIrst of all you have twice the initializing code and you aren't assing the result of the call to the build method. So you are basically left with an empty MockMvc object.

@Beforepublic void before() {    MockitoAnnotations.initMocks(this);    MockMvcBuilders.webAppContextSetup(this.wac).dispatchOptions(true).build();    MockMvcBuilders.webAppContextSetup(this.wac).dispatchOptions(true).build();}

Should be

@Beforepublic void before() {    MockitoAnnotations.initMocks(this);    this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).dispatchOptions(true).build();}

As stated this is all explained in the reference guide.


I believe you just haven't enabled <mvc:annotation-driven> in your beanconfig.xml and so your @Controller classes just aren't being registered.

Add this

<mvc:annotation-driven></mvc:annotation-driven>


In my case, I was missing the below annotation and was getting this error.

@WebMvcTest(UserStatsController.class)public class UserStatsControllerTest {  ..}

Note that the class of the controller and NOT the test.Make sure as well if you load other class using @ContextConfiguration(NOT the test) not load test class.