在开发项目 时常会编写方法的测试,如果为公共方法junit提供测试办法,类中的方法不全为公共方法,若为私有方法怎样测试呢。下面给出私有方法的测试办法。
下面是一个组件类:
public class Component extends DefaultComponent {
/**
* 功能描述:获得业务数组
* @param con
* @param xzqh
* @param pcs
* @param csrq
* @return
* @throws Exception
*/
private int[] getArray(Connection con, ParamObject paramObject) {
String sql = "select nfpwh,wfpwh from audanum where xzqh=? and pcscod=? and csrq=?";
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
preparedStatement = con.prepareStatement(sql);
preparedStatement.setString(1, paramObject.getXzqh());
preparedStatement.setString(2, paramObject.getPcscode());
preparedStatement.setString(3, paramObject.getCsrq());
resultSet = preparedStatement.executeQuery();
resultSet.next();
int[] whArray = { resultSet.getInt("nfpwh01"),
resultSet.getInt("wfpwh02") };
return whArray;
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return null;
}
}
/**
* 功能描述:获得业务数组
* @param con
* @param xzqh
* @param pcs
* @param csrq
* @return
* @throws Exception
*/
private int[] getArray(Connection con, ParamObject paramObject) {
String sql = "select nfpwh,wfpwh from audanum where xzqh=? and pcscod=? and csrq=?";
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
preparedStatement = con.prepareStatement(sql);
preparedStatement.setString(1, paramObject.getXzqh());
preparedStatement.setString(2, paramObject.getPcscode());
preparedStatement.setString(3, paramObject.getCsrq());
resultSet = preparedStatement.executeQuery();
resultSet.next();
int[] whArray = { resultSet.getInt("nfpwh01"),
resultSet.getInt("wfpwh02") };
return whArray;
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return null;
}
}
测试类如下:
利用类的反射机制,得到类中的方法。
public class ComponentTest extends TestCase ...{
private final String URL = "jdbc:oracle:thin:@192.10.0.23:1521:orcl";
private final String USERNAME = "username";
private final String PASSWORD = "password";
private Connection connection = null;
private ParamObject paramObject = null;
private SfzhComponent comp = null;
@Override
protected void setUp() throws Exception ...{
super.setUp();
connection = getConnection();
paramObject = getParamObject();
comp = new SfzhComponent();
}
private Connection getConnection() ...{
Connection connection = null;
try ...{
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}
try ...{
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (SQLException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
private ParamObject getParamObject() ...{
ParamObject paramObject = new ParamObject();
paramObject.setCsrq("20071217");
paramObject.setPcscode("069");
paramObject.setPcscodemc("西关派出所");
paramObject.setXb("1");
paramObject.setXzqh("610104");
paramObject.setXzqhmc("陕西省西安市莲湖区");
return paramObject;
}
public void testgetArray() ...{
try ...{
Method m = comp.getClass().getDeclaredMethod("getArray",
new Class[] ...{ Connection.class, ParamObject.class });
m.setAccessible(true);
Object ob = m
.invoke(comp, new Object[] ...{ connection, paramObject });
assertEquals(true, ob != null);
} catch (Exception e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private final String URL = "jdbc:oracle:thin:@192.10.0.23:1521:orcl";
private final String USERNAME = "username";
private final String PASSWORD = "password";
private Connection connection = null;
private ParamObject paramObject = null;
private SfzhComponent comp = null;
@Override
protected void setUp() throws Exception ...{
super.setUp();
connection = getConnection();
paramObject = getParamObject();
comp = new SfzhComponent();
}
private Connection getConnection() ...{
Connection connection = null;
try ...{
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}
try ...{
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (SQLException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
private ParamObject getParamObject() ...{
ParamObject paramObject = new ParamObject();
paramObject.setCsrq("20071217");
paramObject.setPcscode("069");
paramObject.setPcscodemc("西关派出所");
paramObject.setXb("1");
paramObject.setXzqh("610104");
paramObject.setXzqhmc("陕西省西安市莲湖区");
return paramObject;
}
public void testgetArray() ...{
try ...{
Method m = comp.getClass().getDeclaredMethod("getArray",
new Class[] ...{ Connection.class, ParamObject.class });
m.setAccessible(true);
Object ob = m
.invoke(comp, new Object[] ...{ connection, paramObject });
assertEquals(true, ob != null);
} catch (Exception e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}