I\'m building a Spring application and I need to inspect my H2 in-memory database while I\'m running my JUnit tests from a web browser.
In my Spring configuration I
For future reference here's another way to do it:
Start database and web servers (version can differ):
$ cd .../maven_repository/com/h2database/h2/1.4.194
$ java -cp h2-1.4.194.jar org.h2.tools.Server -tcp -web -browser
TCP server running at tcp://169.254.104.55:9092 (only local connections)
Web Console server running at http://169.254.104.55:8082 (only local connections)
Set database url for tests in code to jdbc:h2:tcp://localhost:9092/mem:mytest
.
Connect
in browser window which opened in step 1.Jar file for H2 can be downloaded at https://mvnrepository.com/artifact/com.h2database/h2.
Server can be started via @Before
in test file like in snovelli's answer, but only in case connection to database in established afterwards, which might be a problem.
I guess the problem is that you are connecting to h2db directly from your application. Not through the server you are launching with bean. Because of this your app and h2db-web-interface can't share one in-memory database.
You should change jdbcUrl in tests to something like jdbc:h2:tcp://localhost/mem:my_DB;DB_CLOSE_DELAY=-1;MODE=Oracle
and in browser you should connect to the same url.
With jdbc urls like jdbc:h2:tcp://localhost/...
all connections will go through the h2db-server and you can view database state in browser.
As this is probably going to be a test-debugging feature, you can add it at runtime with your @Before:
import org.h2.tools.Server;
/* Initialization logic here */
@BeforeAll
public void initTest() throws SQLException {
Server.createWebServer("-web", "-webAllowOthers", "-webPort", "8082")
.start();
}
And then connect to http://localhost:8082/