Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 335
Geoffrey Wiseman Avatar asked May 16 '18 15:05

Geoffrey Wiseman


People also ask

How do I access my H2 console?

Accessing the H2 Console By default, the H2 console is not enabled in Spring. Then, after starting the application, we can navigate to http://localhost:8080/h2-console, which will present us with a login page. The web console has an auto-complete feature that suggests SQL keywords.

How do I access my H2 memory database?

To access an in-memory database from another process or from another computer, you need to start a TCP server in the same process as the in-memory database was created. The other processes then need to access the database over TCP/IP or TLS, using a database URL such as: jdbc:h2:tcp://localhost/mem:db1 .

How do I know if H2 is running?

Step 3: Verify H2 Database InstallationClick Windows → type H2 Console → Click H2 console icon. Connect to the URL http://localhost:8082. At the time of connecting, the H2 database will ask for database registration as shown in the following screenshot.


1 Answers

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.

like image 164
Geoffrey Wiseman Avatar answered Sep 25 '22 00:09

Geoffrey Wiseman