1

Here is my First Class AdminPostlCode. Contains some Strings and it's getters and Setters.

package org.sanket.zivameDataBase.adminPostalCode.model;

public class AdminPostalCode {

    private String entity_id1;
    private String postal_code1;
    private String city1;
    private String state1;
    private String country1;
    private String days_to_deliver1;
    private String cod1;
    private String pg1;
    private String rpu1;
    private String updated_by1;
    private String updated_at1;
    private String cod_couriername1;
    private String pg_couriername1;
    private String rto_couriername1;

    public AdminPostalCode(){}
    public AdminPostalCode(String entity_id1, String postal_code1,
            String city1, String state1, String country1,
            String days_to_deliver1, String cod1, String pg1, String rpu1,
            String updated_by1, String updated_at1, String cod_couriername1,
            String pg_couriername1, String rto_couriername1) {
        super();
        this.entity_id1 = entity_id1;
        this.postal_code1 = postal_code1;
        this.city1 = city1;
        this.state1 = state1;
        this.country1 = country1;
        this.days_to_deliver1 = days_to_deliver1;
        this.cod1 = cod1;
        this.pg1 = pg1;
        this.rpu1 = rpu1;
        this.updated_by1 = updated_by1;
        this.updated_at1 = updated_at1;
        this.cod_couriername1 = cod_couriername1;
        this.pg_couriername1 = pg_couriername1;
        this.rto_couriername1 = rto_couriername1;
    }


    public String getEntity_id1() {
        return entity_id1;
    }
    public void setEntity_id1(String entity_id1) {
        this.entity_id1 = entity_id1;
    }
    public String getPostal_code1() {
        return postal_code1;
    }
    public void setPostal_code1(String postal_code1) {
        this.postal_code1 = postal_code1;
    }
    public String getCity1() {
        return city1;
    }
    public void setCity1(String city1) {
        this.city1 = city1;
    }
    public String getState1() {
        return state1;
    }
    public void setState1(String state1) {
        this.state1 = state1;
    }
    public String getCountry1() {
        return country1;
    }
    public void setCountry1(String country1) {
        this.country1 = country1;
    }
    public String getDays_to_deliver1() {
        return days_to_deliver1;
    }
    public void setDays_to_deliver1(String days_to_deliver1) {
        this.days_to_deliver1 = days_to_deliver1;
    }
    public String getCod1() {
        return cod1;
    }
    public void setCod1(String cod1) {
        this.cod1 = cod1;
    }
    public String getPg1() {
        return pg1;
    }
    public void setPg1(String pg1) {
        this.pg1 = pg1;
    }
    public String getRpu1() {
        return rpu1;
    }
    public void setRpu1(String rpu1) {
        this.rpu1 = rpu1;
    }
    public String getUpdated_by1() {
        return updated_by1;
    }
    public void setUpdated_by1(String updated_by1) {
        this.updated_by1 = updated_by1;
    }
    public String getUpdated_at1() {
        return updated_at1;
    }
    public void setUpdated_at1(String updated_at1) {
        this.updated_at1 = updated_at1;
    }
    public String getCod_couriername1() {
        return cod_couriername1;
    }
    public void setCod_couriername1(String cod_couriername1) {
        this.cod_couriername1 = cod_couriername1;
    }
    public String getPg_couriername1() {
        return pg_couriername1;
    }
    public void setPg_couriername1(String pg_couriername1) {
        this.pg_couriername1 = pg_couriername1;
    }
    public String getRto_couriername1() {
        return rto_couriername1;
    }
    public void setRto_couriername1(String rto_couriername1) {
        this.rto_couriername1 = rto_couriername1;
    }

}

Here is My Database connection Class

package org.sanket.zivameDataBase.adminPostalCode.DataBase;

import java.sql.Connection;
import java.sql.DriverManager;


public class DataBaseConnection {
    public Connection getConnection() throws Exception
    {
    try
    {
    String connectionURL = "jdbc:mysql://localhost:3306/zdb";
    Connection connection = null;
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    connection = DriverManager.getConnection(connectionURL, "root", "root");
    return connection;
    } catch (Exception e)
    {
    throw e;
    }

    }


}

Here is my access class.

package org.sanket.zivameDataBase.adminPostalCode.DataBase;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.sanket.zivameDataBase.adminPostalCode.model.AdminPostalCode;

public class Access {

    public List<AdminPostalCode> getAdminPostalCode(int code,Connection con) throws SQLException
    {
        List<AdminPostalCode> adminDetail = new ArrayList<AdminPostalCode>();
        PreparedStatement stmt = con.prepareStatement("select * from admin_postalcode where postal_code ="+code+"");
        ResultSet resultSet = stmt.executeQuery();

    //System.out.println(resultSet);
        try
        {  
            while (resultSet.next()) 
            {   
                AdminPostalCode adminPostalCode = new AdminPostalCode(); 
                adminPostalCode.setEntity_id1(resultSet.getString("entity_id")); 
                adminPostalCode.setPostal_code1(resultSet.getString("postal_code"));
                adminPostalCode.setCity1(resultSet.getString("city"));
                adminPostalCode.setState1(resultSet.getString("state"));
                adminPostalCode.setCountry1(resultSet.getString("country"));
                adminPostalCode.setDays_to_deliver1(resultSet.getString("days_to_deliver"));
                adminPostalCode.setCod1(resultSet.getString("cod"));
                adminPostalCode.setPg1(resultSet.getString("pg"));
                adminPostalCode.setRpu1(resultSet.getString("rpu"));
                adminPostalCode.setUpdated_by1(resultSet.getString("updated_by"));
                adminPostalCode.setUpdated_at1(resultSet.getString("updated_at"));
                adminPostalCode.setCod_couriername1(resultSet.getString("cod_couriername"));
                adminPostalCode.setPg_couriername1(resultSet.getString("pg_couriername"));
                adminPostalCode.setRto_couriername1(resultSet.getString("rto_couriername"));

                adminDetail.add(adminPostalCode);

            }


        }catch (SQLException e)
            {
            e.printStackTrace();
            }
        return adminDetail ;


    }

}

Now I want to write JUnit test case for Above code. And I tried in following way. But it is not working Please Help me in writing JUnit Test case. Remaining Everything is working fine.

package adminPostalCode;

import static org.junit.Assert.*;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.sanket.zivameDataBase.adminPostalCode.DataBase.Access;
import org.sanket.zivameDataBase.adminPostalCode.DataBase.DataBaseConnection;
import org.sanket.zivameDataBase.adminPostalCode.model.AdminPostalCode;

public class SampleTest {

    List<AdminPostalCode> detailsActual = new ArrayList<AdminPostalCode>();
    List<AdminPostalCode> detailsExpected = new ArrayList<AdminPostalCode>();

    boolean abc;

    AdminPostalCode adminPoCo = new AdminPostalCode();

    public SampleTest()
    {
        adminPoCo.setCity1("WEST CHAMPARAN");
        adminPoCo.setCod1("0");
        adminPoCo.setCod_couriername1("NO SERVICE");
        adminPoCo.setDays_to_deliver1("11");
        adminPoCo.setEntity_id1("19625");
        adminPoCo.setPg1("1");
        adminPoCo.setPg_couriername1("INDIA POST");
        adminPoCo.setPostal_code1("845106");
        adminPoCo.setRpu1("0");
        adminPoCo.setRto_couriername1("NO SERVICE");
        adminPoCo.setState1("BIHAR");
        adminPoCo.setUpdated_by1("panindra@milastar.in");
        adminPoCo.setUpdated_at1("2015-03-19 19:30:22.0");
        adminPoCo.setCountry1("IN");

        detailsExpected.add(adminPoCo);

    }

    @Test
    public void test() throws Exception {
        //fail("Not yet implemented");
        DataBaseConnection db = new DataBaseConnection();
        Connection con = db.getConnection();
        Access access = new Access();
        detailsActual =access.getAdminPostalCode(845106, con);

        abc = (detailsActual.equals(detailsExpected)) ; 
             System.out.println(abc);


    }

}
Sanket
  • 31
  • 1
  • 4

4 Answers4

1
  • use assertXxx in junit instead of System.out. junit is used to automate your test and find potential problems in your code and provide useful information about them. junit will help much less if you use System.out.

  • if you use junit to write a unit test, you should not connect the 'real' database, you should use some mock framework such as mockito to mock an Access object.

  • if you want write integration test using junit, you should use some specific junit extension framework like dbunit.

walsh
  • 3,041
  • 2
  • 16
  • 31
0

You have to use assertEquals for testing

assertEquals(detailsActual,detailsExpected);

if you are comparing arrayList object, you have to convert them to array than you can use assertArrayEquals.

sTg
  • 4,313
  • 16
  • 68
  • 115
BoraKoc
  • 1
  • 3
  • @Sanket You're tagging the wrong person. Shirish simply edited the answer. BoraKoc is the one who answered – Vince Aug 05 '15 at 07:52
  • @Sanket are you running as JUnit Test? Your IDE has at least give you some results in Junit tab. – BoraKoc Aug 05 '15 at 08:05
0

It seems what you are looking for is away to compare 2 objects of type ArrayList.

The answer to this is by overriding equals method.

Add the below code to your AdminPostalCode POJO class.

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    final AdminPostalCode other = (AdminPostalCode) obj;
    if (city1 == null) {
        if (other.city1 != null)
            return false;
    } else if (!city1.equals(other.city1))
        return false;
    if (cod1 == null) {
        if (other.cod1 != null)
            return false;
    } else if (!cod1.equals(other.cod1))
        return false;
    if (cod_couriername1 == null) {
        if (other.cod_couriername1 != null)
            return false;
    } else if (!cod_couriername1.equals(other.cod_couriername1))
        return false;
    if (country1 == null) {
        if (other.country1 != null)
            return false;
    } else if (!country1.equals(other.country1))
        return false;
    if (days_to_deliver1 == null) {
        if (other.days_to_deliver1 != null)
            return false;
    } else if (!days_to_deliver1.equals(other.days_to_deliver1))
        return false;
    if (entity_id1 == null) {
        if (other.entity_id1 != null)
            return false;
    } else if (!entity_id1.equals(other.entity_id1))
        return false;
    if (pg1 == null) {
        if (other.pg1 != null)
            return false;
    } else if (!pg1.equals(other.pg1))
        return false;
    if (pg_couriername1 == null) {
        if (other.pg_couriername1 != null)
            return false;
    } else if (!pg_couriername1.equals(other.pg_couriername1))
        return false;
    if (postal_code1 == null) {
        if (other.postal_code1 != null)
            return false;
    } else if (!postal_code1.equals(other.postal_code1))
        return false;
    if (rpu1 == null) {
        if (other.rpu1 != null)
            return false;
    } else if (!rpu1.equals(other.rpu1))
        return false;
    if (rto_couriername1 == null) {
        if (other.rto_couriername1 != null)
            return false;
    } else if (!rto_couriername1.equals(other.rto_couriername1))
        return false;
    if (state1 == null) {
        if (other.state1 != null)
            return false;
    } else if (!state1.equals(other.state1))
        return false;
    if (updated_at1 == null) {
        if (other.updated_at1 != null)
            return false;
    } else if (!updated_at1.equals(other.updated_at1))
        return false;
    if (updated_by1 == null) {
        if (other.updated_by1 != null)
            return false;
    } else if (!updated_by1.equals(other.updated_by1))
        return false;
    return true;
}

The above method will help you compare the 2 Actual and Expected objects. Though, a correct JUnit will make use of assertEquals, verify, etc to validate results

Maulik Shah
  • 402
  • 1
  • 4
  • 18
0

Your AdminPostalCode class should implement boolean equals(Object other) and also int hashCode(). Implementing these methods will guarantee that custom objects, like your AdminPostalCode class, will be able to perform checks on certain fields. assertEquals(detailsActual, detailsExpected) f.e. invokes this method internally. By default equals(...) compares objects on their memory address instead of the content. That's why you should overwrite these two methods.

If you don't want to overwrite these methods (for whatever reason), you also have the possibility to implement custom Hamcrest matchers which will perform the equality check. You are then able to utilize this matcher like this: assertThat(detailsActual, containsAdminPostalCodes(detailsExpected)); where containsAdminPostalCodes(...) is your custom Hamcrest matcher which takes a List of AdminPostalCode as input. Inside this Hamcrest matcher you will need to compare the fields of the AdminPostalCode instances of the actual list with the one provided in the expected list.

Hamcrest comes already with quite a lot of matchers. So check if there are already any available that suits your need, though some may rely on a correct implementation of equals(...) and hashCode().

Also, as @walsh has correctly pointed out, you should not call a database in your unit-test directly but instead use mocked objects (Mockito, PowerMock, ...). If you write integration tests (our company f.e. prefers integration tests over unit-tests), try to refactor the initialization and tear-down code for your database into JUnit rules

Community
  • 1
  • 1
Roman Vottner
  • 12,213
  • 5
  • 46
  • 63