問題描述
我正在使用以下內容.
Powermock-mockito 1.5.12
Mockito 1.95
junit 4.11
這是我的實用程序類
public void InternalUtils {
public static void sendEmail(String from, String[] to, String msg, String body) {
}
}
這里是被測類的要點:
public class InternalService {
public void processOrder(Order order) {
if (order.isSuccessful()) {
InternalUtils.sendEmail(...);
}
}
}
這是測試:
@PrepareForTest({InternalUtils.class})
@RunWith(PowerMockRunner.class)
public class InternalService {
public void verifyEmailSend() {
mockStatic(Internalutils.class);
doNothing().when(InternalUtils, "sendEmail", anyString(), any(String.class), anyString(), anyString());
Order order = mock(Order.class);
when(order.isSuccessful()).thenReturn(true);
InternalService is = new InternalService();
verifyStatic(times(1));
is.processOrder(order);
}
}
上述測試失敗.給定的驗證方式是none,但是根據code,如果下單成功則必須發送email.
The above test fails. The verification mode given is none, but according to the code, if order is successful than email must be send.
推薦答案
如果你在模擬行為(使用 doNothing()
之類的東西),真的不需要調用 驗證*()
.也就是說,這是我重寫測試方法的嘗試:
If you are mocking the behavior (with something like doNothing()
) there should really be no need to call to verify*()
. That said, here's my stab at re-writing your test method:
@PrepareForTest({InternalUtils.class})
@RunWith(PowerMockRunner.class)
public class InternalServiceTest { //Note the renaming of the test class.
public void testProcessOrder() {
//Variables
InternalService is = new InternalService();
Order order = mock(Order.class);
//Mock Behavior
when(order.isSuccessful()).thenReturn(true);
mockStatic(Internalutils.class);
doNothing().when(InternalUtils.class); //This is the preferred way
//to mock static void methods.
InternalUtils.sendEmail(anyString(), anyString(), anyString(), anyString());
//Execute
is.processOrder(order);
//Verify
verifyStatic(InternalUtils.class); //Similar to how you mock static methods
//this is how you verify them.
InternalUtils.sendEmail(anyString(), anyString(), anyString(), anyString());
}
}
為了更好地突出正在發生的事情,我將其分為四個部分:
I grouped into four sections to better highlight what is going on:
我選擇在這里聲明任何實例變量/方法參數/模擬協作者.如果它在多個測試中使用,請考慮將其作為測試類的實例變量.
I choose to declare any instance variables / method arguments / mock collaborators here. If it is something used in multiple tests, consider making it an instance variable of the test class.
這是您定義所有模擬行為的地方.在執行被測代碼之前,您在此處設置返回值和期望值.一般來說,如果您在此處設置模擬行為,則以后無需驗證該行為.
This is where you define the behavior of all of your mocks. You're setting up return values and expectations here, prior to executing the code under test. Generally speaking, if you set the mock behavior here you wouldn't need to verify the behavior later.
這里沒有什么花哨的;這只是啟動正在測試的代碼.我喜歡給它一個單獨的部分來引起人們的注意.
Nothing fancy here; this just kicks off the code being tested. I like to give it its own section to call attention to it.
這是當您調用任何以 verify
或 assert
開頭的方法時.測試結束后,您檢查您希望發生的事情是否確實發生了.這是我在您的測試方法中看到的最大錯誤;你試圖在它有機會運行之前驗證方法調用.其次是您從未指定哪個要驗證的靜態方法.
This is when you call any method starting with verify
or assert
. After the test is over, you check that the things you wanted to have happen actually did happen. That is the biggest mistake I see with your test method; you attempted to verify the method call before it was ever given a chance to run. Second to that is you never specified which static method you wanted to verify.
這主要是我個人的喜好.您需要按照一定的順序做事,但在每個分組中都有一點回旋余地.這有助于我快速區分出發生了什么.
This is mostly personal preference on my part. There is a certain order you need to do things in but within each grouping there is a little wiggle room. This helps me quickly separate out what is happening where.
我還強烈建議您瀏覽以下網站上的示例,因為它們非常強大,可以幫助您處理您需要的大多數案例:
I also highly recommend going through the examples at the following sites as they are very robust and can help with the majority of the cases you'll need:
- https://github.com/powermock/powermock/wiki/Mockito (PowerMock 概述/示例)
- http://site.mockito.org/mockito/docs/current/org/mockito/Mockito.html(Mockito 概述/示例)
- https://github.com/powermock/powermock/wiki/Mockito (PowerMock Overview / Examples)
- http://site.mockito.org/mockito/docs/current/org/mockito/Mockito.html (Mockito Overview / Examples)
這篇關于如何驗證靜態 void 方法是否已使用 power mockito 調用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!