模拟的jdbcTemplate没有返回所需的列表。 (另一个存根在基本的Integer类型上也可以正常工作。)我在thenReturn和thenAnswer中都尝试过,但没有帮助。 thenReturn和Answer都不起作用。
我必须对这2个jdbcTemplate查询进行存根:(
...
List<V_MONITORING_Record> vMonitoring_Record_List = new ArrayList<>();
...
try {
vMonitoring_Record_List = jdbcTemplate.query(SELECT_V_MONITORING,
namedParameters,
new BeanPropertyRowMapper<>(V_MONITORING_Record.class));
} catch (DataAccessException exception) {
}
try {
Integer count = jdbcTemplate.queryForObject(SELECT_MONTHLYTABLES.toString(), namedParameters, Integer.class);}
catch(...){}
@RunWith(MockitoJUnitRunner.class)
public class MonitoringTest {
@MockBean
NamedParameterJdbcTemplate jdbcTemplate = Mockito.mock(NamedParameterJdbcTemplate.class);
@InjectMocks
private MonitoringProcessorNode MonitoringProcessorNode;
@Mock
private EmailServiceImpl emailServiceImpl;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void test1(){
Answer<List<V_MONITORING_Record>> ans_V_MONITORING = inv -> {
List<V_MONITORING_Record> vMonitoring_Record_List = new ArrayList<>();
V_MONITORING_Record rec = new V_MONITORING_Record();
rec.setANZAHL(123);
...
vMonitoring_Record_List.add(rec);
return vMonitoring_Record_List;
};
Answer<Integer> ans_Integer = new Answer<Integer>(){
@Override
public Integer answer( InvocationOnMock inv)throws Throwable{
return 3;
}
};
MapSqlParameterSource namedParameters = new MapSqlParameterSource();
//This doesn't work
Mockito.when(jdbcTemplate.query("select * from V_MONITORING", namedParameters, new BeanPropertyRowMapper<>(V_MONITORING_ELINK_Record.class))).thenAnswer(ans_V_MONITORING);
//This works correctly
Mockito.when(jdbcTemplate.queryForObject(eq("select count(*) from TEST_TABLE_1"), (MapSqlParameterSource)any(), eq(Integer.class))).thenAnswer(ans_Integer);
}