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

使用 Mockito 存根并執行測試方法

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

問題描述

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

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 應用程序中的一個代理中使用的方法編寫一些測試用例.該方法與代理內部的其他幾個方法交互以驗證某些對象.我現在只想測試這個方法.

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. 像這樣創建我的代理的 Mockito 對象:

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

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

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

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

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

嘗試像這樣執行我的方法:

Try executing my method like so:

List myReturnValue = mockMyAgent.methodThatNeedsTestCase();

我期待 myReturnValue 中的內容,但收到 0,所以我嘗試調試.當我調用該方法時,它永遠不會執行.我在方法的第一行有一個永遠不會被觸及的調試點.

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.

如果我想在類的一個方法中執行代碼,但強制類中的其他方法(嘗試與外部世界中的數據庫交互的方法)返回偽造的值.Mockito 可以做到這一點嗎?

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?

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

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.

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

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 中驗證方法調用并覆蓋方法行為.

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);

一個簡單的例子:

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

注意:您真的應該嘗試模擬正在測試的類的依賴項而不是類本身.

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

這篇關于使用 Mockito 存根并執行測試方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How to mock super reference (on super class)?(如何模擬超級參考(在超級類上)?)
Java mock database connection(Java 模擬數據庫連接)
Mockito ClassCastException - A mock cannot be cast(Mockito ClassCastException - 無法投射模擬)
Set value to mocked object but get null(將值設置為模擬對象但獲取 null)
How to mock DriverManager.getConnection(...)?(如何模擬 DriverManager.getConnection(...)?)
Mockito; verify method was called with list, ignore order of elements in list(模擬;使用列表調用驗證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 草草影院ccyy | 欧美八区 | 国产亚洲区 | 91免费看片神器 | 国产精品成人av | 91在线看网站 | 日韩一级免费电影 | 人人爽人人草 | 99影视| av在线免费观看网址 | 日本三级全黄三级a | 国产精品久久久久久久久久久久冷 | 综合一区 | 国产精品久久久久久久久久免费看 | 欧产日产国产精品国产 | 久久久久亚洲精品 | 91一区二区三区在线观看 | 2018天天干天天操 | 日本午夜一区二区三区 | 黄网站免费观看 | 欧美一级一区 | 中文字幕国产一区 | 欧美成人一级视频 | 亚洲品质自拍视频 | 中文二区 | 国产精品视频在线免费观看 | 日韩欧美在线免费观看 | 男女视频在线观看 | 81精品国产乱码久久久久久 | 成人免费视频网站在线看 | 黄a在线观看 | 国产美女高潮 | 久久久999国产精品 中文字幕在线精品 | 欧美一区二区黄 | 国产成人精品一区二区三区视频 | 国产日产精品一区二区三区四区 | 爱爱免费视频 | 久久精品在线播放 | 国产精品久久久久久久久久久免费看 | 天天射影院 | 国产综合av |