Connect to database instance running inside a Java testcontainer using IntelliJ IDEA

Testcontainers library (Java) is generally used along with Junit test cases to spawn a test environment within a docker container. IntelliJ IDEA provides an option to run these test cases from within the IDE.

Databases spwaned using testcontainers when run from within the IDE can become inaccessible from outside the IDE. In order to connect to such databases you can uses the database tool that comes with IDEA ultimate version.

Steps:

  1. Add a debug point in the code such that the testcontainer has been brought up at this point. Screenshot 2020-12-17 at 10.58.27 AM.png

  2. Run the test program using debug mode and wait till it stops on the break point. Screenshot 2020-12-17 at 10.59.27 AM.png Screenshot 2020-12-17 at 11.09.16 AM.png

  3. Click on the database tool. Screenshot 2020-12-17 at 11.10.00 AM.png

  4. Select your database type. Screenshot 2020-12-17 at 11.12.06 AM.png

  5. Enter your credentials. Test your connection and save credentials. Screenshot 2020-12-17 at 11.17.17 AM.png

  6. Run query. Screenshot 2020-12-17 at 11.19.44 AM.png