問題描述
我正在嘗試實現 Mockito 來測試特定方法,但 .thenReturn(...) 似乎總是返回一個空對象,而不是我想要的:
I'm trying to implement Mockito to test a particular method but the .thenReturn(...) seems to always be returning a null object instead of what I intended:
剪切:
public class TestClassFacade {
// injected via Spring
private InterfaceBP bpService;
public void setBpService(InterfaceBP bpService) {
this.bpService = bpService;
}
public TestVO getTestData(String testString) throws Exception {
BPRequestVO bpRequestVO = new BPRequestVO();
bpRequestVO.setGroupNumber(testString) ;
bpRequestVO.setProductType("ALL") ;
bpRequestVO.setProfileType("Required - TEST") ;
IBPServiceResponse serviceResponse = bpService.getProduct(bpRequestVO); //PROBLEM
if (serviceResponse.getMessage().equalsIgnoreCase("BOB")) {
throw new Exception();
} else {
TestVO testVO = new TestVO();
}
return testVO;
}
}
彈簧配置:
<bean id="testClass" class="com.foo.TestClassFacade">
<property name="bpService" ref="bpService" />
</bean>
<bean id="bpService" class="class.cloud.BPService" />
Mockito 測試方法:
@RunWith(MockitoJUnitRunner.class)
public class BaseTest {
@Mock BPService mockBPService;
@InjectMocks TestClassFacade mockTestClassFacade;
private String testString = null;
private BPRequestVO someBPRequestVO = new BPRequestVO();
private IBPServiceResponse invalidServiceResponse = new BPServiceResponse();
@Test (expected = Exception.class)
public void getBPData_bobStatusCode_shouldThrowException() throws Exception {
invalidServiceResponse.setMessage("BOB");
someBPRequestVO.setGroupNumber(null);
someBPRequestVO.setProductType("ALL");
someBPRequestVO.setProfileType("Required - TEST");
System.out.println("1: " + someBPRequestVO.getGroupNumber());
System.out.println("2: " + someBPRequestVO.getProductType());
System.out.println("3: " + someBPRequestVO.getProfileType());
System.out.println("4: " + someBPRequestVO.getEffectiveDate());
when(mockBPService.getProduct(someBPRequestVO)).thenReturn(invalidServiceResponse);
mockTestClassFacade.getTestData(testString);
verify(mockBPService).getProduct(someBPRequestVO);
}
}
系統輸出:
1: null
2: ALL
3: Required - TEST
4: null
這里發生的情況是,當我運行測試時,serviceResponse 對象在上面標有//PROBLEM 的 CUT 行上為 null.我的愿望是用我的測試方法中的invalidServiceResponse"對象填充該對象.從我的 System.out.println 的輸出來看,我的 bpRequestVO 似乎與我的 someBPRequestVO 在內容上相匹配.
What's happening here is that when I run the test the serviceResponse object is null on the line in the CUT marked with //PROBLEM above. My desire is to have that object be populated with my "invalidServiceResponse" object from my test method. Judging from the output of my System.out.println's it appears that my bpRequestVO matches my someBPRequestVO in content.
誰能告訴我我在這里缺少什么?
Could some one show me what I'm missing here?
感謝您的寶貴時間!
推薦答案
when()
中使用的 BPRequestVO 實例與 getTestData() 中使用的實例不同
代碼>.
除非您覆蓋 equals()
,否則它們將不匹配.
The instance of BPRequestVO that you use with when()
is different than the one used in getTestData()
.
Unless you override equals()
, they will not match.
如果你重寫 equals(),你應該不需要編寫自定義 Matcher.請注意 Mockito 文檔中的以下內容:
You should not need to write a custom Matcher if you override equals(). Note the following from the Mockito documentation:
自定義參數匹配器會使測試的可讀性降低.有時最好為傳遞給 mock 的參數實現 equals()(Mockito 自然使用 equals() 進行參數匹配).這可以使測試更清晰."
"Custom argument matchers can make the test less readable. Sometimes it's better to implement equals() for arguments that are passed to mocks (Mockito naturally uses equals() for argument matching). This can make the test cleaner."
這篇關于Mockito - thenReturn 總是返回空對象的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!