Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

java - How to mock NamedParameterJdbcTemplate query with ResultSet lambda in unit test using Mockito and Junit5 in Spring Boot

In Junit4, I would do something like this:

jdbcTemplate.query(sql, new MapSqlParameterSource("id", id),new MyMapper());
->
Mockito.when(jdbcTemplate.query(any(), Mockito.any(MyMapper.class)))
                .thenReturn(mockQueryResult);

however, I am now using Java 11 with Junit5 like so:

            List<String> things =  jdbcTemplate.query(SELECT_QUERY, Map.of("thing", thing), rs -> {
                List<String> thingsList= new ArrayList<>();
                while(rs.next()) {
                    thingsList.add(rs.getString("THING"));
                }
                return thingsList;
            });

and I tried testing this with:

@ExtendWith(MockitoExtension.class)
public class MyDaoTest {

    @Mock
    NamedParameterJdbcTemplate jdbcTemplate;

    @InjectMocks
    private MyDao myDao;

    @Test
    public void test_getThings_valid() {
        Mockito.when(jdbcTemplate.query(
                ArgumentMatchers.eq(SELECT_QUERY),
                ArgumentMatchers.eq(Map.of("thing", "thing")),
                ArgumentMatchers.any(ResultSetExtractor.class)))
                    .thenAnswer((invocation) -> {
                        ResultSetExtractor<List<String>> resultSetExtractor =
                                invocation.getArgument(0);
                        ResultSet rs = Mockito.mock(ResultSet.class);
                        when(rs.next()).thenReturn( true, false);
                        Mockito.when(rs.getString(ArgumentMatchers.eq("THING")))
                                .thenReturn("thing");
                        return resultSetExtractor.extractData(rs);
                    });
        boolean result = myDao.getThings("thing");
        assertTrue(result);
    }

I was using this as a reference, but now I am a bit lost on how the invocation works in this. (as well as getting an exception)

class java.util.ImmutableCollections$Map1 cannot be cast to class org.springframework.jdbc.core.ResultSetExtractor (java.util.ImmutableCollections$Map1 is in module java.base of loader 'bootstrap'; org.springframework.jdbc.core.ResultSetExtractor is in unnamed module of loader 'app')
java.lang.ClassCastException: class java.util.ImmutableCollections$Map1 cannot be cast to class org.springframework.jdbc.core.ResultSetExtractor (java.util.ImmutableCollections$Map1 is in module java.base of loader 'bootstrap'; org.springframework.jdbc.core.ResultSetExtractor is in unnamed module of loader 'app')

question from:https://stackoverflow.com/questions/65893238/how-to-mock-namedparameterjdbctemplate-query-with-resultset-lambda-in-unit-test

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...