4

i'm trying to learn project Reactor and have the problem.

@Test
@DisplayName("check that standaloneUser fields are correct")
void validateUserFields() {
    userService.save(standaloneUser).subscribe();
    assertEquals(userService.count().block(), Long.valueOf(1));
    User user = userService.findByEmail("[email protected]").block();
    assertNotNull(user);
    assertNotNull(user.getId());
    assertEquals(user.getFirstName(), "test");
    assertEquals(user.getLastName(), "test");
    assertNotEquals(user.getPassword(), "test");
    assertEquals(user.getRole(), Role.CANDIDATE);
    assertNotNull(user.getCreatedDate());
    assertNull(user.getStoppedDate());
    assertEquals(user.getEmail(), "[email protected]");
}

Sometimes block() method returns null. Who can explain me this? Thanks

1 Answer 1

6

block() can return null, it means the Mono completed empty, which in this case means the user wasn't found.

Could it be that it wasn't properly saved? (although you assert the user count)

Note that you do userService.save(standaloneUser).subscribe(). This form is often not ideal, as it is "async fire-and-forget":

  • async -> it might complete after the subsequent asserts
  • fire and forget -> no error handler means that it could terminate with an error and hide it from you.

Make an habit of at least setting onNext and onError handler lambdas when calling subscribe.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.