Java.lang.AssertionError: Expected status: 200 Actual: 404

I get this error message:

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

My controller is like that

        @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 this code, the test works 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?

+4
source share
2 answers

Methods in your RestController should be marked as @RequestMapping if you want to be picked up as request resources. If you want to keep the base query mapping at the controller level, as in your first RestController, you need to do the following:

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

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

As the documentation says :

@RequestMapping . type (class), , /.

, @RequestMapping . . :

@GetMapping
public InputState executeRestFile(@RequestParam String name) throws Exception {
    // omited
}

:

@RequestMapping(method = RequestMethod.GET)
public InputState executeRestFile(@RequestParam String name) throws Exception {
    // omited
}
0

Source: https://habr.com/ru/post/1681851/


All Articles