@RequestMapping java.lang.AssertionError: Status Expected :200 Actual :404

Spyros_av picture Spyros_av · Jul 20, 2017 · Viewed 8.7k times · Source

Assertion error using @RequestMapping annotation outside of the class

I am getting this error message:

 java.lang.AssertionError: Status 
    Expected :200
    Actual   :404

My Controller is like this

        @Service
        @RestController
        @RequestMapping("/execute/files")
        @ResponseBody
        public class ControllerFiles {
            @Autowired
            @Qualifier("fileRunner")
            ProcessRunnerInterface processRunnerInterfaceFiles;  

            public InputState executeRestFile(@RequestParam String name) throws ExecutionFailedException, URISyntaxException {
               ///code///    
            }
            public List<String>....{
             ///code///
            }
        }

My Test

  @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest
    @AutoConfigureMockMvc
    public class ControllerFilesTest {

        @Autowired
        private MockMvc mockMvc;
        @Autowired
        ControllerFiles controllerFiles;

        @Test
        public void testSpringMvcGetFiles() throws Exception {

            this.mockMvc.perform(get("/execute/files").param("name", "Spring Community Files"))
                    .andDo(print()).andExpect(status().isOk());
        }
}

But when I have my code like this the test work fine!

            @Service
            @RestController
            public class ControllerFiles {
                @Autowired
                @Qualifier("fileRunner")
                ProcessRunnerInterface processRunnerInterfaceFiles;

                @RequestMapping("/execute/files")
                @ResponseBody
                public InputState executeRestFile(@RequestParam String name) throws ExecutionFailedException, URISyntaxException {
                  ///code///        
                }  
               public List<String>....{  
                  ///code/// 
               }
}

Any ideas what is going wrong?

Answer

Plog picture Plog · Jul 20, 2017

The methods in your RestController need to be marked as @RequestMapping if you want them to be picked up as request resources. If you want to keep the base request mapping at the controller level as in your first RestController then you need to do the following:

@RestController
@RequestMapping("my/path")
public class MyController {

   @RequestMapping("/")
   public InputState myMethod() {
     ...
   }
}