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

使用 Mockito 存根并執(zhí)行測試方法

Using Mockito to stub and execute methods for testing(使用 Mockito 存根并執(zhí)行測試方法)
本文介紹了使用 Mockito 存根并執(zhí)行測試方法的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我最近問了幾個(gè)面向 jUnit 和 Mockito 的問題,但我仍然很難掌握它的竅門.這些教程都是針對非常簡單的示例,所以我正在努力擴(kuò)大我的測試用例以適用于我的課程.

I've asked a couple of jUnit and Mockito oriented questions recently and I'm still really struggling to get the hang of it. The tutorials are all for very simple examples, so I'm struggling to scale up my test cases to work for my classes.

我目前正在嘗試為我在 web 應(yīng)用程序中的一個(gè)代理中使用的方法編寫一些測試用例.該方法與代理內(nèi)部的其他幾個(gè)方法交互以驗(yàn)證某些對象.我現(xiàn)在只想測試這個(gè)方法.

I'm currently trying to write some test cases for a method I have in one of my agents in a webapp. The method interacts with a couple of other methods inside the agent to validate some objects. I just want to test this one method right now.

這是我嘗試做的:

  1. 像這樣創(chuàng)建我的代理的 Mockito 對象:

  1. Create a Mockito object of my agent like so:

MyProcessingAgent mockMyAgent = Mockito.mock(MyProcessingAgent.class);

使用 Mockito.when 設(shè)置存根(希望是正確的術(shù)語),如下所示:

Setup stubs(hopefully the right term) using Mockito.when like so:

Mockito.when(mockMyAgent.otherMethod(Mockito.any(arg1)).thenReturn(requiredReturnArg);

嘗試像這樣執(zhí)行我的方法:

Try executing my method like so:

List myReturnValue = mockMyAgent.methodThatNeedsTestCase();

我期待 myReturnValue 中的內(nèi)容,但收到 0,所以我嘗試調(diào)試.當(dāng)我調(diào)用該方法時(shí),它永遠(yuǎn)不會執(zhí)行.我在方法的第一行有一個(gè)永遠(yuǎn)不會被觸及的調(diào)試點(diǎn).

I was expecting to things in myReturnValue, but received 0 instead so I tried to debug. When I call the method, it never executes. I have a debug point at the first line in the method that never gets touched.

如果我想在類的一個(gè)方法中執(zhí)行代碼,但強(qiáng)制類中的其他方法(嘗試與外部世界中的數(shù)據(jù)庫交互的方法)返回偽造的值.Mockito 可以做到這一點(diǎn)嗎?

If I want to execute the code in one method of a class, but force other methods in the class (one's that try to interact with databases in the outside world) to return faked out values. Is this possible with Mockito?

看來我目前的方法不是正確的測試風(fēng)格,但我不確定如何繼續(xù)前進(jìn).我可以模擬我的類并讓一個(gè)方法像平常一樣執(zhí)行,而其他方法被存根以返回我的給定值,這樣我就不必在測試這個(gè)方法期間處理數(shù)據(jù)訪問?

It appears that my current method of approach is not a correct testing style, but I'm not sure how to move forward. Can I mock my class and have one method be executed like normal while other methods are stubbed to return my given values so that I don't have to deal with data access during testing this one method?

推薦答案

您將 MockSpy 混淆了.

You are confusing a Mock with a Spy.

在模擬中,所有方法都被存根并返回智能返回類型".這意味著調(diào)用模擬類的任何方法什么都不做,除非你指定行為.

In a mock all methods are stubbed and return "smart return types". This means that calling any method on a mocked class will do nothing unless you specify behaviour.

在 spy 中,類的原始功能仍然存在,但您可以在 spy 中驗(yàn)證方法調(diào)用并覆蓋方法行為.

In a spy the original functionality of the class is still there but you can validate method invocations in a spy and also override method behaviour.

你想要的是

MyProcessingAgent mockMyAgent = Mockito.spy(MyProcessingAgent.class);

一個(gè)簡單的例子:

static class TestClass {

    public String getThing() {
        return "Thing";
    }

    public String getOtherThing() {
        return getThing();
    }
}

public static void main(String[] args) {
    final TestClass testClass = Mockito.spy(new TestClass());
    Mockito.when(testClass.getThing()).thenReturn("Some Other thing");
    System.out.println(testClass.getOtherThing());
}

輸出是:

Some Other thing

注意:您真的應(yīng)該嘗試模擬正在測試的類的依賴項(xiàng)而不是類本身.

NB: You should really try to mock the dependencies for the class being tested not the class itself.

這篇關(guān)于使用 Mockito 存根并執(zhí)行測試方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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)用驗(yàn)證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 日韩欧美国产成人一区二区 | 第四色影音先锋 | 成人欧美一区二区三区黑人孕妇 | 国产精品视频免费观看 | 国产999精品久久久 日本视频一区二区三区 | 手机看黄av免费网址 | 日韩中文字幕在线观看 | 天天干天天操天天爽 | 天天拍天天操 | 日韩精品一区二 | 国产一区不卡 | 天天草天天操 | av黄色国产| 午夜视频一区二区三区 | 国产精品久久久亚洲 | www亚洲精品| 国产a级黄色录像 | 午夜免费在线观看 | 国产精品一区久久久 | 午夜精品一区 | 久草视频观看 | 亚洲视频欧美视频 | 国产露脸国语对白在线 | 欧美综合国产精品久久丁香 | 91精品国产综合久久久久 | 最新中文字幕在线 | 成人免费视频7777777 | 精品欧美二区 | 中文字幕精品一区二区三区精品 | 97色在线视频 | 丝袜美腿一区二区三区 | 精品无码三级在线观看视频 | 国产成人麻豆免费观看 | 少妇黄色 | 精品久久久久久久久久久久 | 97超碰人人草 | 久久精品国产99国产精品 | 精品国产一区二区三区免费 | 亚洲天堂一区二区 | 国产精品自产拍在线观看蜜 | 久久亚洲综合 |