I am trying to substitute the correct use of Mockito, during a test session by JUnit, in place of to stub a class. Unfortunately on web there are a lot of tutorials regarding Mockito but, less for the stub approach, and I would like to learn this technique.
This test is made by Mockito:
@Test
public void addWrongNewUserSpaceInUsername() throws Exception {
when(userValidator.isValidUsername(user.getUsername())).thenReturn(false);
try {
mockMvc.perform(
post("/register")
.contentType(MediaType.APPLICATION_JSON)
.content(asJsonString(user)
));
} catch (Exception e) {
Assert.assertTrue(e.getCause() instanceof UsernameNotValidException);
}
}
To clarify these are the classes involved:
1) Controller
@RestController
public class UserController {
@Autowired
RepositoryUserDB repositoryUserDB;
@Autowired
UserValidator userValidator;
@RequestMapping(value = "/register", method = RequestMethod.POST)
public User createUser(@RequestBody User user) {
if (userValidator.isValidUsername(user.getUsername())) {
if(!userValidator.isValidPassword(user.getPassword())){
throw new PasswordNotValidException();
}
if(userValidator.isValidDateOfBirth(user.getDateOfBirth()) == false){
throw new DOBNotValidException();
}
// se lo user e' gia' presente
if (repositoryUserDB.getUserByUsername(user.getUsername()) == null) {
return repositoryUserDB.createUser(user);
}
throw new UsernameAlreadyExistException();
} else throw new UsernameNotValidException();
}
}
2)Interface of repo:
public interface RepositoryUserDB {
User getUserByUsername(String username);
User createUser(User user);
}
3)Repo:
@Component
public class MemoryUserDB implements RepositoryUserDB{
Map<String, User> repo;
public MemoryUserDB() {
this.repo = new HashMap<>();
}
@Override
public User getUserByUsername(String username) {
return repo.get(username);
}
@Override
public User createUser(User user) {
repo.put(user.getUsername(),user);
return repo.get(user.getUsername());
}
}
4) Validator:
@Component
public class UserValidator {
public boolean isValidUsername(String username) {
return username.matches("^[a-zA-Z0-9]+$");
}
public boolean isValidPassword(String pwd) {
if (pwd == null)
return false;
return pwd.matches("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d){4,}.+$");
}
public boolean isValidDateOfBirth(String DOB) {
return DOB.matches("^(?:(?:(?:0?[13578]|1[02])(\\/|-|\\.)31)\\1|(?:(?:0?[1,3-9]|1[0-2])(\\/|-|\\.)(?:29|30)\\2))(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$|^(?:0?2(\\/|-|\\.)29\\3(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0?[1-9])|(?:1[0-2]))(\\/|-|\\.)(?:0?[1-9]|1\\d|2[0-8])\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$");
}
}
5)ResEntityExceptionHandler
@ControllerAdvice
public class RestEntityExceptionHandler {
@ExceptionHandler(UsernameNotValidException.class)
@ResponseStatus(value = HttpStatus.FORBIDDEN, reason = "username wrong")
public void handleUsernameException() {
}
@ExceptionHandler(UsernameAlreadyExistException.class)
@ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "the username is already presents")
public void handleUsername() {
}
@ExceptionHandler(PasswordNotValidException.class)
@ResponseStatus(value = HttpStatus.FORBIDDEN, reason = "password wrong")
public void handlePasswordException() {
}
@ExceptionHandler(DOBNotValidException.class)
@ResponseStatus(value = HttpStatus.FORBIDDEN, reason = "Date Of Birth wrong")
public void handleDOBException(){
}
}
Theoretically, for you use case, the stub approach should be rather simple to do.
But as you rely on a Spring Boot Test that uses the Spring beans container, things are really harder to setup as you should find a way to inject the mocked bean in the container to replace the actual bean : UserValidator
.
Mocks in Spring Boot test rely usually on Spring Boot MockBean
.
It is not a mockito mock but not very far.
To understand differences with Mockito mocks, you may refer to this question.
Using a framework gives many features out of the box, but also limits yourself as you want to bypass the framework features.
Supposing that you didn't perform an integration test with Spring Boot, but a real unit test (so without the Spring boot container), things could be performed in this way.
Instead of mocking UserValidator.isValidUsername()
, you define a custom implementation of UserValidator
that stubs the method return as expected in your test.
Finally, what Mockito or any mock framework does for you.
So here is the stub class :
public class UserValidatorStub extends UserValidator {
private String expectedUsername;
private boolean isValidUsername;
public UserValidatorStub(String expectedUsername, boolean isValidUsername){
this.expectedUsername = expectedUsername;
this.isValidUsername = isValidUsername;
}
public boolean isValidUsername(String username) {
if (username.equals(expectedUsername)){
return isValidUsername;
}
// as fallback, it uses the default implementation but you may return false or null as alternative
return super.isValidUsername(username);
}
}
It accepts a constructor to store the expected arguments passed to the stubbed method and the stubbed result to return.
Now, here how your test could be written :
@Test
public void addWrongNewUserSpaceInUsername() throws Exception {
// inject the mock in the class under test
UserController userController = new UserController(new UserValidatorStub(user.getUsername(), false));
try {
userController.createUser(user);
} catch (Exception e) {
Assert.assertTrue(e.getCause() instanceof UsernameNotValidException);
}
}
Note that the example relies on constructor injection in UserController
to set the dependencies.