gpt4 book ai didi

java - 安卓交互器测试

转载 作者:行者123 更新时间:2023-11-29 08:36:47 26 4
gpt4 key购买 nike

我有以下类(class),我正试图弄清楚如何为其编写单元测试:

@EBean
public class LoginInteractorImpl implements LoginInteractor {

private final static String LOGIN_INTERACTOR_THREAD_ID = "LOGIN_INTERACTOR_WORKER";

private UserRepo mUserRepository;
private Call<ResponseBody> mCall;

@Override
@Background(id = LOGIN_INTERACTOR_THREAD_ID)
public void login(final String username, final String password, final OnLoginFinishedListener listener) {

mUserRepository = RetrofitHelper.createClient(UserRepo.class);

mCall = mUserRepository.doLogin(Credentials.basic(username, password));

mCall.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
final int code = response.code();
if (code == 200) {
listener.onSuccess(response.headers().get(""));
} else if (code == 401) {
listener.invalidCredentialsFailure();
} else if (code >= 400 && code < 500) {
listener.invalidCredentialsFailure();
} else if (code >= 500 && code < 600) {
listener.noServerResponseFailure();
} else {
APIError error = ErrorUtils.parseError(response);
listener.onError("Unexpected response: code:"
+ error.getStatusCode()
+ "message: " + error.getMessage());
}
}

@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
if (t instanceof IOException) {
listener.noNetworkFailure();
} else if(call.isCanceled()) {
Log.e(TAG, "request was aborted");
} else {
listener.onError(t.getMessage());
}
}
});
}

@Override
public void cancel() {
mCall.cancel();
BackgroundExecutor.cancelAll(LOGIN_INTERACTOR_THREAD_ID, true);
}
}

使用 Retrofit 我正在调用我的网络服务以进行身份​​验证,网络服务应该返回一个 Authtoken 供以后使用。我只是不知道如何测试这个。我试过使用 Mockito 创建一个解决方案,但我不知道如何测试登录方法的逻辑。是否有 Mockito/Retrofit 方面的专家可以指导我更接近可行的解决方案?

最佳答案

在尝试任何单元测试之前,我会重构很多东西:

1) 根据在 enqueue 方法中创建的匿名类创建一个显式 Callback 类。我们称它为 LoginCallback:

public class LoginCallback implements Callback{
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
final int code = response.code();
if (code == 200) {
...
}

@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
if (t instanceof IOException) {
listener.noNetworkFailure();
...
}
}

现在您对这两个方法逻辑执行独立的单元测试。

2) 将静态方法调用移至包级方法。你最终会得到这样的结果:

public class LoginInteractorImpl implements LoginInteractor {

public void login(final String username, final String password, final OnLoginFinishedListener listener) {

mUserRepository = createClient();

mCall = mUserRepository.doLogin(getCredentials(username,password));

mCall.enqueue(new LoginCallback());
}

UserRepo createClient(){
RetrofitHelper.createClient(UserRepo.class)
}

Credentials getCredentials(String username, String password){
return Credentials.basic(username, password)
}
}

3) 单元测试login方法

@RunWith(JunitMockitoRunner.class)
public class TestClass{

@Spy
private LoginInteractorImpl loginInterceptor= new LoginInteractorImpl();

@Mock
private UserRepo mUserRepositoryMock;

@Mock
private Call<ResponseBody> mCallMock;

@Mock
private Credentials credentialsMock;

public void shouldEnqueueWhenLogin(){
// Arrange
String username = "name";
String password = "pass";
Mockito.doReturn(mUserRepositoryMock).when(loginInterceptor).createClient();

Mockito.doReturn(credentialsMock).when(loginInterceptor).when(getCredentials(username, password));

Mockito.doReturn(mCallMock).when(mUserRepositoryMock).doLogin(credentialsMock);

// Act
loginInterceptor.login(username, password);

// Assert that proper callback has been passed to enqueue
verify(mCallMock).enqueue(Mockito.any(LoginCallback.class));
}
}

关于java - 安卓交互器测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43515037/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com