Writing your first MYSQL DB functional Test Case in J unit

In junit you can write test cases for any SQL Queries here I am using MYSQL however if you want to perform this on some other D.B you will just have to change connection configurations according to that D.B.
You need to download MY SQL Connector for java and import that in your project.


Creating Connection Class

First Step is to create a connection class with data base here I am following singleton pattern for creating connection with D.B
Example Code is :
import com.mysql.*;

import com.mysql.jdbc.Connection;

public class dbConnectionInstance {
public static Connection conn;
   static String url= “jdbc:mysql://localhost:3306/”;
       static String dbName = “test_data_eoffice”;
       static String driver = “com.mysql.jdbc.Driver”;
       static String userName = “root”;
       static String password = “”;
  public static Connection MysqlConnect() {
       if ( conn == null ) {
        try {
        conn = (Connection)DriverManager.getConnection(url+dbName,userName,password);
       catch (Exception sqle) {
        return conn;
       return conn;

Benefit of using singleton pattern is that you won’t need to created create a connection object ever time you will simply call this instance by dbConnectionInstance. MysqlConnect(); and this method will return you connection.

Creating Functionalities Class

This class will contain execution of all queries which we want to test
First step is to create this class and identify a query here I am writing a test case for query “insert into testdatacreatefile”
I created a function public void createFileTestDataGenarator()
Then I executed this query with in this function
Now my test functionality is ready to be called in a J-Unit Test case  

Example code is :
import java.sql.Connection;

import java.sql.SQLException;
import java.sql.Statement;

public class TestDataGenerator {
private Connection connect = dbConnectionInstance.MysqlConnect();
 private Statement statement = null;
 public void createFileTestDataGenarator()
 try {
 for(int i=1000; i<=2000; i++)
 { String fileRefNum=“Test FIle Reference Number”+i;
 String Subject=“Test FIle Subject Number”+i;
 String Keywords =“keyword”+i+“A,keyword”+i+“B”;
 String Decscription=“Test FIle Description Number”+i;
 int status=0;  
 statement.executeUpdate(“insert into  testdatacreatefile(fileSubject,fileReferenceNumber,fileKeyword,fileDescription,testDataStatus) values (‘”+Subject+“‘,'”+fileRefNum+“‘,'”+Keywords+“‘,'”+Decscription+“‘,”+status+“)”);
 }} catch (SQLException e) {
// TODO Auto-generated catch block
 public void Log(String log)
 try {
 statement.executeUpdate(“insert into  logger(step) values (‘”+log+“‘)”);
 } catch (SQLException e) {
// TODO Auto-generated catch block
 public void createDocumentTestDataGenarator()
 try {
 for(int i=1000; i<=2000; i++)
String DocumentRefNum=“Test Document Reference Number”+i;
 String Subject=“Test Doucument subject “+i;
 String Decscription=“Test Document  Description”+i;
 String FromMinsitry=“Some Test Mionistry”+i;
 String FromDesigntation=“Some Test Mionistry Designation ABC”+i;
 String FromName=“Some Test Misnitry Named ABC:GM”+i;
 String FromMedium=“Some Test Courior Service”+i;
 int status=0;  
 statement.executeUpdate(“insert into  testdatacreatedocument( DocumentRef,DocumentDesc,FromMinistry,FromDesignation,FromName,FromMedium,Status) values (‘”+DocumentRefNum+“‘,'”+Decscription+“‘,'”+FromMinsitry+“‘,'”+FromDesigntation+“‘,'”+FromName+“‘,'”+FromMedium+“‘,”+status+“)”);
 }} catch (SQLException e) {
// TODO Auto-generated catch block

Creating J unit Class

Left Click your project and add a J Unit Test Case
There are three basic attenuation is Junit
@Before: this attenuation is used for initialization
@ Test: with this attenuation you call your actual test or execute with in this attenuation
@ After: this attenuation is used at the end of test e.g for closing all DB connections
Next step is now to call you created functionality in @ test Attenuation
Create Object of that class and then call its instance like example below
 public void testCreateFIle() throws Exception {
TestDataGenerator TDG= new TestDataGenarator();
 //FM.addNtoing(“fazal.karim”, baseUrl, driver, “Abdul Hafeez“, “KPO”, “abdul.hafeeez”);
// Thread.sleep(5000L);
At Last execute your Junit Class as a Junit project not as a Java Application
For knowing more about MY SQL and java refer following links


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s