Minato Namikaze
Minato Namikaze

Reputation: 606

Assertions are not failing in onErrorReturn rx java

I am having following code:

public Single<Post> doSomething(Post post) {
    String endPoint = "some-url";
    JsonObject payload = new JsonObject().put("message", post.getPost());
    return oAuth.rxApi(HttpMethod.POST, endPoint, payload)
            .map(r -> {
                post.setPostId(r.getString("id"));
                post.setStatus(PostStatus.PROCESSED);
                return post;
            }).onErrorReturn(e -> {
                post.setStatus(PostStatus.FAILED);
                post.setError(e.getMessage());
                return post;
            });
}

And I have written following test case for it.

@Test
public void shouldHandleFailureWhilePosting() {
    Post post = new Post();
    post.setSocialAccountID("1234");
    post.setAccessToken("ABC");
    post.setPost("Some post to share!!!");

    String shareEndPoint = "some-url";
    JsonObject sharePayload = new JsonObject().put("message", post.getPost());
    when(oAuth2Auth.rxApi(HttpMethod.POST, shareEndPoint, sharePayload)).thenReturn(Single.error(new NoStackTraceThrowable("Error")));

    service.doSomething(post)
            .subscribe(postResponse -> {
                assert postResponse.getError().equals("Errorssss");
                assert postResponse.getAttachedMedia() == null;
                assert postResponse.getPostId() == null;
                assert postResponse.getStatus().equals(PostStatus.FAILED);
            });
}

Now my assertions in this case always pass. Even if I make a wrong assertion, it is always green.

Upvotes: 1

Views: 244

Answers (2)

Shine
Shine

Reputation: 3818

Have you enabled assertion checks with the -ea option?

Assertion, by default, are disabled ... Also, testing framework usually expose own "asserts" methods. For instance, on jUnit (org.junit.Assert), you may find static assertTrue(), assertArrayEquals(), assertNull() and so on

Upvotes: 1

Bob Dalgleish
Bob Dalgleish

Reputation: 8227

Assertions are executed in the context of the observable chain, and won't be directly accessible from your tests.

Instead use a TestSubscriber to subscribe to the observable chain. It has a number of assertions on it.

Upvotes: 1

Related Questions