Geoffrey Wiseman
Geoffrey Wiseman

Reputation: 5637

Accessing H2 Console While Running SpringBootTest

If I'm running a test with @SpringBootTest is there any way to access the H2 console? I have a test that accesses an H2 database (successfully), but if I want to inspect the database myself, how can I?

I started by running the test with a webEnvironment=DEFINED_PORT and http://localhost:8080/ responds to HTTP, but there's nothing http://localhost:8080/h2-console/ still responds with 404.

I tried turning on the console explicitly by adding values to application.properties:

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

That seemed to have no effect. Still 404s at /h2-console and /h2.

The H2 Console seems to come in through Auto-Configuration, so I turned on the auto configuration report using -Ddebug, and I can see that despite the enabled flag being on in application.properties, it's seen as being off:

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)

Seems like some mechanism is overriding the value or ignoring the value from application.properties while running the test.

Does Spring Boot Test not start up the console? Is there any way to convince it to do so?

Upvotes: 11

Views: 7898

Answers (1)

Geoffrey Wiseman
Geoffrey Wiseman

Reputation: 5637

Ok, I was able to get it to appear with the following annotation (in Kotlin): @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, properties = arrayOf("spring.h2.console.enabled=true"))

Even with this, the console is still at /h2-console, so it's clearly ignoring the console path as well. I could presumably control that by adding another property.

Upvotes: 13

Related Questions