如何使用 Mockito 模拟 Spring 的 JdbcTemplate.query

我想知道如何使用 Mockito 模拟特定代码:


List<Map<String, Object>> list = jdbcTemplate.queryForList(

    sqlQuery, 

    new Object[] { inflowId }

);

我尝试了以下代码:


Mockito.doReturn(list)

       .when(jdbcTemplate)

       .queryForList(Mockito.anyString(), Mockito.any(Class.class));

和:


when(

    jdbcTemplate.queryForList(Mockito.anyString(), Mockito.any(Object[].class))

).thenReturn(list);

我的问题是特定方法在 JUnit 中没有被嘲笑。调用该方法时,它会返回null,而它应该返回列表。


青春有我
浏览 276回答 2
2回答

墨色风雨

这应该有效:import org.hamcrest.CoreMatchers;import org.junit.Assert;import org.junit.Test;import org.mockito.ArgumentMatchers;import org.mockito.Mockito;import org.springframework.jdbc.core.JdbcTemplate;import java.util.ArrayList;import java.util.List;import java.util.Map;public class DemoTest {&nbsp; &nbsp; @Test&nbsp; &nbsp; public void mockJdbcTemplate() {&nbsp; &nbsp; &nbsp; &nbsp; JdbcTemplate mockTemplate = Mockito.mock(JdbcTemplate.class);&nbsp; &nbsp; &nbsp; &nbsp; List<Map<String, Object>> mockResult = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; Mockito.when(mockTemplate.queryForList(Mockito.anyString(), ArgumentMatchers.<Object>any())).thenReturn(mockResult);&nbsp; &nbsp; &nbsp; &nbsp; // Alternatively:&nbsp; &nbsp; &nbsp; &nbsp; // when(mockTemplate.queryForList(anyString(), Mockito.<Object>any())).thenReturn(mockResult);&nbsp; &nbsp; &nbsp; &nbsp; String query = "some query";&nbsp; &nbsp; &nbsp; &nbsp; Object[] params = new Object[]{1};&nbsp; &nbsp; &nbsp; &nbsp; List<Map<String, Object>> returnedResult = mockTemplate.queryForList(query, params);&nbsp; &nbsp; &nbsp; &nbsp; Assert.assertThat(returnedResult, CoreMatchers.sameInstance(mockResult));&nbsp; &nbsp; }}诀窍是使用ArgumentMatchers.<Object>any(),因为有多种queryForList方法实现,我们要模拟的方法接收一个可变参数。

HUX布斯

以下代码我与spring boot一起使用,mockito/** class on which uni test is driven **/public class Decompile {&nbsp; &nbsp; @Autowired&nbsp; &nbsp; private JdbcTemplate jdbcTemplate;&nbsp; &nbsp; public List<Map<String, Object>> getRunner()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return jdbcTemplate.queryForList("select * from users");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.err.println(e);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }}单元测试用例开始/** Unit test case for above class **/import static org.junit.jupiter.api.Assertions.assertEquals;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.mockito.InjectMocks;import org.mockito.Mock;import org.mockito.Mockito;import org.mockito.MockitoAnnotations;import org.mockito.junit.MockitoJUnitRunner;import org.springframework.jdbc.core.JdbcTemplate;@RunWith(MockitoJUnitRunner.class)public class DecompileTest {&nbsp; &nbsp; @Mock/* works fine with autowired dependency too */&nbsp; &nbsp; JdbcTemplate jdbcTemplate;&nbsp; &nbsp; @InjectMocks/* for the claa that we are mocking */&nbsp; &nbsp; Decompile testclass;&nbsp; &nbsp; @Before&nbsp; &nbsp; public void setUp() throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; MockitoAnnotations.initMocks(this);&nbsp; &nbsp; }&nbsp; &nbsp; @Test&nbsp; &nbsp; public void testgetRunner() {&nbsp; &nbsp; &nbsp; &nbsp; List<Map<String, Object>> expectedresultList&nbsp; = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; Mockito.lenient().when(jdbcTemplate.queryForList("select * from users.. ")).thenReturn(expectedresultList);&nbsp; &nbsp; &nbsp; &nbsp; List<Map<String, Object>> response = testclass.getRunner();&nbsp; &nbsp; }}mvn 包使用如下(兼容 spring 版本> 2)<dependency>&nbsp;&nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp;&nbsp; &nbsp; <artifactId>spring-boot-test</artifactId>&nbsp;&nbsp; &nbsp; <scope>test</scope>&nbsp;</dependency>&nbsp;<dependency>&nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp;&nbsp; &nbsp; <artifactId>spring-boot-starter-test</artifactId>&nbsp;&nbsp; &nbsp; <scope>test</scope>&nbsp;</dependency>&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java