有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在Spring引导测试中设置servlet上下文路径?

我使用SpringBoot2.1.2编写集成测试。释放,无法设置所需的上下文路径。它始终等于空字符串(使用servletContext.getContextPath()获取)

应用测试。特性:

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;

应用程序测试中的其他属性。属性按预期注入。通常在运行服务器时设置上下文路径。我尝试以@SpringBootTest的形式启动此测试,在变量@TestPropertySource(locations=“classpath:application test.properties”,properties=“server.servlet.context path=/app”)中提供了上下文路径,但没有结果。如何设置测试的上下文路径


共 (1) 个答案

  1. # 1 楼答案

    我发现了这样的漫游:

    文件应用程序测试。属性:

    application.base-url=http://test.ru
    server.servlet.context-path=/app
    application.root-url=${application.base-url}${server.servlet.context-path:}
    

    呼叫代码:

    @Value("${application.root-url}")
    private String siteUrl;