android 接口单元测试,使用Retrofit和Mockito进行Android单元测试
我从活动代码中分离了改进的api调用方法,我想对这些方法进行单元测试,例如:接口:public interface LoginService {@GET("/auth")public void basicLogin(Callback response);}这是执行调用的方法,在主活动中,我通过事件总线获取对象.public class AuthAPI {private Bus bus;LoginS
我从活动代码中分离了改进的api调用方法,我想对这些方法进行单元测试,例如:接口:
public interface LoginService {
@GET("/auth")
public void basicLogin(Callback response);
}
这是执行调用的方法,在主活动中,我通过事件总线获取对象.
public class AuthAPI {
private Bus bus;
LoginService loginService;
public AuthAPI(String username, String password) {
this.bus = BusProvider.getInstance().getBus();
loginService = ServiceGenerator.createService(LoginService.class,
CommonUtils.BASE_URL,
username,
password);
}
public void Login() {
loginService.basicLogin(new Callback() {
@Override
public void success(AuthObject authObject, Response response) {
bus.post(authObject);
}
@Override
public void failure(RetrofitError error) {
AuthObject authObject = new AuthObject();
authObject.setError(true);
bus.post(authObject);
}
});
}
}
在这里测试
@RunWith(MockitoJUnitRunner.class)
public class AuthCallTest extends TestCase {
AuthAPI authAPI;
@Mock
private LoginService mockApi;
@Captor
private ArgumentCaptor> cb;
@Before
public void setUp() throws Exception {
authAPI = new AuthAPI("username", "password");
MockitoAnnotations.initMocks(this);
}
@Test
public void testLogin() throws Exception {
Mockito.verify(mockApi).basicLogin((cb.capture()));
AuthObject authObject = new AuthObject();
cb.getValue().success(authObject, null);
assertEquals(authObject.isError(), false);
}
}
当我启动测试时,我有这个错误
Wanted but not invoked:
mockApi.basicLogin();
-> at AuthCallTest.testLogin(AuthCallTest.java:42)
Actually, there were zero interactions with this mock.
谁来帮帮我 :(
更多推荐
所有评论(0)