这是我的First Class AdminPostlCode。包含一些字符串及它的getter和Setter。
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;
}
}
这是我的数据库连接类
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;
}
}
}
这是我的访问类。
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 ;
}
}
现在我想为Above代码编写JUnit测试用例。我尝试了以下方式。但它不起作用请帮我写JUnit测试用例。剩下的一切都很好。
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);
}
}
答案 0 :(得分:1)
在junit而不是System.out中使用assertXxx。 junit用于自动化测试并在代码中发现潜在问题并提供有关它们的有用信息。如果你使用System.out,junit将帮助少得多。
如果使用junit编写单元测试,则不应连接“真实”数据库,应使用某些模拟框架(如mockito)来模拟Access对象。
如果你想使用junit进行写集成测试,你应该使用一些特定的junit扩展框架,比如dbunit。
答案 1 :(得分:0)
您必须使用assertEquals进行测试
assertEquals(detailsActual,detailsExpected);
如果您要比较arrayList
个对象,则必须将它们转换为array
,而不是使用assertArrayEquals
。
答案 2 :(得分:0)
看起来你正在寻找的是比较ArrayList类型的两个对象。
答案是通过重写equals方法。
将以下代码添加到AdminPostalCode POJO类。
@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;
}
上述方法将帮助您比较2个Actual和Expected对象。 但是,正确的JUnit将使用 assertEquals,验证等来验证结果
答案 3 :(得分:0)
您的AdminPostalCode
课程应该实施boolean equals(Object other)
和also int hashCode()
。实现这些方法将保证自定义对象(如AdminPostalCode
类)能够对某些字段执行检查。 assertEquals(detailsActual, detailsExpected)
f.e。在内部调用此方法。默认情况下,equals(...)
会比较memory address上的对象而不是内容。这就是你应该覆盖这两种方法的原因。
如果您不想覆盖这些方法(无论出于何种原因),您还可以实现custom Hamcrest matchers来执行相等性检查。然后,您可以像这样使用此匹配器:assertThat(detailsActual, containsAdminPostalCodes(detailsExpected));
其中containsAdminPostalCodes(...)
是您的自定义Hamcrest匹配器,它将AdminPostalCode列表作为输入。在这个Hamcrest匹配器中,您需要将实际列表的AdminPostalCode实例的字段与预期列表中提供的字段进行比较。
Hamcrest已经有了很多matchers。因此,请检查是否有适合您需要的可用内容,但有些可能依赖于equals(...)
和hashCode()
的正确实施。
另外,正如@walsh正确指出的那样,你不应该直接在单元测试中调用数据库,而是使用模拟对象(Mockito,PowerMock,...)。如果您编写集成测试(我们的公司f.e。更喜欢集成测试而不是单元测试),请尝试将数据库的初始化和拆除代码重构为JUnit rules