目录
Junit 是什么?
用于检查银行帐号的 Maven 项目
算法
示例
方法
方法一:使用Long.parseLong
方法2:使用Character.isDigit()
方法 3:使用正则表达式模式
结论
首页 Java java教程 使用Junit的Maven项目 - 检查银行账号

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

Aug 29, 2023 pm 02:33 PM

使用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>
登录后复制

上面的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;
      }
   }
}
登录后复制

在上面的代码中,我们首先检查帐号是否为 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"));
   }
}
登录后复制

在上面的代码中,我们检查了 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;
      }
   }
}
登录后复制

在上面的代码中,我们首先检查帐号是否为 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"));
   }
}
登录后复制

在上面的代码中,我们检查了 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;
      }
   }
}
登录后复制

在上面的代码中,我们首先检查帐号是否为 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"));
   }
}
登录后复制

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

结论

在本文中,我们使用 Junit 创建了一个 Maven 项目,专门用于检查银行帐号。我们讨论了三种不同的方法来编写验证银行帐号的业务逻辑,即使用 Long.parseLong、Character.isDigit() 和使用正则表达式模式。它们中的任何一个都可用于执行 Java 中银行帐号的验证。

以上是使用Junit的Maven项目 - 检查银行账号的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
威尔R.E.P.O.有交叉游戏吗?
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)