問題描述
我最近問了幾個面向 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.
這是我嘗試做的:
像這樣創建我的代理的 Mockito 對象:
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?
推薦答案
您將 Mock
與 Spy
混淆了.
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模板網!