Presenter 클래스에 대한 테스트를 만들고 싶지만 Presenter 자체의 CompositeSubscription 인스턴스에 문제가 있습니다. 이 RxJava CompositeSubscription을 사용한 발표자 단위 테스트
public class Presenter {
CompositeSubscription compositeSubscription = new CompositeSubscription();
//creation methods...
public void addSubscription(Subscription subscription) {
if (compositeSubscription == null || compositeSubscription.isUnsubscribed()) {
compositeSubscription = new CompositeSubscription();
}
compositeSubscription.add(subscription);
}
public void getGummyBears() {
addSubscription(coreModule.getGummyBears());
}
}
는 CoreModule가 인터페이스 (다른 모듈의 일부)이며 :
java.lang.NullPointerException
at rx.subscriptions.CompositeSubscription.add(CompositeSubscription.java:60)
at com.example.Presenter.addSubscription(Presenter.java:67)
at com.example.Presenter.getGummyBears(Presenter.java:62)
이 대략 내 발표자 클래스입니다 : 내가 테스트를 실행하면이 오류를 받고 있어요 모든 개조 API 호출과 Subscription으로의 변환으로 위치하는 또 다른 클래스 CoreModuleImpl입니다. 같은 뭔가 :
@Override public Subscription getGummyBears() {
Observable<GummyBears> observable = api.getGummyBears();
//a bunch of flatMap, map and other RxJava methods
return observable.subscribe(getDefaultSubscriber(GummyBear.class));
//FYI the getDefaultSubscriber method posts a GummyBear event on EventBus
}
이제 어떻게 내가하고 싶은 것은 getGummyBears()
방법을 테스트하는 것입니다. 내 시험 방법은 다음과 같습니다
@Mock EventBus eventBus;
@Mock CoreModule coreModule;
@InjectMock CoreModuleImpl coreModuleImpl;
private Presenter presenter;
@Before
public void setUp() {
presenter = new Presenter(coreModule, eventBus);
coreModuleImpl = new CoreModuleImpl(...);
}
@Test
public void testGetGummyBears() {
List<GummyBears> gummyBears = MockBuilder.newGummyBearList(30);
//I don't know how to set correctly the coreModule subscription and I'm trying to debug the whole CoreModuleImpl but there are too much stuff to Mock and I always end to the NullPointerException
presenter.getGummyBears(); //I'm getting the "null subscription" error here
gummyBears.setCode(200);
presenter.onEventMainThread(gummyBears);
verify(gummyBearsView).setGummyBears(gummyBears);
}
이미 다른 프로젝트에서 많은 테스트 사례가 보았지만 아무도이 구독 방식을 사용하지 않습니다. 그들은 단지 발표자 내에서 직접 소비되는 Observable을 반환합니다. 그리고 그 경우에는 테스트를 작성해야하는 방법을 알고 있습니다.
내 상황을 테스트하는 올바른 방법은 무엇입니까?
당신은 생성자에서 CoreModule을 사용합니까? – skywall
예, 죄송합니다.'setUp' 메쏘드에'presenter = new Presenter (coreModule, eventBus)'를 추가하는 것을 잊어 버렸습니다. – nicopasso
안녕하세요 @nicopasso 해결 방법은 없습니까? –