1

我正在创建 UI 测试。为了不与真实服务器交互,我使用了 MockWebServer。我的目标是模拟各种服务器响应并查看整个程序将如何响应它们。目前,我不明白如何打开需要授权的屏幕。当然,我可以编写一个登录到授权屏幕的代码,然后转到所需的窗口。但这需要额外的时间来完成测试,我想避免这种情况。我不想模拟类,因为我需要检查应用程序的生产版本。我怎样才能做到这一点?

对于 DI,我使用 Dagger-2。这是组件代码:

@Singleton
@Component(modules = {
        AvatarsModule.class,
        EncryptionModule.class,
        ApiModule.class,
        WalletsModule.class,
        GeneralModule.class,
        InteractorsModule.class,
        PushNotificationsModule.class,
        AppModule.class
})
public interface AppComponent {
    @Component.Builder
    interface Builder {
        @BindsInstance
        Builder context(Context context);
        AppComponent build();
    }

    void inject(App app);
}

这是存储授权状态的类代码:

public class ApiWrapper {
    private Api api;
    private KeyPair keyPair;
    private Account account; 

    ...

    public Flowable<Authorization> authorize(KeyPair tempKeyPair) {
        return api
                .authorize(tempKeyPair.getPublicKeyString().toLowerCase())
                .subscribeOn(Schedulers.io())
                .doOnNext((authorization -> {
                    this.account = authorization.getAccount();
                    this.keyPair = tempKeyPair;
                }));
    }
    ...
}
4

1 回答 1

0

如果还有人感兴趣的话。我编写了一个 InstrumentationTestFacade 类,我使用 Dagger 在其中放置了一个 ApiWrapper 对象。接下来,将 InstrumentationTestFacade 注入到 Application 对象中。由于应用程序对象不是单例,因此在主代码中没有责任泄漏,但是从测试代码中您可以使用以下代码访问此外观:

Application application = (Application) InstrumentationRegistry.getInstrumentation().getTargetContext().getApplicationContext();
InstrumentationTestFacade facade = application.getInstrumentationTestFacade();

下面是一个例子:

public class InstrumentationTestFacade {
    private LogoutInteractor logoutInteractor;
    private SecurityInteractor securityInteractor;
    private ApiWrapper apiWrapper;

    public InstrumentationTestFacade(
            LogoutInteractor logoutInteractor,
            SecurityInteractor securityInteractor,
            ApiWrapper apiWrapper
    ) {
        this.logoutInteractor = logoutInteractor;
        this.securityInteractor = securityInteractor;
        this.apiWrapper = apiWrapper;
    }

    public void logout() {
        logoutInteractor.logout();
    }

    public ApiWrapper getApiWrapper() {
        return apiWrapper;
    }

    public SecurityInteractor getSecurityInteractor() {
        return this.securityInteractor;
    }
}
public class Application extends MultiDexApplication implements HasActivityInjector, HasServiceInjector {

    ...

    @Inject
    InstrumentationTestFacade instrumentationTestFacade;


    @Override
    public void onCreate() {
        super.onCreate();

        DaggerAppComponent
                .builder()
                .context(this)
                .build()
                .inject(this);

    }

    ...

    public InstrumentationTestFacade getInstrumentationTestFacade() {
        return instrumentationTestFacade;
    }
}
于 2019-09-18T17:15:27.970 回答