亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

青春有我 2022-11-10 15:08:57
我想知道如何使用 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,而它應該返回列表。
查看完整描述

2 回答

?
墨色風雨

TA貢獻1853條經驗 獲得超6個贊

這應該有效:


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.<Object>any(),因為有多種queryForList方法實現,我們要模擬的方法接收一個可變參數。


查看完整回答
反對 回復 2022-11-10
?
HUX布斯

TA貢獻1876條經驗 獲得超6個贊

以下代碼我與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> 


查看完整回答
反對 回復 2022-11-10
  • 2 回答
  • 0 關注
  • 544 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號