DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Why "Polyglot Programming" or "Do It Yourself Programming Languages" or "Language Oriented Programming" sucks?
  • The Most Popular Technologies for Java Microservices Right Now
  • The First Annual Recap From JPA Buddy
  • Prototype for a Java Database Application With REST and Security

Trending

  • Improving DAG Failure Detection in Airflow Using AI Techniques
  • Detecting Bugs and Vulnerabilities in Java With SonarQube
  • Build Self-Managing Data Pipelines With an LLM Agent
  • Throughput vs Goodput: The Performance Metric You Are Probably Ignoring in LLM Testing
  1. DZone
  2. Data Engineering
  3. Databases
  4. Connect to a Database Instance Running Inside a Java Testcontainer Using IntelliJ IDEA

Connect to a Database Instance Running Inside a Java Testcontainer Using IntelliJ IDEA

Learn how to use IntelliJ IDEA along with Testcontainers and JUnit to write effective unit tests for your Java module that communicate with databases.

By 
Sumit Kumar user avatar
Sumit Kumar
·
Oct. 27, 21 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
5.9K Views

Join the DZone community and get the full member experience.

Join For Free

JUnit is one of the most popular unit testing frameworks used with Java to create repeatable tests. With JUnit, each test is written as a separate method inside a Java class. IntelliJ IDEA provides an option to run these test cases from within the IDE.

In case you have a module that communicates with a MySQL database, you can unit test the module by providing it access to a MySQL server running inside a Testcontainer. You can also configure this MySQL database instance with your desired username, password, and database name (in MySQL server) using the API provided by the Testcontainers framework.

In case you use Maven to manage dependencies in your project, as used in Appsmith (the open-source low code project that I work on), you can add the following snippet in your POM file to include all the required packages:

Java
 
<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>testcontainers</artifactId>
    <version>1.15.1</version>
    <scope>test</scope>
</dependency>


To create a new MySQL Testcontainer instance with JUnit 4, you may follow these steps as used in Appsmith's unit test file to test its MySQL plugin:

Java
 
public static MySQLContainer mySQLContainer = new MySQLContainer("mysql:5.7")
            .withUsername("username")
            .withPassword("password")
            .withDatabaseName("test_db");


Please note that the Testcontainers framework is different for JUnit4 and JUnit5. Please use the framework as per the JUnit version that you have used. For more details, please see the Testcontainers page.

Databases spawned using Testcontainers when run from within the IDE can seem to become inaccessible from outside the IDE. In order to connect to such databases, you can use the database tool that comes with IDEA Ultimate version.

Steps to Connect to the MySQL Database

1. Add a debug point in the code such that the Testcontainer has been brought up at this point.

Adding a debug point in the code


2. Run the test program using debug mode and wait till it stops on the breakpoint.

Running the test program using debug mode
Programming stopping on the breakpoint


3. Click on the database tool.

Clicking on database tool


4. Select your database type.

Selecting database type


5. Fetch your credentials. You may read the credentials from the Testcontainer using the following API when using it with JUnit 4.

Java
 
address  = mySQLContainer.getContainerIpAddress();
port     = mySQLContainer.getFirstMappedPort();
username = mySQLContainer.getUsername();
password = mySQLContainer.getPassword();
database = mySQLContainer.getDatabaseName();


6. Test your connection and save credentials.

Testing the connection

7. Run query.

Running the query

Wrapping Up

It is worth noting that Testcontainers provide containerized instances of many other popular databases as well, like Postgres, MongoDB, and Neo4j. Similarly, IntelliJ IDEA's database tool also provides connectivity support for most of the popular databases. The steps described above to integrate the Testcontainers package or to investigate the containerized database can be used with databases other than MySQL as well. In summary, the steps to write a unit test using JUnit and any Testcontainer can be generalized as follows:

  1. Add dependency for JUnit package
  2. Add dependency for Testcontainers package
  3. Write a code snippet to start a containerized instance of your desired database

The steps to investigate the containerized database instantiated above can be generalized as follows:

  1. Add a debug point after the container is instantiated but before the test ends
  2. Start the test and wait till the execution stops at the debug point
  3. Use the database tool to connect to the containerized database and run queries on it

In case you need access to more code examples to see the above steps in usage, check out the test files in Appsmith's GitHub repository. I hope this article was useful to you and please share your feedback in the comments.

intellij Database unit test Java (programming language)

Published at DZone with permission of Sumit Kumar. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Why "Polyglot Programming" or "Do It Yourself Programming Languages" or "Language Oriented Programming" sucks?
  • The Most Popular Technologies for Java Microservices Right Now
  • The First Annual Recap From JPA Buddy
  • Prototype for a Java Database Application With REST and Security

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook