久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 Mockito 模擬方法的局部變量

Using Mockito to mock a local variable of a method(使用 Mockito 模擬方法的局部變量)
本文介紹了使用 Mockito 模擬方法的局部變量的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我有一個需要測試的類A.以下是A的定義:

I have a class A that needs to the tested. The following is the definition of A:

public class A {
    public void methodOne(int argument) {
        //some operations
        methodTwo(int argument);
        //some operations
    }

    private void methodTwo(int argument) {
        DateTime dateTime = new DateTime();
        //use dateTime to perform some operations
    }
}

并且基于 dateTime 值,一些數(shù)據(jù)將被操作,從數(shù)據(jù)庫中檢索.對于此數(shù)據(jù)庫,這些值通過 JSON 文件進行持久化.

And based on the dateTime value some data is to be manipulated, retrieved from the database. For this database, the values are persisted via a JSON file.

這使事情變得復(fù)雜.我需要的是在測試時將 dateTime 設(shè)置為某個特定日期.有沒有辦法可以使用 mockito 模擬局部變量的值?

This complicates things. What I need is to set the dateTime to some specific date while it is being tested. Is there a way I can mock a local variable's value using mockito?

推薦答案

你不能模擬一個局部變量.但是,您可以做的是將其創(chuàng)建提取到 protected 方法并 spy 它:

You cannot mock a local variable. What you could do, however, is extract its creation to a protected method and spy it:

public class A {
  public void methodOne(int argument) {
    //some operations
    methodTwo(int argument);
    //some operations
  }

  private void methodTwo(int argument) {
    DateTime dateTime = createDateTime();
    //use dateTime to perform some operations
  }

  protected DateTime createDateTime() {
    return new DateTime();
  }
}

public class ATest {
  @Test
  public void testMethodOne() {
    DateTime dt = new DateTime (/* some known parameters... */);
    A a = Mockito.spy(new A());
    doReturn(dt).when(a).createDateTime();
    int arg = 0; // Or some meaningful value...
    a.methodOne(arg);
    // assert the result
}

這篇關(guān)于使用 Mockito 模擬方法的局部變量的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to mock super reference (on super class)?(如何模擬超級參考(在超級類上)?)
Java mock database connection(Java 模擬數(shù)據(jù)庫連接)
Mockito ClassCastException - A mock cannot be cast(Mockito ClassCastException - 無法投射模擬)
Set value to mocked object but get null(將值設(shè)置為模擬對象但獲取 null)
How to mock DriverManager.getConnection(...)?(如何模擬 DriverManager.getConnection(...)?)
Mockito; verify method was called with list, ignore order of elements in list(模擬;使用列表調(diào)用驗證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 99re热精品视频 | 碰碰视频 | 色婷婷久久久亚洲一区二区三区 | 亚洲日本欧美日韩高观看 | 亚洲精品电影网在线观看 | 国产精品久久久久久久久久久免费看 | 综合国产| 在线观看亚洲专区 | 久久精品色欧美aⅴ一区二区 | 欧美一区二区三区,视频 | 中文字字幕一区二区三区四区五区 | 亚洲精品乱码久久久久久按摩观 | 成人亚洲精品 | 成人精品一区二区三区中文字幕 | 成人影院在线视频 | 国产一区在线看 | 午夜视频一区二区三区 | 午夜视频在线 | 日日干干 | 国产午夜精品一区二区三区嫩草 | 中文字幕精品一区二区三区精品 | 久久国产一区二区 | 先锋资源网站 | 香蕉久久a毛片 | 成人片免费看 | 国产露脸国语对白在线 | 羞羞色在线观看 | 亚洲午夜在线 | 91精品国产一二三 | 91精品国模一区二区三区 | 一区二区三区精品视频 | 国产精品免费一区二区三区四区 | 久久久精品一区 | 国产亚洲第一页 | 日韩欧美国产精品 | 成人免费一区二区三区视频网站 | 国产精品久久久久久久久久久免费看 | 91免费视频观看 | 成人1区 | 日韩免费视频 | 毛片99|