Spring Boot集成测试中的H2控制台

9
我正在使用Spring Boot 1.3.1.RELEASE。我在application.properties文件中设置了spring.h2.console.enabled=true,以启用H2 Web控制台。当我启动Spring Boot应用程序时,可以通过http://localhost:8080/h2-console/访问H2 Web控制台。
但是,当我在调试模式下执行集成测试并设置断点时,无法访问控制台(网站不可用)。集成测试的配置如下:
@ActiveProfiles("local,unittest")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyApplication.class)
@WebAppConfiguration
@IntegrationTest
public class MyResourceTest {

我有没有考虑到什么?

你尝试过将 "test" 添加到 @ActiveProfiles 吗? - yugo
是的,但这并不太有效。 - René Winkler
1个回答

4
为了启用H2 Web控制台,您应该将与此项相关的属性文件的值作为注释@IntegrationTest的可选元素进行聚合。
字符串数组值 以key=value形式呈现的属性,应在测试运行之前添加到Spring环境中。
示例:
@IntegrationTest({"spring.h2.console.enabled=true"})

请记住以下建议:
如果您的测试还使用@WebAppConfiguration,请考虑使用@WebIntegrationTest而不是@IntegrationTest
因此,请尝试使用@WebIntegrationTest而不是@IntegrationTest,并添加键spring.h2.console.enabled=true

http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/IntegrationTest.html

http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/WebIntegrationTest.html

也许你需要在@WebIntegrationTest注解中添加键"server.port=8080"作为另一个值。

移除注解@IntegrationTest,你的测试是否成功结束? - Jesus Zavarce
不,它没有。会抛出以下异常:java.lang.IllegalArgumentException: Could not resolve placeholder 'local.server.port' in string value "${local.server.port}"。 - René Winkler
尝试将“server.port=8080”作为您注释的键添加,还要在您的属性文件中添加此键。 - Jesus Zavarce
正在加载中...网站仍未出现。但至少我不再收到错误提示 :-) - René Winkler
尝试更改您的端口,例如8081,运行测试并转到http://localhost:8081/h2-console/。 - Jesus Zavarce
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接