Last active
November 13, 2015 14:49
-
-
Save up1/ec087ea9702345027fbf to your computer and use it in GitHub Desktop.
Test Double :: Mock, Stub and Dummy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UserService { | |
private final UserDAO userDAO; | |
public UserService(UserDAO userDAO) { | |
this.userDAO = userDAO; | |
} | |
public void createUser(User user) { | |
//Some business logic and validation and etc | |
userDAO.save(user); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UserService { | |
private final UserDAO userDAO; | |
private final UserValidationService userValidationService; | |
public UserService(UserDAO userDAO, UserValidationService userValidationService) { | |
this.userDAO = userDAO; | |
this.userValidationService = userValidationService; | |
} | |
public void validate(User user) { | |
if(this.userValidationService.isValid(user)) { | |
user.setStatus(true); | |
} else { | |
user.setStatus(false); | |
} | |
} | |
public void createUser(User user) { | |
//Some business logic and validation and etc | |
userDAO.save(user); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UserServiceTest { | |
//Test fixtures | |
UserDAO userDAO = mock(UserDAO.class); | |
UserService userService = new UserService(userDAO); | |
User newUser = new User(); | |
@Test | |
public void createUser_save_user() { | |
//Call method from Class Under Test | |
userService.createUser(newUser); | |
//Verify expectation | |
verify(userDAO).save(newUser); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UserServiceTest { | |
//Test fixtures | |
UserDAO userDAO = mock(UserDAO.class); | |
User newUser = new User(); | |
class UserValidationServiceStub extends UserValidationService { | |
@Override | |
public boolean isValid(User user) { | |
return true; | |
} | |
} | |
@Test | |
public void valid_user_when_validate_is_passed() { | |
//Arrange | |
UserValidationService userValidationServiceStub = new UserValidationServiceStub(); | |
UserService userService = new UserService(userDAO, userValidationServiceStub); | |
//Call method from Class Under Test | |
userService.validate(newUser); | |
//Verify state | |
assertTrue(newUser.isStatus()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UserServiceTest { | |
//Test fixtures | |
User newUser = new User(); | |
class UserDAODummy extends UserDAO { | |
public void save(User user) { | |
throw new RuntimeException("Not expected to be called"); | |
} | |
} | |
class UserValidationServiceStub extends UserValidationService { | |
@Override | |
public boolean isValid(User user) { | |
return true; | |
} | |
} | |
@Test | |
public void valid_user_when_validate_is_passed() { | |
//Arrange | |
UserDAO userDAODummy = new UserDAODummy(); | |
UserValidationService userValidationServiceStub = new UserValidationServiceStub(); | |
UserService userService = new UserService(userDAODummy, userValidationServiceStub); | |
//Call method from Class Under Test | |
userService.validate(newUser); | |
//Verify state | |
assertTrue(newUser.isStatus()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment