spring-boot - 运行 SpringBootTest 时访问 H2 控制台

标签 spring-boot spring-boot-test

如果我正在使用 @SpringBootTest 运行测试有没有办法访问H2控制台?我有一个访问 H2 数据库的测试(成功),但是如果我想自己检查数据库,我该怎么做?

我首先使用 webEnvironment=DEFINED_PORT 运行测试。和 http://localhost:8080/响应 HTTP,但没有任何内容 http://localhost:8080/h2-console/仍然以 404 响应。

我尝试通过向 application.properties 添加值来显式打开控制台:

spring.h2.console.enabled=true
spring.h2.console.path=/h2

那似乎没有效果。在 /h2-console 仍然是 404s和 /h2 .

H2 控制台似乎是通过自动配置进来的,所以我使用 -Ddebug 打开了自动配置报告,我可以看到,尽管在 application.properties 中启用了启用标志,但它被视为关闭:
H2ConsoleAutoConfiguration: 
      Did not match:
          - @ConditionalOnProperty (spring.h2.console.enabled=true) did not find property 'enabled' (OnPropertyCondition)
      Matched:
         - @ConditionalOnClass found required class 'org.h2.server.web.WebServlet'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
         - found WebApplicationContext (OnWebApplicationCondition)

似乎某种机制正在覆盖该值或忽略来自 application.properties 的值在运行测试时。

Spring Boot Test 不启动控制台吗?有什么方法可以说服它这样做吗?

最佳答案

好的,我能够使用以下注释显示它(在 Kotlin 中):@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, properties = arrayOf("spring.h2.console.enabled=true"))
即使这样,控制台仍然在 /h2-console ,所以它显然也忽略了控制台路径。我大概可以通过添加另一个属性来控制它。

关于spring-boot - 运行 SpringBootTest 时访问 H2 控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50374545/

相关文章:

java - 从 Junit Test 运行时 Spring Boot 应用程序抛出异常

java - @Autowired 在 SpringRunner SpringBootTest 单元测试用例中

java - 使用 eclipse 无法在 java 中解决 PMD 错误

java - 当多个端点映射到同一路径时Spring MVC默认MIME类型

Spring-boot 测试加载外部 jars(相当于 loader.path)

java - 使用 Spring Embedded Kafka 测试 @KafkaListener

spring - 替换 spring-data Page 的全部内容,同时保持分页信息

Spring Boot selenium 与 Headless Chrome 的集成测试运行速度非常慢

java - 部署到tomcat时如何设置logback.xml的位置?

junit - 如何正确实现 attributeHasFieldErrors(没有 'AssertionError: No BindingResult for attribute: abc' 错误)