Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么Spring引导控制器测试返回404,但在上下文中找到了控制器?_Java_Spring_Spring Boot_Junit5 - Fatal编程技术网

Java 为什么Spring引导控制器测试返回404,但在上下文中找到了控制器?

Java 为什么Spring引导控制器测试返回404,但在上下文中找到了控制器?,java,spring,spring-boot,junit5,Java,Spring,Spring Boot,Junit5,我有一个REST控制器,我正试图测试,但当试图发布到它,我得到一个404。测试是JUnit5和SpringBoot2.1.5。如果我运行应用程序,我可以通过邮递员点击控制器。我已经在调试模式下运行了它,并验证了myController不是null,并且已将模拟服务注入其中。我错过了什么?SpringBootStarter测试是一个依赖项,junit4是一个排除项 @RestController @Slf4j @RequestMapping(path = /integrations, pr

我有一个REST控制器,我正试图测试,但当试图发布到它,我得到一个404。测试是JUnit5和SpringBoot2.1.5。如果我运行应用程序,我可以通过邮递员点击控制器。我已经在调试模式下运行了它,并验证了myController不是null,并且已将模拟服务注入其中。我错过了什么?SpringBootStarter测试是一个依赖项,junit4是一个排除项

@RestController
@Slf4j
@RequestMapping(path = /integrations,
    produces = "application/json")
public class MyController {

    private MyService myService;
    private MyValidationService myValidationService;

    public MyController(MySerivce service, MyValidationService myValidationService) {
        this.myService = service;
        this.myValidationService = myValidationService;
    }

    @PostMapping(path = "/users", produces = "application/json", consumes = 
                                       "application/json")
    public ResponseEntity<User> getUserList(@Valid @RequestBody final RequestPayload 
          requestPayload, @RequestHeader Map<String, String> headers) throws MyException {
        // check the credentials and permission
        Map<String, String> credInfo = myValidationService.validateHeaders(headers);

        // process payload
        return myService.retrieveUsers(requestPayload);


    }
}

您可以改用切片测试注释。要测试控制器,可以使用@WebMvcTest

您的设置如下所示:

@SpringBootTest(value = MyController.class)
@ExtendWith(SpringExtension.class)
class MyControllerTest { 

您仍然可以@Autowired MockMvc。此外,在测试中也不需要自动连接控制器。

我很感谢您的回答,但我发现了问题所在-这是一个愚蠢的问题

事实证明,当application.properties文件中已经提供了上下文路径时,我在mockMvc调用中包含了该路径!因此,使用的真正URI不是/integrations/users的URI,而是/integrations/integrations/users,这并不奇怪


感谢所有人,很抱歉没有将我的眼睛放在手中,仔细观察。

当您正常启动spring boot应用程序时,此端点是否正常工作?是的,它在正常操作下工作。@Deadpool我们在同一页上。我试过了,但结果还是一样的。@Deadpool我会试着做点什么。我基本上简化了真正的代码。我还更新了junit5 jupiter和mockito的版本。将第二行更改为@ExtendWithMockitoExtension.class@RunWithJUnitPlatform.class Nope。没有变化。在黑暗中开枪!
MockHttpServletResponse:
           Status = 404
    Error message = null
          Headers = []
     Content type = null
             Body = 
    Forwarded URL = null
   Redirected URL = null
          Cookies = []
@SpringBootTest(value = MyController.class)
@ExtendWith(SpringExtension.class)
class MyControllerTest {