Rxjava: TestSubscriber throws AssertionError in .assertError even if Exceptions are the same

Created on 6 Aug 2015  路  3Comments  路  Source: ReactiveX/RxJava

I am trying to use a TestSubscriber to unit test a function that receives an Observable. I am arranging the test using TestSubscriber and its onError call. When I try to assert using assertError I get an AssertionError: Exceptions differ; expected: java.net.SocketTimeoutException, actual: java.net.SocketTimeoutException.

My unit test:

    // arrange
    Mockito.when(mockJobRepository.getJobCountRefreshedTime()).thenReturn(0l);
    Mockito.when(mockJobRepository.refreshJobCount()).thenReturn(Observable.just(1337));
    TestSubscriber<Integer> testSubscriber = new TestSubscriber<>();
    testSubscriber.onError(new SocketTimeoutException());
    mockJobRepository.refreshJobCount().subscribe(testSubscriber);

    // act
    dashboardPresenter.onViewCreated();

    // assert
    testSubscriber.assertCompleted();
    testSubscriber.assertError(new SocketTimeoutException());

Most helpful comment

Usually, exceptions do not implement equals(), you can check exception type and message separately:

// assert type of exception
testSubscriber.assertError(SocketTimeoutException.class);

// assert exception's message
assertThat(testSubscriber.getOnErrorEvents().get(0).getMessage())
  .isEqualTo("expected message");

All 3 comments

System.out.println(new SocketTimeoutException() == new SocketTimeoutException());
System.out.println(new SocketTimeoutException().equals(new SocketTimeoutException()));

Usually, exceptions do not implement equals(), you can check exception type and message separately:

// assert type of exception
testSubscriber.assertError(SocketTimeoutException.class);

// assert exception's message
assertThat(testSubscriber.getOnErrorEvents().get(0).getMessage())
  .isEqualTo("expected message");

@headinthebox @artem-zinnatullin I didn't realize that, thank you! Using Exception.class worked.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gfx picture gfx  路  3Comments

SammyVimes picture SammyVimes  路  4Comments

archenroot picture archenroot  路  3Comments

yubaokang picture yubaokang  路  3Comments

perlow picture perlow  路  3Comments