java. Lang. assertionerror: expected status: 200 actual: 404

I received 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 such code, the test works normally!

@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/// 
               }
}

What's wrong?

Solution

If you want to get them as request resources, you need to mark the method in restcontroller as @ requestmapping If you want to keep the basic request mapping at the controller level as in the first restcontroller, you need to do the following:

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

   @RequestMapping("/")
   public InputState myMethod() {
     ...
   }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>