【发布时间】:2018-09-07 05:53:14
【问题描述】:
在 application.properties 我配置了server.contextPath=/app-service。
很遗憾,测试上下文中不存在此值:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = { Application.class, SwaggerConfig.class })
@WebAppConfiguration
public class Swagger2MarkupTest {
@Autowired
private WebApplicationContext context;
private MockMvc mockMvc;
@Before
public void setUp() {
this.context.getServletContext().getContextPath(); // null
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
}
}
我需要默认使用的 Swagger Docket 的 contextPath
springfox.documentation.spring.web.paths.RelativePathProvider 通过
@Override
protected String applicationPath() {
return isNullOrEmpty(servletContext.getContextPath()) ? ROOT : servletContext.getContextPath();
}
这都是关于在单元测试之前被调用的 RelativePathProvider。这与将 contextPath 注入到单元测试本身无关,因为相对路径提供者已经为 SwaggerConfiguration 调用了servletContext.getContextPath(),所以它已经很晚了。
【问题讨论】:
标签: spring-boot swagger springfox contextpath