2016-09-27 22 views
5

Presenter sınıfım için bir test oluşturmak istiyorum, ancak Presenter'ın kendisinde CompositeSubscription örneğiyle ilgili sorunlarım var. Orada RxJava CompositeSubscription ile sunum birimi testi

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 bir arayüz (farklı bir modülün parçası) ve:

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) 

Bu kabaca benim Sunucu sınıftır: Testi çalıştırdığınızda bu hatayı alıyorum tüm retrofit API çağrılarını ve Aboneliklere dönüşümlerini içeren başka bir sınıf CoreModuleImpl'dir. gibi şey:

@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 
} 

Şimdi ne yapmak istiyorum getGummyBears() yöntemi test etmektir. Benim test yöntemi şöyle görünür:

@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); 
} 

Zaten farklı projelerden birçok test örnekleri gördüm ama hiç kimse bu Abonelik yaklaşımı kullanıyor. Sunucunun içinde doğrudan tüketilen Gözlemlenebilir'i iade ediyorlar. Ve bu durumda bir testin nasıl yazılacağını biliyorum.

Durumumu test etmenin doğru yolu nedir?

+0

Sunucunuzda CoreModule öğesini kabul ediyor musunuz? – skywall

+0

yep kusura bakmayın bu satırı 'presenter = yeni Presenter (coreModule, eventBus)' 'setUp' yönteminde – nicopasso

+0

merhaba @nicopasso bunu çözmek için herhangi bir yol bulabilir miydiniz? –

cevap

1

coreModule.getGummyBears() null döndürüyor gibi görünüyor. Sadece hata ayıklama ile adım ve oldukça açık olmalı. Alaycı çerçeveler kullanırken, yöntem çağrısının bu alay konusu nesnede ne döndürmesi gerektiğini belirmediğinizde alay edilmemiş bir nesnede yapılan yöntem çağrılarından döndürülmüş olabilirsiniz.

+0

Aslında kod, soruda yazdığımdan çok daha karmaşıktır. "CoreModule" sınıfı bir arabirimdir ve bu arabirimi uygulayan başka bir "coreImpl" sınıfı vardır. Sorumu proje yapmamla uyumlu olarak güncelledim. – nicopasso

0

Dave'in bahsettiği gibi, CoreModule.getGummyBears dönüş değeriyle dalga geçmeniz gerekir. Bir garip şey, oluşturulmakta olan CoreModuleImpl kullanmıyor olmanızdır. Bunun yerine, sunucunun yapıcısına coreModule aktarıyorsunuz.

Böyle bir şey yaparak getGummyBears() taklit:

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

Sonra karşılaşıyorsanız özel hata çözülmelidir. Bu özel test durumu için CoreModuleImpl'a ihtiyacınız var gibi görünmüyor.