Access to h2 web console while running junit test in a Spring application

18,693

Solution 1

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/

Note: unless you need this to run as part of your CI build, you'll need to remove this code when you're finished debugging

Solution 2

For future reference here's another way to do it:

  1. 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)

  2. Set database url for tests in code to jdbc:h2:tcp://localhost:9092/mem:mytest.

  3. Run or debug tests.
  4. Click 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.

Solution 3

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.

Share:
18,693

Related videos on Youtube

Ivan Fernandez
Author by

Ivan Fernandez

About me. I'm a passionate developer who is keen on seeking new technologies related to web or mobile development. I'm currently working as a JEE programmer analyst with frameworks such as Spring, Hibernate, JQuery or Apache CXF. Besides that I'm really interested in others frameworks such as Grails, Ruby or Node.js. I'm also taking my first steps in the front-end layer (Backbone.js, Express.js, Jade, Sass ...). It's pretty nice how is integrating lately with other mobile technologies like JQuery Mobile and PhoneGap. I'm always looking for new things to learn too. Lately, I'm really interested in cloud computing and NoSQL technologies, such as Amazon Web Services, Google App Engine, Cloud Foundry or Heroku. I'm starting with some new cool stuff like Hadoop, MongoDB or MemCached. I've been writting apps getting the information from some REST API (Twitter, Yahoo or Google) lately. You can take a look at: http://mycityfinder.cloudfoundry.com/ Grails + JQuery + Google Reader + Flickr + Eventful API http://looking4tweets.herokuapp.com/ Node.js + Express.js + Backbone.js + Jade + Less. I do like writing in my blog as well. Since I started to write it, I became interested in SEO issues, Wordpress administration, ... Specialties Web development : JEE, Spring, Hibernate, Maven. Mobile development: Android, PhoneGap. Any stuff regarding blog writing. Please, visit my blog if you feel like it: http://hop2croft.wordpress.com

Updated on June 15, 2022

Comments

  • Ivan Fernandez
    Ivan Fernandez almost 2 years

    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 have a bean which is responsible of creating my database schema and populating it with some data which will be used within my JUnit tests. I've also added a bean in my test context which creates a web server where I eventually will look for my data.

    <bean id="org.h2.tools.Server-WebServer" class="org.h2.tools.Server"
        factory-method="createWebServer" init-method="start" lazy-init="false">
        <constructor-arg value="-web,-webAllowOthers,-webPort,11111" />
    </bean>
    

    Everything seems ok because the database is populated properly since I can access to its data from my JUnit tests and H2 Server only runs while I'm in my test-phase (I can know that, because if I try to access to my_ip:111111 before debugging my tests I cannot connnect but I can connect afterwards once I've started my tests).

    Anyway If I open my H2 console from a web browser no schema is shown in it. Any ideas??

    Many thanks!!

    • michael
      michael over 11 years
      What does your jdbcUrl in unit tests look like?
    • Ivan Fernandez
      Ivan Fernandez over 11 years
      Hi Michael, thank you for your answer. My jdbc URL looks like jdbc:h2:mem:my_DB;DB_CLOSE_DELAY=-1;MODE=Oracle I've also tried to add the IFEXIST property in the jdbc URL just in case that could help me. It couldn't :(
    • ROCKY
      ROCKY about 11 years
      Hello @Ivan Fernandez can you let us know how you have solved this problem ? I am facing the same issue. You answer is much appreciated. Thanks !!
    • Manu
      Manu over 7 years
      Were you able to resolve the issue ?
  • Purushothaman
    Purushothaman over 4 years
    If you have not provided connection URL, then the you could connect to h2 db from the console using default URL jdbc:h2:mem:dataSource
  • Mehrdad HosseinNejad Yami
    Mehrdad HosseinNejad Yami over 4 years
    It was better to use BeforeClass instead of Before
  • Chamila Wijayarathna
    Chamila Wijayarathna almost 4 years
    Can we observe "jdbc:h2:mem:test" in memory H2 DB using the web console?