如何在Spring Boot Tests中设置servlet上下文路径?

问题描述 投票:0回答:1

我使用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:...

java spring spring-boot spring-mvc spring-test
1个回答
0
投票

我发现了这种解决方法:

© www.soinside.com 2019 - 2024. All rights reserved.