代码之家  ›  专栏  ›  技术社区  ›  Geoffrey Wiseman

运行SpringBootTest时访问H2控制台

  •  6
  • Geoffrey Wiseman  · 技术社区  · 6 年前

    如果我用 @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
    

    这似乎没有效果。仍为404s at /h2-console /h2 .

    H2控制台似乎是通过自动配置进入的,所以我使用 -Ddebug ,我可以看到,尽管在应用程序中启用了enabled标志。属性,它被视为关闭:

    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)
    

    似乎有某种机制正在覆盖值或忽略 应用属性 运行测试时。

    Spring Boot Test是否未启动控制台?有没有办法说服它这样做?

    1 回复  |  直到 6 年前
        1
  •  10
  •   Geoffrey Wiseman    6 年前

    好的,我可以用以下注释显示它(在Kotlin中): @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, properties = arrayOf("spring.h2.console.enabled=true"))

    即使在控制台上 /h2-console ,因此它显然也忽略了控制台路径。我大概可以通过添加另一个属性来控制它。