Search code examples
spring-bootspring-data-jpaspring-boot-test

How do I enable H2 Console in a Spring Boot Library?


I have a non-web Spring Boot Library which has persistence entities that will be used by web application clients to communicate with the database. When I create my tests, I'm using @SpringBootTest in my spring integration tests, thus simulating a spring boot application using my library.

When setting the spring.h2.console.enabled to true, I thought I would be able to debug through my integration tests and have the H2 console enabled so that I can peer into the data by going to http://localhost:8080/h2-console, but the console doesn't load in my browser.

I have suspicions that this is somehow related to the fact that since my library is not a web-mvc library, it isn't auto configuring the H2 WebServlet, but I would like to know if providing the property would be enough for spring boot to auto configure the servlet.

Does anyone know if providing the H2 property would be enough? Or do I need to do something to enable the H2 WebServlet to be enabled?


Solution

  • I managed to get it working (somewhat) by adding a webEnvironment entry in the @SpringBootTest annotation. This allowed the H2 WebServlet to be registered. The reason that I said "somewhat" was because whenever I debug the test, the thread seems to suspend and I get very delayed response times from H2 when requesting it in my browser (http://localhost:8080/h2-console). My H2 database is very small (500 KB) so I would assume this has something to do with the server being embedded and tied to the test somehow?