Rxjava: 2.X Null object reference on Completable.test() with Mockito

Created on 17 Jun 2018  路  4Comments  路  Source: ReactiveX/RxJava

Hello, I'm actually having the following issue:

java.lang.NullPointerException: Attempt to invoke virtual method 'io.reactivex.observers.TestObserver io.reactivex.Completable.test()' on a null object reference
at repositorio.RepositorioProductoTest.insertarProductoTest(RepositorioProductoTest.java:48)

While testing a Completable in my code

    @Test
    public void insertarProductoTest(){
        repo.agregarElemento(productoDummy)
            .test()
            .assertComplete();

        verify(repo).darInstanciaDB().darDaoProducto().agregarProducto(argumentCaptor.capture());
        assertThat(argumentCaptor.getValue().getNombre(), Matchers.is("producto"));
    }

repo variable is being mocked with the @Mocked anotattion
And if it is useful, here is the .agregarElemento(...) method

   public Completable agregarElemento(Producto elemento) {
        return Completable.fromAction(()-> darInstanciaDB().darDaoProducto().agregarProducto(elemento));
    }

I'm following this as example
https://github.com/googlesamples/android-architecture-components/blob/master/BasicRxJavaSample/app/src/test/java/com/example/android/observability/UserViewModelTest.java

Hope you can help me.

Question StackOverflow

All 4 comments

The Completable reference is null per the exception. This doesn't have anything to do with RxJava.

Most likely you have the wrong or missing mocking of your object or method calls. Please ask this question on StackOverflow under the mockito tag.

Juan David, pudo encontrar la forma de probar el metodo? tengo exactamente el mismo incoveniente con un metodo igual. Gracias

Hola Jorge, la verdad no pude solucionarlo; hasta ah铆 deje el problema. Que tenga suerte solucionandolo.

Was this page helpful?
0 / 5 - 0 ratings