Rit*_*esh 4 java junit junit4 mockito
我使用mockito作为模拟框架.我在这里有一个scenerio,我的时间(abc.method()).thenReturn(value)不返回值,而是返回null.
这是我的课和测试的样子.
public class foo(){
public boolean method(String userName) throws Exception {
ClassA request = new ClassA();
request.setAbc(userName);
ClassB response = new ClassB();
try {
response = stub.callingmethod(request);
} catch (Exception e) {
}
boolean returnVal = response.isXXX();
return returnVal;
}
Run Code Online (Sandbox Code Playgroud)
现在接下来是测试
@Test
public void testmethod() throws Exception{
//arrange
String userName = "UserName";
ClassA request = new ClassA();
ClassB response = new ClassB();
response.setXXX(true);
when(stub.callingmethod(request)).thenReturn(response);
//act
boolean result = fooinstance.lockLogin(userName);
//assert
assertTrue(result);
}
Run Code Online (Sandbox Code Playgroud)
stub使用mockito进行模拟,即使用@Mock.测试在类foo中抛出NullPointerException,接近boolean retrunVal = response.isXXX();
stub.callingmethod(request).thenReturn(response)的参数匹配器正在比较引用相等性.你想要一个更宽松的匹配器,我认为这样:
stub.callingmethod(isA(ClassA.class)).thenReturn(response);
Run Code Online (Sandbox Code Playgroud)