Create RegistrationApiControllerTests

In Chapter 7, RESTful API Design - Building Language Between Frontend and Backend, we created the unit test class, MessageControllerTest. In this section, we will use the same approach to create the RegistrationApiControllerTests class. To test the register(RegistrationPayload) method, we will need to provide the input via Spring's MockMvc, and mock its dependency, UserService. In this way, we can verify the behavior of this API handler.

Now we're clear what need to do, let's add the following test methods that will cover four different scenarios:

  • register_blankPayload_shouldFailAndReturn400()
  • register_existedUsername_shouldFailAndReturn400()
  • register_existedEmailAddress_shouldFailAndReturn400()
  • register_validPayload_shouldSucceedAndReturn201()

The following is how RegistrationApiControllerTests looks with the first test method:

...
@RunWith(SpringRunner.class)
@WebMvcTest(RegistrationApiController.class)
public class RegistrationApiControllerTests {

@Autowired
private MockMvc mvc;

@Test
public void register_blankPayload_shouldFailAndReturn400() throws Exception {
mvc.perform(post("/api/registrations"))
.andExpect(status().is(400));
}
}

As you can see, in this test, we perform a HTTP POST request with an empty request body and expect the API handler to return a response with the HTTP status 400, meaning a bad request.

The second test method, register_existedUsername_shouldFailAndReturn400(), looks like the following:

...
@MockBean
private UserService serviceMock;
...
@Test
public void register_existedUsername_shouldFailAndReturn400() throws Exception {
RegistrationPayload payload = new RegistrationPayload();
payload.setUsername("exist");
payload.setEmailAddress("[email protected]");
payload.setPassword("MyPassword!");

doThrow(UsernameExistsException.class)
.when(serviceMock)
.register(payload.toCommand());

mvc.perform(
post("/api/registrations")
.contentType(MediaType.APPLICATION_JSON)
.content(JsonUtils.toJson(payload)))
.andExpect(status().is(400))
.andExpect(jsonPath("$.message").value("Username already exists"));
}

As you can see, we add the UserService dependency here as a mocked bean. Inside the test method, we specify the behavior of serviceMock, which throws RegistrationExcpetion when the register(RegistrationCommand) method is invoked. The doThrow() and the when() methods are the APIs of Mockito. Once the input and the mock are ready, we call the /api/registrations API with registration data passed in as request parameters. And then, we expect the result to be a HTTP 400 failure.

The third test method is similar to the second one. The only different is that inside this test, the exception thrown is EmailAddressExistException.

The fourth test method is also similar to the second one. The difference is that, we define the behavior of serviceMock differently, as shown in the following code. This is because the return value of the service's method is void and no exception will be thrown when the registration succeeds:

doNothing().when(serviceMock).register(form.toCommand());

As the name of this Mockito API, doNothing(), suggests, nothing will happen after the register() method completes, which is the desired behavior.

For the sake of brevity, many details of the RegistrationApiControllerTests class have been left out. You can check them in the commit history on GitHub.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.179.85