我使用Spring Boot 2.1.2.RELEASE编写集成测试,无法设置所需的上下文路径。它始终等于空字符串(通过ServletContext.getContextPath()来获取。)>
application-test.properties:
server.servlet.context-path=/app
测试配置:
@ExtendWith(SpringExtension.class) @ContextConfiguration @Import({MailSenderAutoConfiguration.class, ThymeleafAutoConfiguration.class}) @WebAppConfiguration @TestPropertySource(locations="classpath:application-test.properties") public class MyServiceTests {...}
呼叫代码:
@Value("#{'${application.base-url}' + servletContext.contextPath}") private String siteUrl;
application-test.properties中的其他属性已按预期注入。运行服务器时通常设置上下文路径。我试图以@SpringBootTest形式启动此测试,并在变量@TestPropertySource中提供了上下文路径(locations =“ classpath:application-test.properties”,properties =“ server.servlet.context-path = / app”),但没有结果。如何设置测试的上下文路径?
我使用Spring Boot 2.1.2.RELEASE编写集成测试,无法设置所需的上下文路径。它始终等于空字符串(通过ServletContext.getContextPath()获得)。 application-test.properties:...
我发现了这种解决方法: