如何在公共方法中测试私有可观察性



我是测试新手。

我有类似下一个代码的东西,并希望使用 Mockito 用 unitTests 来覆盖它:

    public void doSomeJob(){
        //some code before
        getMvpView().execute(getObservable());
        //some code after
    }
    private Observable<Boolean> getObservable(){
        return Observable.create(new ObservableOnSubscribe<Boolean>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<Boolean> e) throws Exception {
                Thread.sleep(5000);
                e.onNext(true);
                e.onComplete();
            }
        });
    }

所以问题:

  1. 使用Mokito getMvpView().execute(getObservable());的正确写作测试如何?
  2. 如何验证getObservable()的结果?

如果你的私有方法不是接口的一部分,即无法从类外部访问,那么它就不是你应该测试的东西(大概不是,因为它是私有的(。反过来,Mockito不提供对私有方法的嘲弄。因此,您要么需要更改接口(使这些数据在外部可用(,要么

不进行测试。

你应该测试的是调用被测类的公共方法的效果。如果这样做,以后可以自由重构实现细节,并且测试仍将验证类是否按预期工作。

假设您的代码是演示器实现的一部分,getMvpView(( 方法返回一个视图接口:

public class MvpPresenterImpl {
    private MvpView view;
    public void doSomeJob(){
        //some code before
        getMvpView().execute(getObservable());
        //some code after
    }
    public void attachView(MvpView view) {
        this.view = view;
    }
    private MvpView getMvpView() {
        return view;
    }
    private Observable<Boolean> getObservable(){
        return Observable.create(new ObservableOnSubscribe<Boolean>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<Boolean> e) throws Exception {
                Thread.sleep(5000);
                e.onNext(true);
                e.onComplete();
            }
        });
    }
}

您可以像这样测试doSomeJob()的效果:

public class MvpPresenterImplTest {
    private MvpPresenterImpl presenter;
    private MvpView mockView;
    @Before
    public void setUp() throws Exception {
        // Create a mock view instance so that we can verify method calls on it
        mockView = mock(MvpView.class);
        // Create our object under test, and set it up with the mock view
        presenter = new MvpPresenterImpl();
        presenter.attachView(mockView);
    }
    @Test
    public void doSomeJob_callsExecuteOnViewWithCorrectObserver() throws Exception {
        // What we want to test is the effect of invoking a public method.
        presenter.doSomeJob();
        // Verify that the execute method has been called by your class 
        // under test, and save the parameter for later.
        ArgumentCaptor<Observable<Boolean>> paramCaptor = 
            ArgumentCaptor.<Observable<Boolean>>forClass((Class)Observable.class);
        verify(mockView).execute(paramCaptor.capture());
        // Get the actual observable that the execute method was called with.
        Observable<Boolean> param = paramCaptor.getValue();
        // Get a test observer so that we can check what our Observable emits 
        // (TestObserver is a built-in feature of RxJava, not Mockito.)
        TestObserver<Boolean> test = param.test();
        // Assert that the Observable behaves as expected
        test.assertComplete();
        test.assertResult(true);
    }
}

相关内容

  • 没有找到相关文章

最新更新