2016-04-12 21 views
5

Bu sınıfı standart bir JUnit testinde denemeye çalışıyorum, ancak Schedulers.io() ile NullPointerException'a asıyorum. Schedulers.io() alay edilebilir mi?RxJava Schedules.io() Birim Testlerinde NullPointerException

Bu bir Android uygulamasıdır ve sürekli entegrasyon ve coveralls.io kapsamını bildirmek için travis-ci'yi kullanarak kod üzerinde tam kapsama alanı sağlamaya çalışıyorum.

Sınıf Test edilecek:

public class GetLiveStreamsList extends UseCase { 

    private final String filename; 
    private final ContentRepository contentRepository; 

    public GetLiveStreamsList(final String filename, final ContentRepository contentRepository, ThreadExecutor threadExecutor, PostExecutionThread postExecutionThread) { 
     super(threadExecutor, postExecutionThread); 

     this.filename = filename; 
     this.contentRepository = contentRepository; 

    } 

    @Override 
    protected Observable buildUseCaseObservable() { 

     Action1<List<LiveStreamInfo>> onNextAction = new Action1<List<LiveStreamInfo>>() { 

      @Override 
      public void call(List<LiveStreamInfo> liveStreamInfos) { 

       try { 
        Thread.sleep(5000); 
       } catch(InterruptedException e) { } 

      } 

     }; 

     return this.contentRepository.liveStreamInfos(this.filename) 
       .repeat(Schedulers.io()) 
       .doOnNext(onNextAction); 
    } 

} 

Testi Sınıfı:

java.lang.NullPointerException 
    at org.mythtv.android.domain.interactor.GetLiveStreamsList.buildUseCaseObservable(GetLiveStreamsList.java:47) 
    at org.mythtv.android.domain.interactor.GetLiveStreamsListTest.testGetLiveStreamsListUseCaseObservableHappyCase(GetLiveStreamsListTest.java:48) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300) 
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157) 
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) 
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234) 
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 


Process finished with exit code 255 
+0

Eğer yığın izleme gönderir misiniz:

Böyle dönüş değeri ayarlamanız gerekir? – zsxwing

+0

@zsxwing Stacktrace'i ekledim. Bu şekilde hareket eden testlerimden sadece biri. Diğer benzerleri bunu yapmaz. Bunun repeat() içindeki schedulers.io() ile yapılması gerektiğine inanıyorum. Diğer aramalar, uygulamayı çalıştırdığımda, o zaman patlar, ancak schedulers.immediate() kullanmam gerektiğini gösterir. – dmfrey

+0

Eh, sahte sahtekarlar varsa, gerçek yürütmenin gerçekleşmesini nasıl beklersiniz? Sadece gerçek konuları içeren bir entegrasyon testine geçin ... –

cevap

2

Sen contentRepository.liveStreamInfos döndürülen değerini kurmayı unuttuğu: Burada

public class GetLiveStreamsListTest { 

    private static final String FAKE_FILENAME = "fake filename"; 

    private GetLiveStreamsList getLiveStreamsList; 

    @Mock 
    private ThreadExecutor mockThreadExecutor; 

    @Mock 
    private PostExecutionThread mockPostExecutionThread; 

    @Mock 
    private ContentRepository mockContentRepository; 

    @Before 
    public void setUp() { 

     MockitoAnnotations.initMocks(this); 
     getLiveStreamsList = new GetLiveStreamsList(FAKE_FILENAME, mockContentRepository, mockThreadExecutor, mockPostExecutionThread); 

    } 

    @Test 
    public void testGetLiveStreamsListUseCaseObservableHappyCase() { 

     getLiveStreamsList.buildUseCaseObservable(); 

     verify(mockContentRepository).liveStreamInfos(FAKE_FILENAME); 
     verifyNoMoreInteractions(mockContentRepository); 
     verifyZeroInteractions(mockThreadExecutor); 
     verifyZeroInteractions(mockPostExecutionThread); 

    } 

} 

StackTrace olduğunu. Varsayılan olarak null.

when(contentRepository.liveStreamInfos()).thenReturn(...); 
+0

Teşekkürler. Aramayı tekrar tekrar etme ihtiyacını ortadan kaldırmak için bunu tekrar destekledim. – dmfrey