【问题标题】:How to mock Spring's JdbcTemplate.queryForList using Mockito?如何使用 Mockito 模拟 Spring 的 JdbcTemplate.queryForList?
【发布时间】:2019-05-07 04:47:35
【问题描述】:

我想知道如何使用 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,而它应该返回列表。

【问题讨论】:

  • 你是否在被测对象中设置了 jdbcTemplate 模拟实例?请也显示这部分。

标签: java spring junit mockito


【解决方案1】:

这应该可行:

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 {

    @Test
    public void mockJdbcTemplate() {
        JdbcTemplate mockTemplate = Mockito.mock(JdbcTemplate.class);

        List<Map<String, Object>> mockResult = new ArrayList<>();

        Mockito.when(mockTemplate.queryForList(Mockito.anyString(), ArgumentMatchers.<Object>any())).thenReturn(mockResult);
        // Alternatively:
        // when(mockTemplate.queryForList(anyString(), Mockito.<Object>any())).thenReturn(mockResult);

        String query = "some query";
        Object[] params = new Object[]{1};

        List<Map<String, Object>> returnedResult = mockTemplate.queryForList(query, params);

        Assert.assertThat(returnedResult, CoreMatchers.sameInstance(mockResult));
    }

}

诀窍是使用ArgumentMatchers.&lt;Object&gt;any(),因为有多个queryForList 方法实现,而我们要模拟的那个接收一个可变参数。

【讨论】:

    【解决方案2】:

    下面的代码是我在 spring boot 中使用的,mockito

    /** class on which uni test is driven **/
    public class Decompile {
    
    
        @Autowired
        private JdbcTemplate jdbcTemplate;
    
        public List<Map<String, Object>> getRunner()
        {
            try{
                return jdbcTemplate.queryForList("select * from users");
            } 
            catch (Exception e) {
                System.err.println(e);
            }
            return null;
        }
    
    }
    

    单元测试用例开始

    /** 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 {
    
        @Mock/* works fine with autowired dependency too */
        JdbcTemplate jdbcTemplate;
    
        @InjectMocks/* for the claa that we are mocking */
        Decompile testclass;
    
        @Before
        public void setUp() throws Exception {
            MockitoAnnotations.initMocks(this);
        }
    
        @Test
        public void testgetRunner() {
            List<Map<String, Object>> expectedresultList  = new ArrayList<>();
            Mockito.lenient().when(jdbcTemplate.queryForList("select * from users.. ")).thenReturn(expectedresultList);
            List<Map<String, Object>> response = testclass.getRunner();
        }
    
    }
    

    mvn包使用如下(兼容spring版本> 2)

    <dependency> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-test</artifactId> 
        <scope>test</scope> 
    </dependency> 
    
    <dependency>
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-test</artifactId> 
        <scope>test</scope> 
    </dependency> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-08
      • 2012-04-05
      相关资源
      最近更新 更多