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());
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.
Most helpful comment
Usually, exceptions do not implement
equals(), you can check exception type and message separately: