当前位置: 首页 > 面试题库 >

Spring REST模拟上下文路径

通学真
2023-03-14
问题内容

我尝试使用以下代码段设置spring rest模拟的上下文路径:

private MockMvc mockMvc;

@Before
public void setUp() {
    this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
            .apply(documentationConfiguration(this.restDocumentation))
            .alwaysDo(document("{method-name}/{step}/",
                    preprocessRequest(prettyPrint()),
                    preprocessResponse(prettyPrint())))
            .build();
}

@Test
public void index() throws Exception {
    this.mockMvc.perform(get("/").contextPath("/api").accept(MediaTypes.HAL_JSON))
            .andExpect(status().isOk())
            .andExpect(jsonPath("_links.business-cases", is(notNullValue())));
}

但是我收到以下错误:

java.lang.IllegalArgumentException: requestURI [/] does not start with contextPath [/api]

怎么了? 是否可以在代码中的单个位置(例如直接在构建器中)指定contextPath?

这里的控制器

@RestController
@RequestMapping(value = "/business-case", produces = MediaType.APPLICATION_JSON_VALUE)
public class BusinessCaseController {
    private static final Logger LOG = LoggerFactory.getLogger(BusinessCaseController.class);

    private final BusinessCaseService businessCaseService;

    @Autowired
    public BusinessCaseController(BusinessCaseService businessCaseService) {
        this.businessCaseService = businessCaseService;
    }

    @Transactional(rollbackFor = Throwable.class, readOnly = true)
    @RequestMapping(value = "/{businessCaseId}", method = RequestMethod.GET)
    public BusinessCaseDTO getBusinessCase(@PathVariable("businessCaseId") Integer businessCaseId) {
        LOG.info("GET business-case for " + businessCaseId);
        return businessCaseService.findOne(businessCaseId);
    }
}

问题答案:

您需要在传递到的路径中包括上下文路径get

在问题中显示的情况下,上下文路径为/api并且您想向其发出请求,/因此您需要传递/api/get

@Test
public void index() throws Exception {
    this.mockMvc.perform(get("/api/").contextPath("/api").accept(MediaTypes.HAL_JSON))
            .andExpect(status().isOk())
            .andExpect(jsonPath("_links.business-cases", is(notNullValue())));
}


 类似资料:
  • 问题内容: 我不明白为什么无法在此示例中模拟NamedTemporaryFile.name: 测试结果在: 问题答案: 您设置的模拟错误:不是上下文管理器,而是 返回 了一个上下文管理器。将您的设置行替换为: 这样您的测试就可以了。

  • 我希望创建使用Spring上下文和模拟存储库bean的测试。我使用的是Spring Boot 1.3。2.构建快照JUnit Mockito。 以下是我的测试配置类: 此配置的目的是将OfferPresository从Spring上下文中排除并对其进行模拟,由于此,我将能够编写使用Spring上下文和模拟数据库存储库的测试。 这是我的测试课: 测试和测试配置目录为: 我的应用程序配置和包含Offe

  • 在 Blade 2.0.9 版本后加入了 RouteContext 这个类,作为路由的上下文操作。其本质是封装了 Request 和 Response,所以使用起来和它们的 API 是相同的,下面列举一下包含的方法列表。 请求相关 #request() #method() #uri() #keepAlive() #session() #isIE() #header(String headerNam

  • 我使用Spring Boot 1.3.2,我注意到问题,ComponentScan在我的测试类不工作。我想嘲笑一些春豆。Spring引导是否阻止ComponentScan? 测试配置类: 测试类:

  • 我正在为一个向最终用户隐藏骆驼业务的库编写测试用例。该库公开了sendMessages()等方法,并在内部使用ProducerTemplate将消息发送到一个骆驼路由,在该路由中将消息聚合并最终发送到目的地。 我希望能够编写调用library方法的测试,并模拟骆驼路由endpoint,以便稍后对其进行断言。 如果我使用CamelSpringTestSupport,它基本上会创建一个新的类PathX

  • 我想更改spring Boot2的上下文路径,例如,我想在http://localhost:8080/test/上服务 我的意思是,spring-boot-starter-webflux:2.0.0对我来说不起作用