在我们的spring Boot 应用程序中,我试图为批量插入操作方法编写mockito测试用例,但它不工作。
低于错误
java.lang.NullPointerException Suppressed.org.mockito.exceptions.misusing. InvalidPointerOfMatchersException:此处检测到错误放置或误用的参数匹配器。
我的代码如下
@Repository
public class AuditDao {
@Value("${query}")
private String queryForBatchInsert;
@Value("${batchSize}")
private int batchSize;
@Autowired
private JdbcTemplate jdbcTemplate;
public int[][] batchInsert(List<Audit> auditList){
int[][] updateCounts = jdbcTemplate.batchUpdate(
queryForBatchInsert, auditList, batchSize, new ParameterizedPreparedStatementSetter<Audit>() {
public void setValues(PreparedStatement ps, Audit audit) throws SQLException {
ps.setLong(1, audit.getId);
ps.setString(2, audit.getSourceType);
ps.setString(3, audit.getJson);
ps.setTimestamp(4, Timestamp.valueOf(LocalDateTime.now(ZoneId.of("UTC"))));
}
});
return updateCounts;
}
}
字符串
测试用例如下
@Test
public void testBatchInsert(){
List<Audit> auditList = new ArrayList<Audit>();
Audit audit = Mockito.mock(Audit.class);
auditList.add(audit);
when(jdbcTemplate.batchUpdate(ArgumentMatchers.<String>any(), any(), any(), any(ParameterizedPreparedStatementSetter.class))).thenReturn(new int[1][1]);
assertEquals(new int[1][1], auditDao.batchInsert(auditList));
}
型
1条答案
按热度按时间9ceoxa921#
乍一看,你会发现,
字符串
期望int值作为batchSize,但是你把表示Object的matcher any()放在这一行:
型
尝试将其更改为:
型
但是,我看到您将batchSize用作String,并且没有使用任何隐式转换。
型
无论如何,您的问题与参数匹配器有关,具体来说,问题出现在JdbcTemplate中的batchUpdate方法的预期参数类型与Mockito when().thenReturn逻辑中使用的参数匹配器之间的不匹配。