Maven Project with Junit – Checking for a Bank Account Number


All the applications whether small or large are required to undergo a set of processes building, generating, compiling and running of the source code. These set of processes are manually performed by the programmers. However, with the launch of the Apache’s Maven Project, all these sets of processes can be automated and the manual work can be avoided. Therefore, the maven project is an open source tool that is used to build and deploy several projects at once for providing better project management.

In this article, we will be discussing the Maven Project for checking a bank account number whether it is valid or not and testing it with Junit.

What is Junit?

JUnit is an open source unit testing framework used worldwide by organizations for Java language. In Java language, every time a new code is added, test cases are required to be again executed and such functionality is achieved by the Junit framework. It is used to write and execute automated test cases in Java language.

Maven Project for checking a bank account number

Whenever we are dealing with banking softwares or related applications, one mandatory thing is the validation of an account number. For an account number to be valid, three conditions need to be fulfilled.

The three conditions are as follows −

  • The bank account number should contain only 14 digits.

  • All the 14 digits in an account number should not be zero.

  • The account number field cannot be empty or null.

Now, let us write a business logic in a maven project in which all these three conditions are satisfied.

Algorithm

  • Step 1 − First make a folder BankingAccountNoServices which contains the Java files named BankingAccountNoServices.java for writing the business logic and second TestBankingAccountNoServices.java for testing the business logic.

  • Step 2 − Make another file pom.xml which is an xml file and contains the project and configuration details for a maven project.

  • Step 3 − A key factor in achieving positive outcomes is recording the relevant project and configuration information within the pom.xml file.

  • Step 4 − Write the business logic by fulfilling all the necessary conditions required for the validation of an account number.

  • Step 5 − Write the unit test cases using Junit in the Test BankingAccountNoServices.java file.

The contents of the pom.xml file should be reviewed before proceeding further. It remains consistent throughout all approaches discussed and includes important configuration details for the maven project.

Example

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
                              http://maven.apache.org/xsd/maven-4.0.0.xsd">
  
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.example.BankingAccountNoServices </groupId>
   <artifactId>BankingAccountNoServices </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>
      <dependency>
         <groupId>org.junit.jupiter</groupId>
         <artifactId>junit-jupiter-engine</artifactId>
         <version>${junit.version}</version>
         <scope>test</scope>
      </dependency>
   </dependencies>
   <build>
      <finalName>maven-mutation-testing</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>
  
            <dependencies>
               <dependency>
                  <groupId>org.pitest</groupId>
                  <artifactId>pitest-junit5-plugin</artifactId>
                  <version>0.8</version>
               </dependency>
            </dependencies>
            <configuration>
               <targetClasses>
                  <param>com.example.BankingAccountNoServices.* BankingAccountNoServices *</param>
               </targetClasses>
               <targetTests>
                  <param>com.example.BankingAccountNoServices.*</param>
               </targetTests>
            </configuration>
         </plugin>
  
      </plugins>
   </build>
  
</project>

The above pom.xml code contains all the project and configuration details needed for our maven project.

Approach

  • Approach 1 − In this approach, we will be seeing the business logic using the Long.parseLong.

  • Approach 2 − In this approach, we will be writing the business logic using the Character.isDigit() function.

  • Approach 3 − In this approach, we will be writing the business logic using the Regular expressions in Java.

Approach 1: Using Long.parseLong

As the account number should be of 14 digits, therefore, we are using the Long.parseLong function to convert it into a long type and then checking the three necessary conditions.

Example

import java.util.*;  
public class BankingAccountNoServices {
   public boolean isValid1(String accNo) {
      if (accNo == null || accNo.equalsIgnoreCase("")) {
         return false; 
      }
      try {
         Long.parseLong(accNo); 
         if (accNo.length() == 14) {
            int c = 0;
            int n = accNo.length();
            for (int i = 0; i < n; i++) {
               if (accNo.charAt(i) == '0') {
                  c += 1;
               }
            }
            if (c == 14) { 
               return false;
            } else {
               return true;
            }
         } else {
            return false;
         }
      }
      catch (NumberFormatException exception) {
         return false;
      }
   }
}

In the above code, we have first checked if the account number is null or empty or not, secondly, we have checked if the length of the account number is 14 then counted the number of zeroes in it. If all the 14 digits are zero then returned false, otherwise true.

Now, let us see the unit test cases using JUnit.

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; 
public class TestBankingAccountNoServices {
   public void testForBankAccountNo() {
      BankingAccountNoServices ob = new BankingAccountNoServices();
      assertEquals(false, ob.isValid1(null));
      assertEquals(false, ob.isValid1("8378939"));
      assertEquals(true, ob.isValid1("67874864837684"));
      assertEquals(true, ob.isValid1("23451234543214"));
   }
}

In the above code, we have checked for 4 different unit test cases for validation of an account number.

Approach 2: Using Character.isDigit()

In this approach, we will check the account number using the Character.isDigit() function. We will be checking all the three necessary conditions to validate the account number.

Example

import java.util.*;  
public class BankingAccountNoServices {
   public boolean isValid2(String accNo){
      if (accNo == null || accNo.equalsIgnoreCase("")) {
         return false; 
      }
      if (accNo.length() == 14) {
         int c = 0;
         for (int i = 0; i < accNo.length(); i++) {
            if (!Character.isDigit(accNo.charAt(i))) {
               return false;
            }
            if (accNo.charAt(i) == '0') {
               c += 1;
            }
         }
         if (c == 14) {
            return false;
         } else {
            return true;
         }
      } else {
         return false;
      }
   }
}

In the above code, we have first checked if the account number is null or empty or not, secondly, we have checked if the length of the account number is 14 then checked if the characters of the accNo variable are a digit or not. And thirdly, checked for the presence of zero in the number.

Now, let us see the unit test cases using Junit.

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; 
public class TestBankingAccountNoServices {
   public void testForBankAccountNo() {
      BankingAccountNoServices ob = new BankingAccountNoServices();
      assertEquals(false, ob.isValid2(""));
      assertEquals(false, ob.isValid2("00000000000000"));
      assertEquals(true, ob.isValid2("67874864837684"));
      assertEquals(true, ob.isValid2("34324353488345"));
   }
}

In the above code, we have checked for 4 different unit test cases for validation of an account number.

Approach 3: Using regex pattern

In this approach, we are defining a regex pattern for a digit and checking all the three necessary conditions for the validation of the account number.

Example

import java.util.regex.Matcher;
import java.util.regex.Pattern;
  
public class BankingAccountNoServices {
   public boolean isValid3(String accNo) {
      if (accNo == null || accNo.equalsIgnoreCase("")) {
         return false; 
      }
      if (accNo.length() == 14) {
         int c = 0;
         String r = "[0-9]+"; 
         Pattern p = Pattern.compile(r);
         Matcher matcher = p.matcher(accNo);
         if (matcher.matches()) { 
            for (int i = 0; i < accNo.length(); i++) {
               if (accNo.charAt(i) == '0') {
                  c += 1;
               }
            }
            if (c == 14) { 
               return false;
            } else {
               return true;
            }
         } else {
            return false;
         }
      } else {
         return false;
      }
   }
}

In the above code, we have first checked if the account number is null or empty or not, secondly, we have checked if the length of the account number is 14 then defined a regular expression for a number and checked for the three necessary conditions using Pattern and Matcher classes.

Now, let us see the unit test cases using JUnit.

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; 
public class TestBankingAccountNoServices {
   public void testForBankAccountNo() {
      BankingAccountNoServices ob = new BankingAccountNoServices();
      assertEquals(false, ob.isValid3("47283"));
      assertEquals(false, ob.isValid3("19037293284s32"));
      assertEquals(true, ob.isValid3("67874864837684"));
      assertEquals(true, ob.isValid3("34521678954632"));
   }
}

In the above code, we have checked for 4 different unit test cases for validation of an account number.

Conclusion

In this article, we have created a maven project using Junit specifically for checking a bank account number. We have discussed three different approaches for writing the business logic for validating the bank account number, using Long.parseLong, Character.isDigit() and using regex patterns. Any of them could be used to perform the validation of a bank account number in Java.

Updated on: 28-Jul-2023

56 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements