Connect to a Database Instance Running Inside a Java Testcontainer

Posted by Sumit KumarPublished on Dec 17, 2020
1 min read
SEO | Connect to a Database Instance Running Inside a Java Testcontainer Using IntelliJ IDEA

Challenge with Databases running inside testcontainers

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 spawned using testcontainers when run from within the IDE can become inaccessible from outside the IDE. To connect to such databases, you can use the database tool that comes with IDEA Ultimate. The steps below will cover how we can do this. I've also put across screenshots to explain each of the steps better.

So lets's get started

Add a debug point in the code

You want to add it such that the testcontainer has been brought up at this point. Here's a screenshot showing this.

Run the test program using debug mode

When you run the test program using this debug mode, do wait till it stops on the break point.

Click on the database tool

Select your database type

Enter your credentials

Be sure to test your connection and save those credentials.

Run query