Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用EasyMock对java类扩展存储过程进行单元测试_Java_Easymock_Java Stored Procedures - Fatal编程技术网

使用EasyMock对java类扩展存储过程进行单元测试

使用EasyMock对java类扩展存储过程进行单元测试,java,easymock,java-stored-procedures,Java,Easymock,Java Stored Procedures,当我尝试对以下扩展StoredProcess的类进行单元测试时,我在第行得到一个NullPointerException:return(Map)execute(csc,new CallableStatementCallback()inJDBCTemplate类。我模拟了在execute方法、DataSource和sql中传递的bean public class MyStoredProc extends StoredProcedure { /** * Constructor -

当我尝试对以下扩展StoredProcess的类进行单元测试时,我在第行得到一个NullPointerException:
return(Map)execute(csc,new CallableStatementCallback()
in
JDBCTemplate
类。我模拟了在
execute
方法、
DataSource
和sql中传递的bean

public class MyStoredProc extends StoredProcedure {
    /**
     * Constructor - sets SQLParameters for the stored procedure.
     * 
     * @param ds - DataSource
     */
    public MyStoredProc(DataSource dataSource, String sql) {
        super(dataSource, sql);
        declareParameter(new SqlOutParameter("return",Types.NUMERIC));
        declareParameter(new SqlParameter("BATCH_ID",Types.NUMERIC));
        declareParameter(new SqlParameter("PROCESS_TYPE",Types.VARCHAR));

        complie(); 
    }

    public BigDecimal execute(MyBean bean){
        BigDecimal returnValue = BigDecimal.valueOf(-1);

        Map in = new HashMap();

        in.put("BATCH_ID", bean.getBatchID());
        in.put("PROCESS_TYPE", bean.getProcessType());

        Object obj = execute(in);
        if (obj != null) {
            Object output = ((HashMap) obj).get("return"); 

            if( output instanceof BigDecimal) {
                returnValue = (BigDecimal)output;
            }
        }
        return bigDec; 
    }
}
测试用例:p.S-当我调试这个测试用例时,StoredProcedure mock根本没有被使用,而是使用了实际的实现

public class MyStoredProcTest {
private MyStoredProc mysp;
private DataSource dataSource;
private String sql;
@Before
public void setUp() {
    dataSource = EasyMock.createMock(DataSource.class);
    sql = "Testing";
    mysp = new MyStoredProc(dataSource, sql);
}

@Test
public void testExecute() {

    StoredProcedure storedProcedure = EasyMock
            .createMock(StoredProcedure.class);
    HashMap map = new HashMap();
    map.put("return", BigDecimal.ONE);
    expect(storedProcedure.execute(EasyMock.anyObject(Map.class))).andReturn(map);

    Connection con = EasyMock.createMock(Connection.class);
    expect(dataSource.getConnection()).andReturn(con);   
    MyBean bean = EasyMock.createMock(MyBean.class);


    expect(bean.getBatchID()).andReturn(BigDecimal.valueOf(.0001))
            .anyTimes();
    expect(bean.getProcessType()).andReturn("Process Type").anyTimes();

    replay(bean, dataSource, storedProcedure, con);
    BigDecimal returnValue = null;
    try {
        returnValue = mysp.execute(bean);
    } catch (Exception e) {
        System.out.println("exception" + e.getStackTrace());//  the Null pointer from JDBCTemplate is caught here.
    }
    Assert.assertEquals(BigDecimal.valueOf(-1), returnValue);
}

您的一些模拟没有被使用,因为您没有重放它们。您应该将
replay(bean)
修改为
replay(bean、datasource、storedProcedure)


另一方面,
map
不需要模拟。当您期望调用
storedProcedure.execute(…)
时,您可以返回预填充的
map

因为您没有重放它们,所以您的一些模拟没有被使用。您应该将
重放(bean)
修改为
重放(bean、数据源、StoredProcess)


另一方面,
map
不需要模拟。当您期望调用
storedProcedure.execute(…)
您可以返回预填充的
映射

谢谢jradakov…我仍然收到相同的错误。我添加了replay,创建了一个映射实例,而不是按照您的建议模拟它,并添加了连接模拟(因为它给了我一个意外的getConnection调用)。我忘记添加“con”若要重播,请执行此操作并立即获取不同的错误…将尝试解决此问题并在需要帮助时发布Hanks jradakov…我仍然收到相同的错误。我添加了重播,创建了map实例,而不是根据您的建议对其进行模拟,并添加了连接模拟(因为它给了我对getConnection的意外调用),我忘记添加了“con”重播…这样做了,现在得到了一个不同的错误…将尝试解决它,并张贴,如果需要任何帮助