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