To demonstrate the unit tests with happy and exception scenarios, following classes have been shown below:
Class SignupValidation
The class consists of the three validateXXX methods to validate three different business rules mentioned above. The need is to test these three validation methods in the unit tests.
public class SignupValidation {
private EmailValidator emailValidator;
public SignupValidation() {
emailValidator = new EmailValidator();
}
public boolean validate( User u ) throws SignupException{
validateEmailAddress( u.getEmailAddress() );
validateAge( u.getAge() );
validateFirstName( u.getFirstName() );
return true;
}
public boolean validateFirstName(String firstName) throws SignupException {
if( firstName.trim().length() == 0 ) {
throw new SignupException( "Firstname can not be empty" );
}
return true;
}
public boolean validateEmailAddress( String email ) throws SignupException {
boolean isValid = emailValidator.validate( email );
if( !isValid ) {
throw new SignupException( "Invalid email address" );
}
return isValid;
}
public boolean validateAge( int age ) throws SignupException {
if( age < 18 ) {
throw new SignupException( "Age must be 18 and above" );
}
return true;
}
}
Class SignupValidationTest
The unit tests consist of methods to test both, happy path and the exception scenarios. Take a look at following for details:
public class SignupValidationTest {
private SignupValidation sv;
private User u;
@Before
public void setUp() throws Exception {
sv = new SignupValidation();
}
@After
public void tearDown() throws Exception {
sv = null;
}
@Test
public void testPassedValidation() {
User u = new User();
u.setEmailAddress( "xyz@gmail.com" );
u.setFirstName( "Chris" );
u.setAge( 20 );
try {
sv.validate( u );
} catch (SignupException e) {
fail( e.getMessage() );
}
}
@Test (expected = SignupException.class)
public void testFailedValidation() throws SignupException {
User u = new User();
u.setEmailAddress( "xyz@gmail.com" );
u.setFirstName( "Chris" );
u.setAge( 16 );
sv.validate( u );
}
@Test
public void validateValidFirstName() {
try {
sv.validateFirstName( "Chris" );
} catch (SignupException e) {
fail( e.getMessage() );
}
}
@Test (expected = SignupException.class)
public void validateInvalidFirstName() throws SignupException {
sv.validateFirstName( "" );
sv.validateFirstName( " " );
}
@Test
public void validateValidEmailAddress() {
try {
sv.validateEmailAddress( "xyz@gmail.com" );
} catch (SignupException e) {
fail( e.getMessage() );
}
}
@Test (expected = SignupException.class)
public void validateInvalidEmailAddress() throws SignupException {
sv.validateEmailAddress( "xyz@gmail." );
sv.validateEmailAddress( "xyzgmail.com" );
sv.validateEmailAddress( "gmail.com" );
sv.validateEmailAddress( "gmail" );
sv.validateEmailAddress( "@gmail.com" );
}
@Test
public void validateAgeGreaterThan17() {
try {
sv.validateAge( 18 );
} catch (SignupException e) {
fail( e.getMessage() );
}
}
@Test (expected = SignupException.class)
public void validateAgeLessThan18() throws SignupException {
sv.validateAge( 17 );
}
}
Class SignupException
public class SignupException extends Exception {
public SignupException( String message ) {
super( message );
}
}
Last updated: 25th Jan, 2025 Have you ever wondered how to seamlessly integrate the vast…
Hey there! As I venture into building agentic MEAN apps with LangChain.js, I wanted to…
Software-as-a-Service (SaaS) providers have long relied on traditional chatbot solutions like AWS Lex and Google…
Retrieval-Augmented Generation (RAG) is an innovative generative AI method that combines retrieval-based search with large…
The combination of Retrieval-Augmented Generation (RAG) and powerful language models enables the development of sophisticated…
Have you ever wondered how to use OpenAI APIs to create custom chatbots? With advancements…