Maven项目和Junit – 检查银行账号
所有的应用程序无论大小都需要经历一系列的过程,包括构建、生成、编译和运行源代码。这些一系列的过程由程序员手动执行。然而,随着Apache的Maven项目的推出,所有这些一系列的过程都可以自动化,避免手动工作。因此,maven项目是一个开源工具,用于一次构建和部署多个项目,提供更好的项目管理。
在本文中,我们将讨论Maven项目,以检查银行账号是否有效,并使用Junit进行测试。
Junit是什么
JUnit是世界各地组织广泛使用的用于Java语言的开源单元测试框架。在Java语言中,每次添加新代码时,需要再次执行测试用例来验证功能性,而这一功能由Junit框架实现。它用于编写和执行Java语言的自动化测试用例。
检查银行账号的Maven项目
在处理银行软件或相关应用程序时,一个强制的要求是对账号进行验证。要使账号有效,需要满足三个条件。
这三个条件如下:
- 银行账号只能包含14位数字。
-
账号中的所有14个数字不能都是零。
-
账号字段不能为空或为null。
现在,让我们在一个Maven项目中编写一个业务逻辑,满足所有这三个条件。
步骤
- 第一步 - 首先创建一个名为 BankingAccountNoServices的文件夹,其中包含Java文件BankingAccountNoServices.java用于编写业务逻辑,以及测试业务逻辑的TestBankingAccountNoServices.java文件。
-
第二步 - 创建另一个名为pom.xml的文件,它是一个包含Maven项目的项目和配置详情的xml文件。
-
第三步 - 实现积极结果的关键因素是将相关的项目和配置信息记录在pom.xml文件中。
-
第四步 - 通过满足账号验证所需的所有必要条件来编写业务逻辑。
-
第五步 - 在TestBankingAccountNoServices.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中的正则表达式来编写业务逻辑。
方法1:使用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;
}
}
}
在上面的代码中,我们首先检查账户号码是否为空或为空串,其次,我们检查账户号码的长度是否为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;
}
}
}
在上面的代码中,我们首先检查账号编号是否为空。其次,我们还检查了账号编号的长度是否为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"));
}
}
在上面的代码中,我们检查了四个不同的单元测试用例来验证一个账号号码。
方法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中用于验证银行帐号号码。