0

Not able to access H2 database console during JUnit testing. Since the database will be up and running only when the test is running, I am trying to have a breakpoint or a sleep command to hold the test execution to access http://localhost:8080/h2-console in the browser, but getting "This site can’t be reached" error.

Sleep command I am using : TimeUnit.MINUTES.sleep(2);

POM.xml

spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1;Mode=PostgreSQL
spring.datasource.username=sa
spring.datasource.password=sa
hibernate.hbm2ddl.auto=create
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.hibernate.default_schema=
logging.level.root=DEBUG
hibernate.show_sql=true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.hibernate.use-new-id-generator-mappings=true
hibernate.dialect=org.hibernate.dialect.H2Dialect
spatel
  • 11
  • 2
  • 1
    Hitting a breakpoint during testing i normally am able to use the Debugger IDE (like Intellij or eclipse) to access an injected entitymanager which I can use to do querys on the database. – aschoerk Mar 16 '21 at 22:39
  • 1
    https://stackoverflow.com/a/66259759/410439 – Ravi Parekh Sep 15 '21 at 13:31

1 Answers1

0

To have access to the h2 console you need to spin up the whole application not just the persistence layer there for you can only do it in @SpringBootTest.

Also, you should enable the web endpoint and not use mock during the test.

Finally, do not block all the threads during the test so the h2 console can process the incoming requests.

Suppose we have a Person entity a PersonRepository and we have configured H2. Here is the test code and configuration you need to do in one image:

enter image description here

Mr.Q
  • 4,316
  • 3
  • 43
  • 40