1

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.

Minato Namikaze
  • 576
  • 1
  • 7
  • 22

2 Answers2

1

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.

Bob Dalgleish
  • 8,167
  • 4
  • 32
  • 42
1

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

Shine
  • 3,788
  • 1
  • 36
  • 59