问题描述
我正在尝试实现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
这里发生的是,当我运行测试时,CUT中标有//PROBLEM的行上的serviceResponse对象为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:
这篇关于Mockito-thenReturn总是返回null对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!