java.lang.AssertionError:预期状态:200实际:404

我收到此错误消息:

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

我的控制器是这样的

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

我的测试

  @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());
        }
}

但是当我有这样的代码时,测试工作正常!

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

出了什么问题?

解决方法:

如果希望将它们作为请求资源获取,则需要将RestController中的方法标记为@RequestMapping.如果要像在第一个RestController中那样在控制器级别保留基本请求映射,则需要执行以下操作:

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

   @RequestMapping("/")
   public InputState myMethod() {
     ...
   }
}
上一篇:java – MockMvc返回404状态


下一篇:java – 使用jsonPath检查Map键/值