Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 730
Spyros_av Avatar asked Nov 08 '22 19:11

Spyros_av


1 Answers

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() {
     ...
   }
}
like image 141
Plog Avatar answered Nov 15 '22 08:11

Plog