gpt4 book ai didi

android - Mockito Android 单元测试

转载 作者:行者123 更新时间:2023-11-30 00:38:00 25 4
gpt4 key购买 nike

我的应用程序中有 MVP。 Presenter有接口(interface)

public interface ILoginPresenter<V> extends Presenter<V> {
void logUserIn(String email, String password, String deviceToken, String deviceType);
}

实现有RX Single

 mLoginSubscription = mModel.logUserIn(email, password, deviceToken, deviceType)
.compose(RxUtil.setupNetworkSingle())
.subscribe(new Subscriber<User>() {
@Override
public void onCompleted() {
Timber.i("Log in complete");
}

@Override
public void onError(Throwable e) {
Timber.e(e, "Retrofit could not get User.");

getView().dismissProgressDialog();
}

@Override
public void onNext(UserResponseRetrofit response) {
Timber.i("Retrofit is attempting to get User");
mSaveModel.saveUser(user);
getView().dismissProgressDialog();
getView().goToMenuActivity();
}
});

我还有 Dagger 模块

@Module
public class ModelModule {
@Provides
@ScreenScope
public ILoginModel provideLoginModel(LoginModel p) {
return p;
}
}

我的单元测试如下所示:

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21, manifest = "/src/main/AndroidManifest.xml")
public class LoginPresenterTest {

public static final String SOME_OTHER_TOKEN = "someOtherToken";
private AppComponent mAppComponent;
private LoginComponent mLoginComponent;
private ILoginView mockView;
private ModelModule mockModel;
private ILoginPresenter mLoginPresenter;

@Before
public void setup() {
// Creating the mocks
mockView = Mockito.mock(ILoginView.class);
mockModel = Mockito.mock(ModelModule.class);

ILoginModel mock = Mockito.mock(ILoginModel.class);
User urr = Mockito.mock(User.class);
Mockito.when(mockModel.provideLoginModel(null)).thenReturn(mock);
Mockito.when(mock.logUserIn("", "", "", "")).thenReturn(ScalarSynchronousSingle.just(urr));

mAppComponent = DaggerAppComponent.builder()
.appModule(new AppModule(RuntimeEnvironment.application))
.build();

mLoginComponent = DaggerLoginComponent.builder()
.appComponent(mAppComponent)
.modelModule(mockModel)
.presenterModule(new PresenterModule())
.build();

mLoginPresenter = mLoginComponent.provideLoginPresenter();
mLoginPresenter.setView(mockView);
}

@Test
public void testLogin() {
mLoginPresenter.logUserIn("", "", "", "");
try {
java.lang.Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Mockito.verify(mockView).dismissProgressDialog();
}

所以使用 Dagger 我需要正确构建 Presenter。为此,我正在尝试使用 Mockito.when。首先看起来这条线不起作用

Mockito.when(mockModel.provideLoginModel(null)).thenReturn(mock);

目标目的是使用我自己的模型实现,返回 Single。

真的不明白为什么我的 ModelModule 模拟不起作用?

最佳答案

如何从生产 Module 中创建一个测试模块?

查看他们如何建议通过 Dagger 进行测试 in official site .

@Module
public class ModelModuleTest extends ModelModule {

@Override
public ILoginModel provideLoginModel(LoginModel p) {
...
}
}

您可以将模拟的依赖项传递给您的模块

关于android - Mockito Android 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43093061/

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