VOOZH about

URL: https://www.geeksforgeeks.org/software-testing/junit-sample-maven-project-with-jdbc/

⇱ JUnit - Sample Maven Project With JDBC - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

JUnit - Sample Maven Project With JDBC

Last Updated : 23 Jul, 2025

The quality of software should be of a high standard and then only it is liked and used by most people. A good programmer is one who prepares software error-free, JUnit helps us a lot in achieving that. In this article, let us see a MySQL database connecting with Java via JDBC, and the data is checked against JUnit.

Example Maven Project

Project Structure:

👁 Project Structure
 

Maven Project and hence let us look at the dependencies specified in

pom.xml

MySQL connectivity is specified in

ds-connection.properties

# DataSource 
ds.database-driver=com.mysql.jdbc.Driver
ds.url=jdbc:mysql://localhost:3306/geeksforgeeks
ds.username=*** #Specify username
ds.password=*** #Specify password

Let us assume that there exists a database named "geeksforgeeks " and it has a table named "worker" the structure of "worker" table is given below

👁 Image
 

worker table db data:

👁 Image
 

Here we can get the connection

ConnectionClass.java

Let us create the Worker class now

Worker.java

WorkerQueries.java

This java program helps us to test the available DB data is helpful for subsequent operations.

  1. It will help to set the integrity of the project
  2. By testing in these ways, we can find wrongly entered data and avoid it.
  3. Quality of software is improved and customizing them helps to prepare error-free software.

WorkerApplicationTest.java

This file can be run as a JUnit Test 

👁 Run JUnit
 

When expected values and actual DB data values match for all test cases, we can see like below output

👁 JUnit Test Passed
 

In case of any errors, they can be shown as follows

👁 JUnit Test Failure
 
Comment
Article Tags:

Explore