2016-09-27 8 views
5

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을 반환합니다. 그리고 그 경우에는 테스트를 작성해야하는 방법을 알고 있습니다.

내 상황을 테스트하는 올바른 방법은 무엇입니까?

+0

당신은 생성자에서 CoreModule을 사용합니까? – skywall

+0

예, 죄송합니다.'setUp' 메쏘드에'presenter = new Presenter (coreModule, eventBus)'를 추가하는 것을 잊어 버렸습니다. – nicopasso

+0

안녕하세요 @nicopasso 해결 방법은 없습니까? –

답변

1

coreModule.getGummyBears()은 null을 반환합니다. 그냥 디버그를 통해 단계와 그것은 꽤 분명해야합니다. 조롱 프레임 워크를 사용할 때 조롱 된 객체에 대해 메소드 호출이 반환해야하는 것을 지정하지 않은 경우 조롱 된 객체의 메소드 호출에서 null을 반환 할 수 있습니다.

+0

실제로 코드는 내가이 질문에서 쓴 것보다 훨씬 더 복잡합니다. "coreModule"클래스는 인터페이스이며이 인터페이스를 구현하는 또 다른 클래스 "coreImpl"이 있습니다. 프로젝트 구조에 따라 질문을 업데이트했습니다. – nicopasso

0

Dave가 언급했듯이 반환 값 CoreModule.getGummyBears을 조롱해야합니다. 한 가지 이상한 점은 작성중인 CoreModuleImpl을 사용하고 있지 않다는 것입니다. 대신 coreModule을 표현 자의 생성자에 전달합니다.

이 같은 것을 수행하여 getGummyBears()을 조롱 할 수 있습니다

when(coreModule.getGummyBears()).thenReturn(MockBuilder.newGummyBearList(30); 

그런 다음 당신이 발생한 특정 오류가 해결되어야한다. 이 특정 테스트 케이스에 대해 CoreModuleImpl이 필요하지는 않습니다.

관련 문제