The quality of the software is very very important and it can be enhanced by JUNIT test cases. There are a lot of useful methods available for validating user input. Any login web application has two fields namely EmailId and password. In this article, let us see how to check the validity of email and password as well as test the same using JUNIT. We are going to cover them as a Maven project.
Example Project
Project Structure:
Maven project and hence let us add dependencies in
pom.xml
XML
<? xml version = "1.0" encoding = "UTF-8" ?> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 < modelVersion >4.0.0</ modelVersion > < groupId >com.gfg.loginformsamples</ groupId > < artifactId >LoginFormServices</ artifactId > < packaging >jar</ packaging > < version >1.0-SNAPSHOT</ version > < properties > < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding > < maven.compiler.source >1.8</ maven.compiler.source > < maven.compiler.target >1.8</ maven.compiler.target > < junit.version >5.3.1</ junit.version > < pitest.version >1.4.3</ pitest.version > </ properties > < dependencies > <!-- junit 5, unit test --> < dependency > < groupId >org.junit.jupiter</ groupId > < artifactId >junit-jupiter-engine</ artifactId > < version >${junit.version}</ version > < scope >test</ scope > </ dependency > </ dependencies > < build > < finalName >NumberUtilityServices</ finalName > < plugins > < plugin > < groupId >org.apache.maven.plugins</ groupId > < artifactId >maven-surefire-plugin</ artifactId > < version >3.0.0-M1</ version > </ plugin > < plugin > < groupId >org.pitest</ groupId > < artifactId >pitest-maven</ artifactId > < version >${pitest.version}</ version > < executions > < execution > < id >pit-report</ id > < phase >test</ phase > < goals > < goal >mutationCoverage</ goal > </ goals > </ execution > </ executions > <!-- Need this to support JUnit 5 --> < dependencies > < dependency > < groupId >org.pitest</ groupId > < artifactId >pitest-junit5-plugin</ artifactId > < version >0.8</ version > </ dependency > </ dependencies > < configuration > < targetClasses > < param >com.gfg.loginformsamples.*LoginformSampleService*</ param > </ targetClasses > < targetTests > < param >com.gfg.loginformsamples.*</ param > </ targetTests > </ configuration > </ plugin > </ plugins > </ build > </ project > |
Let us see the validation for email and password first via
LoginformSampleService.java
For passwords, according to our business case, we need to set the condition. Here as given in the code, followed it.
Java
import java.util.regex.Matcher; import java.util.regex.Pattern; public class LoginformSampleService { public boolean checkValidEmail(String emailId) { String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\." + "[a-zA-Z0-9_+&*-]+)*@" + "(?:[a-zA-Z0-9-]+\\.)+[a-z" + "A-Z]{2,7}$" ; Pattern pat = Pattern.compile(emailRegex); if (emailId == null ) return false ; return pat.matcher(emailId).matches(); } public boolean checkValidPassword(String password) { String regex = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{10,20}$" ; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(password); return matcher.matches(); } } |
Testcases
TestLoginFormSampleService.java
Java
import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assumptions.assumeFalse; import static org.junit.jupiter.api.Assumptions.assumingThat; import org.junit.jupiter.api.Test; public class TestLoginFormSampleService { @Test public void testForValidEmailIds() { LoginformSampleService loginFormSampleService = new LoginformSampleService(); assertEquals( true , loginFormSampleService.checkValidEmail( "xxx@gmail.com" )); assertNotEquals( true , loginFormSampleService.checkValidEmail( "x@gmailcom" )); assertEquals( true , loginFormSampleService.checkValidEmail( "abcd12@gfg.com" )); assertTrue(loginFormSampleService.checkValidEmail( "abcd12@gfg.com" )); assertFalse(loginFormSampleService.checkValidEmail( "13ab@A" ), "Invalid EmailId" ); String formName = "login" ; // In a grouped assertion all assertions // are executed, and any failures will // be reported together. assertAll( "loginformvpositivevalidation" , () -> assertTrue(loginFormSampleService.checkValidEmail( "abcd12@gfg.com" )), () -> assertTrue(loginFormSampleService.checkValidPassword( "12345Abc@d" ))); // Let us comment for the first time //assertAll("loginformvnegativevalidation", () -> assertTrue(loginFormSampleService.checkValidEmail("abcd12@gfg.com")), // () -> assertFalse(loginFormSampleService.checkValidPassword("12345Abc@d"))); // The assumingThat() method executes the rest of the statements // if the assumption is valid. If the assumption is invalid, // this method does nothing. // Advantage : To log the information assumingThat( "login" .equals(formName), () -> { System.out.println( "Checking in the login page given email id abc@gmail.com is valid!!!" ); assertEquals( true , loginFormSampleService.checkValidEmail( "abc@gmail.com" )); }); // with assumeFalse // If the boolean condition in assumeFalse becomes // false then only the next set of test method is executed, // else the test is skipped assumeFalse( "register" .equals(formName)); assertTrue(loginFormSampleService.checkValidEmail( "zyxw12@gfg.com" )); } @Test public void testForValidPassword() { LoginformSampleService loginFormSampleService = new LoginformSampleService(); assertEquals( true , loginFormSampleService.checkValidPassword( "12345Abc@d" )); assertNotEquals( true , loginFormSampleService.checkValidPassword( "13ab@A" )); assertEquals( true , loginFormSampleService.checkValidPassword( "ab12Ab13@#" )); assertTrue(loginFormSampleService.checkValidPassword( "12345Abc@d" )); assertFalse(loginFormSampleService.checkValidPassword( "13ab@A" ), "Invalid Password" ); } } |
Explanation
Let us check out for assertAll, assumingThat, and assumeFalse in detail
assertAll: In a grouped assertion all assertions are executed, and any failures will be reported together. Statement 1: As both asserts are true, overall assert is passed assertAll("loginformvpositivevalidation", () -> assertTrue(loginFormSampleService.checkValidEmail("abcd12@gfg.com")), () -> assertTrue(loginFormSampleService.checkValidPassword("12345Abc@d"))); Statement 2: Here second assertFalse yields true and hence it is logically false and because of this overall assert is failed Note: For the first time this is commented assertAll("loginformvnegativevalidation", () -> assertTrue(loginFormSampleService.checkValidEmail("abcd12@gfg.com")), () -> assertFalse(loginFormSampleService.checkValidPassword("12345Abc@d"))); assumingThat: The assumingThat() method executes the rest of the available statements only if the assumption is valid. If the assumption is invalid, this method does nothing. Advantage : To log the information and can find out the rootcause of the error Statement 3: assumingThat("login".equals(formName), () -> { System.out.println("Checking in the login page given email id abc@gmail.com is valid!!!"); assertEquals(true, loginFormSampleService.checkValidEmail("abc@gmail.com")); }); assumeFalse: If the boolean condition in assumeFalse becomes false then only the next set of test method is executed, else the test is skipped. Statement 4: assumeFalse("register".equals(formName)); assertTrue(loginFormSampleService.checkValidEmail("zyxw12@gfg.com"));
On executing the above, we will get as follows
But at the same time as said earlier, if we uncomment the lines
assertAll("loginformvnegativevalidation", () -> assertTrue(loginFormSampleService.checkValidEmail("abcd12@gfg.com")), () -> assertFalse(loginFormSampleService.checkValidPassword("12345Abc@d")));
assertFalse(loginFormSampleService.checkValidPassword(“12345Abc@d”)) yields the result as true and hence overall condition gets false and hence we can see the below screenshot