I've a code where I pass a lambda as a RowMapper to a framework class, which use this lambda to trigger a org.springframework.jdbc.core.JdbcTemplate.query(...) method.
As this is Lambda and not a concrete class, I'm unable to find the correct ArgumentMatchers for mocking the behavior for my test.
Below is some oversimplified code for illustration
public abstract class FrameworkDao{ protected <T> List<T> query( MyStatement sql, RowMapper<T> handler) { //A lot of security and logging handling JdbcTemplate template = this.getTemplate(); List<T> result = template.query(sql.getSql(), sql.getParameterArray(), handler); //A lot of security exception and logging handling return result; } } class MyDao extends FrameworkDao { public List<Model> getModels() { return query(new MyStatement(selectQuery,pram1,param2), (rs, num) -> ModelConverter.convert(rs) ); } } class ModelConverter { Model convert (ResultSet rs){....}; Model convert (SomeOtherModel sm){....}; Model convert (SomeOtherModel2 sm2){....}; } Now because ModelConverter is a one converter class for converting many things into Model class, it is not a dedicated RowMapper class to be used as a Mockito ArgumentMatcher. I used method reference for one of the method in it as a RowMapper.
So my Junit Mocking is as follows
@Test void testSomething(){ List<Models> models = new ArrayList(); //create test models & add with models.add() doReturn(models) .when(jdbcTemplate) .query(any(String.class), any(Object[].class), (RowMapper<Object>) any(FunctionalInterface.class)); ............ } Last line above test is problematic.
Because I'm passing a lambda here I'm unable to get the expected result out of Mocked jdbcTemplate. At last line instead of FunctionalInterface I tried below also, but nothing works.
(RowMapper<Object>) any(Object.class)(RowMapper<Object>) any()any(RowMapper.class)
Unfortunately I can neither change the FrameworkDao nor the converter class.
So question is How to mock a Method with a lambda as a parameter
Mockito.<RowMapper<Models>>any()?