Create UserServiceImplTests

First things first: Let's create the unit test class UserServiceImplTests. To test UserServiceImpl, we will need to mock its dependencies—RegistrationManagement, MailManager, and DomainEventPublisher. There will be at least the following test methods to cover the different scenarios:

  • register_nullCommand_shouldFail()
  • register_existingUsername_shouldFail()
  • register_existingEmailAddress_shouldFail()
  • register_validCommand_shouldSucceed()

The following is how the first test method looks:

...
public class UserServiceImplTests {
private RegistrationManagement registrationManagementMock;
private DomainEventPublisher eventPublisherMock;
private MailManager mailManagerMock;
private UserServiceImpl instance;

@Before
public void setUp() {
registrationManagementMock = mock(RegistrationManagement.class);
eventPublisherMock = mock(DomainEventPublisher.class);
mailManagerMock = mock(MailManager.class);
instance = new UserServiceImpl(registrationServiceMock,
eventPublisherMock, mailerMock);
}

@Test(expected = IllegalArgumentException.class)
public void register_nullCommand_shouldFail() throws
RegistrationException {
instance.register(null);
}
}

As you can see, we use the setUp() method to create the mocks and instantiate UserServiceImpl. In our first test method, register_nullCommand_shouldFail(), we pass a null value to the register() method to let it fail explicitly. We use @Test(expected = IllegalArgumentException.class) to tell JUnit that we're expecting this method to throw an IllegalArgumentException error. If this exception isn't thrown, then this test should be considered failed.

Since the rest of the test methods are similar to those in RegistrationApiControllerTests, we won't list them here. Check the commit history on GitHub for details.

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

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