Home > Java > javaTutorial > body text

Maven project using Junit - Check bank account number

王林
Release: 2023-08-29 14:33:02
forward
1431 people have browsed it

使用Junit的Maven项目 - 检查银行账号

所有的应用程序,无论大小,都需要经历源代码的构建、生成、编译和运行的一系列过程。这组过程由程序员手动执行。然而,随着 Apache Maven 项目的启动,所有这些流程都可以自动化,避免手动工作。因此,maven项目是一个开源工具,用于一次构建和部署多个项目,以提供更好的项目管理。

在本文中,我们将讨论用于检查银行帐号是否有效并使用 Junit 进行测试的 Maven 项目。

Junit 是什么?

JUnit 是一个开源单元测试框架,被世界各地的 Java 语言组织使用。在Java语言中,每次添加新的代码,都需要重新执行测试用例,这样的功能是由Junit框架来实现的。它用于用Java语言编写和执行自动化测试用例。

用于检查银行帐号的 Maven 项目

每当我们处理银行软件或相关应用程序时,一件强制性的事情就是验证帐号。要使帐号有效,需要满足三个条件。

三个条件如下 -

  • 银行帐号只能包含 14 位数字。

  • 帐号中的 14 位数字不能全部为零。

  • 帐号字段不能为空或为空。

现在,让我们在 Maven 项目中编写满足所有这三个条件的业务逻辑。

算法

  • 步骤 1 - 首先创建一个文件夹 BankingAccountNoServices,其中包含名为 BankingAccountNoServices.java 的 Java 文件,用于编写业务逻辑,第二个 TestBankingAccountNoServices.java 用于测试业务逻辑。

  • 步骤 2 - 创建另一个文件 pom.xml,它是一个 xml 文件,包含 Maven 项目的项目和配置详细信息。

  • 第 3 步 - 取得积极成果的关键因素是在 pom.xml 文件中记录相关项目和配置信息。

  • 第 4 步 - 通过满足验证帐号所需的所有必要条件来编写业务逻辑。

  • 步骤 5 - 在 Test BankingAccountNoServices.java 文件中使用 Junit 编写单元测试用例。

在继续操作之前应检查 pom.xml 文件的内容。它在讨论的所有方法中保持一致,并包含 Maven 项目的重要配置详细信息。

示例

<?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>
Copy after login

上面的pom.xml代码包含我们的maven项目所需的所有项目和配置详细信息。

方法

  • 方法 1 - 在这种方法中,我们将看到使用 Long.parseLong 的业务逻辑。

  • 方法 2 - 在这种方法中,我们将使用 Character.isDigit() 函数编写业务逻辑。

  • 方法 3 - 在这种方法中,我们将使用 Java 中的正则表达式编写业务逻辑。

方法一:使用Long.parseLong

由于帐号应该是14位数字,因此我们使用Long.parseLong函数将其转换为long类型,然后检查三个必要条件。

示例

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;
      }
   }
}
Copy after login

在上面的代码中,我们首先检查帐号是否为 null 或空,其次检查帐号的长度是否为 14,然后计算其中零的数量。如果 14 位数字全部为零则返回 false,否则返回 true。

现在,让我们看看使用 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"));
   }
}
Copy after login

在上面的代码中,我们检查了 4 个不同的单元测试用例来验证帐号。

方法2:使用Character.isDigit()

在这种方法中,我们将使用Character.isDigit()函数检查帐号。我们将检查所有三个必要条件来验证帐号。

示例

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;
      }
   }
}
Copy after login

在上面的代码中,我们首先检查帐号是否为 null 或空,其次检查帐号的长度是否为 14,然后检查 accNo 变量的字符是否为数字或不是。第三,检查数字中是否存在零。

现在,让我们看看使用 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"));
   }
}
Copy after login

在上面的代码中,我们检查了 4 个不同的单元测试用例来验证帐号。

方法 3:使用正则表达式模式

在这种方法中,我们为数字定义正则表达式模式,并检查帐号验证的所有三个必要条件。

示例

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;
      }
   }
}
Copy after login

在上面的代码中,我们首先检查帐号是否为 null 或空,其次检查帐号的长度是否为 14,然后定义一个数字的正则表达式并检查这三个值使用 Pattern 和 Matcher 类的必要条件。

现在,让我们看看使用 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"));
   }
}
Copy after login

在上面的代码中,我们检查了 4 个不同的单元测试用例来验证帐号。

in conclusion

In this article, we use Junit to create a Maven project specifically for checking bank account numbers. We discussed three different ways to write business logic for validating bank account numbers, namely using Long.parseLong, Character.isDigit() and using regular expression patterns. Any of them can be used to perform verification of bank account numbers in Java.

The above is the detailed content of Maven project using Junit - Check bank account number. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!