junit - 如何在真实课堂中模拟场景?

时间:2017-06-10 07:46:57

标签: java unit-testing junit mockito

我有一个棘手的情况。我正在使用MVP架构的android,但这并不重要。我有一个名为DoStandardLoginUsecase的类,它基本上只是连接到具有登录信息的服务器并获取访问令牌。我正在尝试测试它。但问题是我传递给它的背景,所以我可以初始化匕首。

    public class DoStandardLoginUsecase extends BaseUseCase {

    @Inject
    UserDataRepository mUserDataRepo;

    private StandardLoginInfo loginInfo;


    public DoStandardLoginUsecase(Context context) {
/* SEE HERE I AM USING A APPLICATION CONTEXT THAT I PASS TO DAGGER
*/
        ((MyApplication)context).getPresenterComponent().inject(this);
    }

    @Override
    public Observable<Login> buildUseCaseObservable() {
        return mUserDataRepo.doStandardLogin(loginInfo);
    }

    public void setLoginInfo(StandardLoginInfo loginInfo) {
        this.loginInfo = loginInfo;
    }
}

这是我到目前为止的测试:

 public class DoStandardLoginUsecaseTest {

    DoStandardLoginUsecase standardLoginUsecase;
        StandardLoginInfo fakeLoginInfo;
    TestObserver<Login> subscriber;

MockContext context;
        @Before
        public void setUp() throws Exception {
//now when i create the object since its a mock context it will fail when it tries to call real things as these are stubs.  So how do i test this object. how do i create an instance of this object ?  I am willing to use [daggerMock][1] if that helps also. 
standardLoginUsecase = New DoStandardLoginUsecase(context); 
            fakeLoginInfo = new StandardLoginInfo("fred@hotmail.com","Asdfgh4534");
            subscriber = TestObserver.create();
        }


       @Test
        public void buildUseCaseObservable(){

standardLoginUsecase.seLoginInfo(fakeLoginInfo);
    standardLoginUsecase.buildUseCaseObservable().subscribe(subscriber);

           subscriber.assertNoErrors();
           subscriber.assertSubscribed();
           subscriber.assertComplete();
       }
    }

0 个答案:

没有答案