0

Mocked jdbcTemplate doesn't return the desired List. ( Another stub works correctly with a basic Integer type.) I tried both with thenReturn and thenAnswer but it didn't helped. Neither thenReturn nor thenAnswer worked.

I have to stub these 2 jdbcTemplate queries: (

...
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);

}


Tomtom
  • 45
  • 7
  • It's desired behavior of Mockito, while stubbing any method call, either use each parameter as exact or use each parameter as generic, but if you want to use mix in that case eq will fix the issue. Or you can also use `Mockito.anyString()` instead of `"select * from V_MONITORING"`. Please have a look in this post [link](https://stackoverflow.com/questions/26318569/unfinished-stubbing-detected-in-mockito) – Another coder Oct 01 '19 at 10:56
  • I modified the code and now all parameters (I think) are exact but the result is the same. (Query returns with a 0 length result.) – Tomtom Oct 01 '19 at 14:12
  • With the following modifications it works. Mockito.when(jdbcTemplate.query(anyString(), (SqlParameterSource) any(),(BeanPropertyRowMapper) any())).thenAnswer(ans_V_MONITORING); Thank you for your help. – Tomtom Oct 01 '19 at 14:45

0 Answers0