![]() |
VOOZH | about |
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.
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
worker table db data:
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.
WorkerApplicationTest.java
This file can be run as a JUnit Test
When expected values and actual DB data values match for all test cases, we can see like below output
In case of any errors, they can be shown as follows