gpt4 book ai didi

java - 如何使用mockito测试匿名类中的方法调用

转载 作者:行者123 更新时间:2023-12-02 12:19:01 26 4
gpt4 key购买 nike

1.这是我的代码,我想使用 mockpowermockito 对其进行单元测试:

public class CarrierGroupDAO {
@Autowired
protected JdbcTemplate jdbcTemplate;

public CarrierGroupDAO() { }

public Map<Long, String> getAllCarrierGroups() {
String sql = "select t.carrier_group_id, t.updated_by from pv_carrier_group t where t.status = 'Q'";
return jdbcTemplate.execute(sql, new CallableStatementCallback<Map<Long, String>>() {
@Override
public Map<Long, String> doInCallableStatement(CallableStatement cs)
throws SQLException, DataAccessException {
ResultSet resultSet = cs.executeQuery();
Map<Long, String> carrierGroups = new HashMap<>();
while (resultSet.next()) {
carrierGroups.put(resultSet.getLong("carrier_group_id"), resultSet.getString("updated_by"));
}
return carrierGroups;
}
});
}
}

2.按照我的测试代码:

    @Test
public void testGetAllCarrierGroups() {
CarrierGroupDAO carrierGroupDAO=new CarrierGroupDAO();

CallableStatement cs=PowerMockito.mock(CallableStatement.class);
JdbcTemplate jdbcTemplate=PowerMockito.mock(JdbcTemplate.class);
PowerMockito.mock(JdbcTemplate.class);
PowerMockito.when(jdbcTemplate.execute(Mockito.any(String.class),Mockito.any(CallableStatementCallback.class))).thenAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] args=invocation.getArguments();
System.out.println(args.length);
TransactionCallback arg = (TransactionCallback)args[1];
return ((CallableStatementCallback) arg).doInCallableStatement(cs);
}
});

ResultSet resultSet =PowerMockito.mock(ResultSet.class);
try {
PowerMockito.when(cs.executeQuery()).thenReturn(resultSet);
PowerMockito.when(resultSet.next()).thenReturn(true).thenReturn(false);
PowerMockito.when(resultSet.getLong(Mockito.any(String.class))).thenReturn((long)1);
PowerMockito.when(resultSet.getString(Mockito.any(String.class))).thenReturn("haha");
carrierGroupDAO.getAllCarrierGroups();
Mockito.verify(cs).executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
}

错误信息:

    java.lang.NullPointerException
at CarrierGroupDAO.getAllCarrierGroups(CarrierGroupDAO.java:27)
at CarrierGroupDAOTest.testGetAllCarrierGroups(CarrierGroupDAOTest.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

如果您有任何想法解决这个问题,请询问我,深深感谢您!

最佳答案

这里有几个问题:

  1. 您正在模拟 JdbcTemplate,但您没有将此模拟实例注入(inject)到 CarrierGroupDAO 中(这会导致 NullPointerException)。
  2. 您正在使用 PowerMockito,其中 Mockito 就足够了。
  3. 您的 Answer 尝试将 jdbcTemplate.execute(...) 的 secopnd 参数转换为 TransactionCallback

可以通过以下方式轻松修复它们:

  • JdbcTemplate 注入(inject) CarrierGroupDAO

    public class CarrierGroupDAO {

    private JdbcTemplate jdbcTemplate;

    @Autowired
    public CarrierGroupDAO(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
    }

    public Map<Long, String> getAllCarrierGroups() {
    String sql = "select t.carrier_group_id, t.updated_by from pv_carrier_group t where t.status = 'Q'";
    return jdbcTemplate.execute(sql, new CallableStatementCallback<Map<Long, String>>() {
    @Override
    public Map<Long, String> doInCallableStatement(CallableStatement cs)
    throws SQLException, DataAccessException {
    ResultSet resultSet = cs.executeQuery();
    Map<Long, String> carrierGroups = new HashMap<>();
    while (resultSet.next()) {
    carrierGroups.put(resultSet.getLong("carrier_group_id"), resultSet.getString("updated_by"));
    }
    return carrierGroups;
    }
    });
    }
    }
  • 重新设计测试用例以使用 Mockito 并更改 Answer 实现:

    @Test
    public void testGetAllCarrierGroups() throws SQLException {
    JdbcTemplate jdbcTemplate = Mockito.mock(JdbcTemplate.class);
    CarrierGroupDAO carrierGroupDAO = new CarrierGroupDAO(jdbcTemplate);

    CallableStatement cs = Mockito.mock(CallableStatement.class);
    Mockito.when(jdbcTemplate.execute(Mockito.any(String.class), Mockito.any(CallableStatementCallback.class)))
    .thenAnswer(new Answer<Object>() {
    @Override
    public Object answer(InvocationOnMock invocation) throws Throwable {
    Object[] args = invocation.getArguments();
    System.out.println(args.length);
    CallableStatementCallback arg = (CallableStatementCallback) args[1];
    return arg.doInCallableStatement(cs);
    }
    });

    ResultSet resultSet = Mockito.mock(ResultSet.class);
    Mockito.when(cs.executeQuery()).thenReturn(resultSet);
    // expecting one entry in the resultset
    Mockito.when(resultSet.next()).thenReturn(true).thenReturn(false);
    long expectedKey = 1;
    String expectedValue = "haha";
    Mockito.when(resultSet.getLong(Mockito.any(String.class))).thenReturn(expectedKey);
    Mockito.when(resultSet.getString(Mockito.any(String.class))).thenReturn(expectedValue);

    Map<Long, String> allCarrierGroups = carrierGroupDAO.getAllCarrierGroups();
    Assert.assertEquals(1, allCarrierGroups.size());
    Assert.assertTrue(allCarrierGroups.containsKey(expectedKey));
    Assert.assertEquals(expectedValue, allCarrierGroups.get(expectedKey));
    }

关于java - 如何使用mockito测试匿名类中的方法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45932023/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com