我的REST API中有一个GET方法

这是我的 Controller 课

@RestController
@RequestMapping("/api")
@Validated
public class ApiController {

@Autowired
private ApiService service;

@GetMapping(path = { "/check/{type}", "/check" },
produces= {MediaType.APPLICATION_JSON_UTF8_VALUE,MediaType.APPLICATION_XML_VALUE})
    public List<Myobject> check(@MyConstraint @RequestParam("email") final List<String> emails,
            @PathVariable(name = "type", required = false) final String type) {
        final String subscriptiontype = StringUtils.isEmpty(type) ? "all" : type;
        final List<Myobject> objects= service.check(emails, subscriptiontype);

    return objects;
}

}

我正在尝试为此 Controller 类编写单元测试
@RunWith(SpringRunner.class)
@WebMvcTest(controllers = SubscriberApiController.class)

public class ApiControllerTest {

private MockMvc mvc;

@Autowired
private WebApplicationContext webApplicationContext;

@MockBean
private ApiService service;

@Before
public void setUp() {
//  mvc = MockMvcBuilders.standaloneSetup(new HandlerController()).build();
    mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}

@Test
public void getIndex() throws Exception {
    mvc.perform(get("/my-service/api/[email protected]").accept(MediaType.APPLICATION_JSON))
       .andExpect(status().isOk());

}
}

但这向我显示了映射错误No mapping found for HTTP request with URI
2018-04-20 11:51:23.409  INFO 12968 --- [           main] c.a.d.s.service.api.ApiControllerTest    : No active profile set, falling back to default profiles: default
    2018-04-20 11:51:23.432  INFO 12968 --- [           main] o.s.w.c.s.GenericWebApplicationContext   : Refreshing org.springframework.web.context.support.GenericWebApplicationContext@43f02ef2: startup date [Fri Apr 20 11:51:23 BST 2018]; root of context hierarchy
    2018-04-20 11:51:24.847  INFO 12968 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/check/{type} || /api/check],methods=[GET],produces=[application/json;charset=UTF-8 || application/xml]}" onto public com.aerlingus.dei.subscriber.service.api.model.Subscriptions com.aerlingus.dei.subscriber.service.api.controller.SubscriberApiController.checkSubscriptionForUSersByType(java.util.List<java.lang.String>,java.lang.String)
    2018-04-20 11:51:24.852  INFO 12968 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
    2018-04-20 11:51:24.852  INFO 12968 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
    2018-04-20 11:51:24.944  INFO 12968 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.web.context.support.GenericWebApplicationContext@43f02ef2: startup date [Fri Apr 20 11:51:23 BST 2018]; root of context hierarchy
    2018-04-20 11:51:24.993  INFO 12968 --- [           main] .m.m.a.ExceptionHandlerExceptionResolver : Detected @ExceptionHandler methods in badRequestExceptionHandler
    2018-04-20 11:35:47.040  INFO 30276 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.web.context.support.GenericWebApplicationContext@43f02ef2: startup date [Fri Apr 20 11:35:44 BST 2018]; root of context hierarchy
    2018-04-20 11:35:47.110  INFO 30276 --- [           main] .m.m.a.ExceptionHandlerExceptionResolver : Detected @ExceptionHandler methods in badRequestExceptionHandler
    2018-04-20 11:35:47.454  INFO 30276 --- [           main] o.s.b.t.m.w.SpringBootMockServletContext : Initializing Spring FrameworkServlet ''
    2018-04-20 11:35:47.454  INFO 30276 --- [           main] o.s.t.web.servlet.TestDispatcherServlet  : FrameworkServlet '': initialization started
    2018-04-20 11:35:47.475  INFO 30276 --- [           main] o.s.t.web.servlet.TestDispatcherServlet  : FrameworkServlet '': initialization completed in 21 ms
    2018-04-20 11:35:47.625  INFO 30276 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 2147483647
    2018-04-20 11:35:47.626  INFO 30276 --- [           main] d.s.w.p.DocumentationPluginsBootstrapper : Context refreshed
    2018-04-20 11:35:47.656  INFO 30276 --- [           main] d.s.w.p.DocumentationPluginsBootstrapper : Found 1 custom documentation plugin(s)
    2018-04-20 11:35:47.702  INFO 30276 --- [           main] s.d.s.w.s.ApiListingReferenceScanner     : Scanning for api listing references
    2018-04-20 11:35:47.938  INFO 30276 --- [           main] .d.s.w.r.o.CachingOperationNameGenerator : Generating unique operation named: checkSubscriptionForUSersByTypeUsingGET_1
    2018-04-20 11:35:47.960  INFO 30276 --- [           main] c.a.d.s.s.api.HandlerControllerTest      : Started HandlerControllerTest in 3.653 seconds (JVM running for 4.761)
    2018-04-20 11:35:47.983  INFO 30276 --- [           main] o.s.b.t.m.w.SpringBootMockServletContext : Initializing Spring FrameworkServlet ''
    2018-04-20 11:35:47.983  INFO 30276 --- [           main] o.s.t.web.servlet.TestDispatcherServlet  : FrameworkServlet '': initialization started
    2018-04-20 11:35:47.985  INFO 30276 --- [           main] o.s.t.web.servlet.TestDispatcherServlet  : FrameworkServlet '': initialization completed in 2 ms
    2018-04-20 11:35:48.031  WARN 30276 --- [           main] o.s.web.servlet.PageNotFound             : No mapping found for HTTP request with URI [/my-service/api/check] in DispatcherServlet with name ''
    2018-04-20 11:35:48.050  INFO 30276 --- [       Thread-2] o.s.w.c.s.GenericWebApplicationContext   : Closing org.springframework.web.context.support.GenericWebApplicationContext@43f02ef2: startup date [Fri Apr 20 11:35:44 BST 2018]; root of context hierarchy
    2018-04-20 11:35:48.052  INFO 30276 --- [       Thread-2] o.s.c.support.DefaultLifecycleProcessor  : Stopping beans in phase 2147483647

我的ciontext根未映射到servlet
server.servlet.contextPath=/my-service

如果我将测试更改为
 mvc.perform(get("/api/[email protected]").accept(MediaType.APPLICATION_JSON))
       .andExpect(status().isOk());

工作正常

更新
http://localhost:8080/my-service/api/[email protected]

有人可以告诉我这是怎么回事吗???

最佳答案

MockMvc是一个模拟,它不是从服务器配置加载上下文路径。您必须将contextPath添加到您的mockMvc对象:

mvc.perform(get("/my-service/api/[email protected]")
    .contextPath("/my-service")
    .accept(MediaType.APPLICATION_JSON))
    .andExpect(status().isOk());

09-27 11:30