Spring 如何模拟jdbctemplate.query()方法?
我使用上述代码行模拟jdbcTemplate,但它不起作用。有人可以帮助您模拟jdbcTemplate。尝试这样做: 在测试类中使用:Spring 如何模拟jdbctemplate.query()方法?,spring,spring-boot,Spring,Spring Boot,我使用上述代码行模拟jdbcTemplate,但它不起作用。有人可以帮助您模拟jdbcTemplate。尝试这样做: 在测试类中使用: Mokito.when(jdbcTemplate.query(sql, new ParticipantMapper())).thenReturn(participantExistingList); 然后尝试: @Mock JdbcTemplate jdbcTemplate; 如果仍然失败,请尝试: Mokito.when(jdbcTemp
Mokito.when(jdbcTemplate.query(sql, new ParticipantMapper())).thenReturn(participantExistingList);
然后尝试:
@Mock
JdbcTemplate jdbcTemplate;
如果仍然失败,请尝试:
Mokito.when(jdbcTemplate.query(sql, new ParticipantMapper())).thenReturn(participantExistingList);
希望这有帮助尝试这样做:
在测试类中使用:
Mokito.when(jdbcTemplate.query(sql, new ParticipantMapper())).thenReturn(participantExistingList);
然后尝试:
@Mock
JdbcTemplate jdbcTemplate;
如果仍然失败,请尝试:
Mokito.when(jdbcTemplate.query(sql, new ParticipantMapper())).thenReturn(participantExistingList);
希望这有助于尝试对所有参数使用ArgumentMatchers,如下所示:
doReturn(participantExistingList).when(jdbcTemplate).query(sql, new ParticipantMapper());
根据您希望集中交互的意愿,您可以使用例如eq()作为sql字符串。请参阅以获取JavaDoc。尝试对所有参数使用ArgumentMatchers,如下所示:
doReturn(participantExistingList).when(jdbcTemplate).query(sql, new ParticipantMapper());
根据您希望集中交互的意愿,您可以使用例如eq()作为sql字符串。查看JavaDoc。查看query()方法中参数的顺序很重要。 在我的例子中,我想嘲笑以下几行:
Mokito.when(jdbcTemplate.query(any(String.class), any(ParticipantMapper.class)).thenReturn(participantExistingList);
查看query()方法中参数的顺序很重要。 在我的例子中,我想嘲笑以下几行:
Mokito.when(jdbcTemplate.query(any(String.class), any(ParticipantMapper.class)).thenReturn(participantExistingList);
使用
Mockito.any(ParticipantMapper.class)
而不是new ParticipantMapper()
org.Mockito.exceptions.misusing.invalidUseofMatchers异常:参数匹配器的使用无效!需要2个匹配器,1个已记录:在添加上述代码后获取此错误。请使用Mockito.any(ParticipantMapper.class)
而不是new ParticipantMapper()
org.Mockito.exceptions.misusing.InvalidUseOfMatchersException:参数匹配器的使用无效!需要2个匹配器,1个已记录:在添加上述代码后获取此错误。