类中私有方法的测试

本文介绍了一种使用反射机制测试私有方法的方法。通过获取私有方法的引用并调用它,可以验证其正确性。这种方法适用于JUnit等测试框架。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在开发项目 时常会编写方法的测试,如果为公共方法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;
    }
}

 

测试类如下:

利用类的反射机制,得到类中的方法。

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();
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值