【Junit Experiment】Junit 软件质量测试实验--日期格式规范性+字符串格式规范性

简介: 软件质量与测试之Junit对Java代码进行测试实战

 测试方法

       使用Junit和 @Test、@Before、@After、@BeforeClass、@AfterClass用法,并结合黑盒测试方法进行程序测试。

实战题目

       1.实现函数String NewDivision(String innumber),输入一个字符串包含两个数字,用逗号分隔,例如“123,234”,“11.1,2”。对字符串进行解析,返回两个数相除结果。输入数字可以是整数或者小数,并且程序可以对非法输入进行处理和识别,例如输入一个数,没有逗号,输入非数字等。

       2.编写函数,RightDate(String date),date表示日期,包含年月日(个位数月份前加0,如,4月->04),例如“19870411”、“20201211”。RightDate函数用来判别输入日期是否正确,合法输入年份范围[1987,2021]。假定被调试的程序能接受一切符合规定的日期,拒绝所有不符合规定的日期。

题目分析

       对于问题1,需要从字符串的长短和内容进行判断,字符串长短较为容易,而字符串内容可根据题目特点来,即该字符串最大特点为以逗号分隔两个数字,先解决数字问题,数字可分小数或者整数,可用正则表达式进行判断,再解决逗号问题,直接判断字符串中逗号的个数比较der,可通过split(",")后字符串的个数以及字符串的内容是否为null进行解决。最后,从业务要求分析,该题为除法运算,自然还有除数为零这个问题。

       对于问题2,延续第一题的分析方法,从字符串的长短和内容进行判断。内容方面,首先是字符方面,该字符串应当全是整数,用正则较为方便,其次该题是日期,分为年、月、日。将这三者进行排列组合逐步排查,单个排查,自然是从时间范围,双个排查,月和日有着31天的问题,三个排查,有着闰年二月二十九日问题。

代码模块

第一题

package JunitTest1;
import java.util.regex.Pattern;
//1. 实现函数String NewDivision(String innumber),
//        输入一个字符串包含两个数字,用逗号分隔,例如“123,234”,“11.1,2”。对字符串进行解析,
//        返回两个数相除结果。输入数字可以是整数或者小数,
//        并且程序可以对非法输入进行处理和识别,
//        例如输入一个数,没有逗号,输入非数字等。
public class NewDivision {
    public String division(String s){
        Pattern pattern1 = Pattern.compile("^[-\\+]?[\\d]*$");
        Pattern pattern2 = Pattern.compile("^[-\\+]?\\d*[.]\\d+$");
        String f[]=s.split(",");
        if(f.length!=2){return "字符输入错误";}
        if (f[0]==null||"".equals(f[0])||f[1]==null||"".equals(f[1]))
        {
            return "字符输入错误";
        }
        if(!((pattern1.matcher(f[0]).matches()||pattern2.matcher(f[1]).matches())&&
                (pattern1.matcher(f[1]).matches()||pattern2.matcher(f[1]).matches())))
        {
            return "字符输入错误";
        }
        double d1=Double.parseDouble(f[0]);
        double d2=Double.parseDouble(f[1]);
        System.out.println(d2);
        if(d2==0.0||d2==0){return "除数为零";}
        return String.valueOf(d1/d2);
    }
}

image.gif

package JunitTest1;
import static org.junit.Assert.*;
import org.junit.*;
public class TestNewDivision {
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        System.out.println("this is beforeclass");
    }
    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        System.out.println("this is afterclass");
    }
    @Before
    public void setUp() throws Exception {
        System.out.println("this is before");
    }
    @After
    public void tearDown() throws Exception {
        System.out.println("this is after");
    }
    @Test
    public void testdivision1() {
        assertEquals("字符输入错误", new NewDivision().division("256"));
        System.out.println("测试成功!");
    }
    @Test
    public void testdivision2() {
        assertEquals("字符输入错误", new NewDivision().division("854,"));
        System.out.println("测试成功!");
    }
    @Test
    public void testdivision3() {
        assertEquals("字符输入错误", new NewDivision().division(",458.5"));
        System.out.println("测试成功!");
    }
    @Test
    public void testdivision4() {
        assertEquals("字符输入错误", new NewDivision().division("452.4,451,42"));
        System.out.println("测试成功!");
    }
    @Test
    public void testdivision5() {
        assertEquals("字符输入错误", new NewDivision().division("452.4,452.0.0"));
        System.out.println("测试成功!");
    }
    @Test
    public void testdivision6() {
        assertEquals("除数为零", new NewDivision().division("2.0,0.0"));
        System.out.println("测试成功!");
    }
    @Test
    public void testdivision7() {
        assertEquals("0.0", new NewDivision().division("0,885.5"));
        System.out.println("测试成功!");
    }
}

image.gif

第二题

package JunitTest1;
import java.util.regex.Pattern;
//2. 编写函数,RightDate(String date),date表示日期,
//        包含年月日(个位数月份前加0,如,4月->04),
//        例如“19870411”、“20201211”。
//        RightDate函数用来判别输入日期是否正确,合法输入年份范围[1987,2021]。
//        假定被调试的程序能接受一切符合规定的日期,拒绝所有不符合规定的日期。
public class RightDate {
    public boolean runnian(int x){
        if ((x%4==0 && x%100!=0)||(x%400==0))
            return true;
        else
            return false;
    }
    public String rightDate(String s){
        Pattern pattern = Pattern.compile("[0-9]*");
        if(s.length()!=8||(!pattern.matcher(s).matches())){
            return "日期格式不规范--请检查日期符号";
        }
        int year=Integer.parseInt(s.substring(0,4));
        int month=Integer.parseInt(s.substring(4,6));
        int day=Integer.parseInt(s.substring(6,8));
        if(year<1987||year>2020)
        {
            return "日期格式不规范--请检查年范围";
        }
        if (month>12||month==0){
            return "日期格式不规范--请检查月范围";
        }
        if ((day>=31||day==0)||((month==4||month==6||month==9||month==11)&&(day==31))){
            return "日期格式不规范--请检查日范围";
        }
        if(month==2&&day==29&&(!runnian(year))){
            return "日期格式不规范--请检查是否是闰年";}
        return "日期规范!";
    }
    public static void main(String argc[]){
        RightDate r1=new RightDate();
        System.out.println("20201310".substring(0,4));
        System.out.println(r1.rightDate("20201310"));
    }
}

image.gif

package JunitTest1;
import static org.junit.Assert.*;
import org.junit.*;
public class TestRigthDate {
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        System.out.println("this is beforeclass");
    }
    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        System.out.println("this is afterclass");
    }
    @Before
    public void setUp() throws Exception {
        System.out.println("this is before");
    }
    @After
    public void tearDown() throws Exception {
        System.out.println("this is after");
    }
    @Test
    public void testrightDay1() {
        assertEquals("日期格式不规范--请检查日期符号",new RightDate().rightDate("202012120000000"));
        System.out.println("测试成功!");
    }
    @Test
    public void testrightDay2() {
        assertEquals("日期格式不规范--请检查年范围",new RightDate().rightDate("20211210"));
        System.out.println("测试成功!");
    }
    @Test
    public void testrightDay3() {
        assertEquals("日期格式不规范--请检查月范围",new RightDate().rightDate("20201310"));
        System.out.println("测试成功!");
    }
    @Test
    public void testrightDay4() {
        assertEquals("日期格式不规范--请检查月范围",new RightDate().rightDate("20201431"));
        System.out.println("测试成功!");
    }
    @Test
    public void testrightDay5() {
        assertEquals("日期格式不规范--请检查日范围",new RightDate().rightDate("20200432"));
        System.out.println("测试成功!");
    }
    @Test
    public void testrightDay6() {
        assertEquals("日期格式不规范--请检查是否是闰年",new RightDate().rightDate("19950229"));
        System.out.println("测试成功!");
    }
    @Test
    public void testrightDay7() {
        assertEquals("日期格式不规范--请检查日期符号",new RightDate().rightDate("2020****"));
        System.out.println("测试成功!");
    }
    @Test
    public void testrightDay8() {
        assertEquals("日期规范!",new RightDate().rightDate("20201212"));
        System.out.println("测试成功!");
    }
}

image.gif

测试结果

image.gif编辑

image.gif编辑

目录
相关文章
|
11月前
|
人工智能 安全 网络安全
Burp Suite Professional 2025.5 for macOS x64 & ARM64 - 领先的 Web 渗透测试软件
Burp Suite Professional 2025.5 for macOS x64 & ARM64 - 领先的 Web 渗透测试软件
471 3
|
10月前
|
人工智能 Java 测试技术
Spring Boot 集成 JUnit 单元测试
本文介绍了在Spring Boot中使用JUnit 5进行单元测试的常用方法与技巧,包括添加依赖、编写测试类、使用@SpringBootTest参数、自动装配测试模块(如JSON、MVC、WebFlux、JDBC等),以及@MockBean和@SpyBean的应用。内容实用,适合Java开发者参考学习。
1090 0
|
6月前
|
Java 测试技术 网络安全
Burp Suite Professional 2025.10 for Windows x64 - 领先的 Web 渗透测试软件
Burp Suite Professional 2025.10 for Windows x64 - 领先的 Web 渗透测试软件
278 0
Burp Suite Professional 2025.10 for Windows x64 - 领先的 Web 渗透测试软件
|
6月前
|
Java 测试技术 数据库连接
【SpringBoot(四)】还不懂文件上传?JUnit使用?本文带你了解SpringBoot的文件上传、异常处理、组件注入等知识!并且带你领悟JUnit单元测试的使用!
Spring专栏第四章,本文带你上手 SpringBoot 的文件上传、异常处理、组件注入等功能 并且为你演示Junit5的基础上手体验
1066 3
|
11月前
|
安全 测试技术 Linux
Flawnter 5.9.1 (macOS, Linux, Windows) - 应用程序安全测试软件
Flawnter 5.9.1 (macOS, Linux, Windows) - 应用程序安全测试软件
346 2
Flawnter 5.9.1 (macOS, Linux, Windows) - 应用程序安全测试软件
|
11月前
|
人工智能 安全 网络安全
Burp Suite Professional 2025.5 for Windows x64 - 领先的 Web 渗透测试软件
Burp Suite Professional 2025.5 for Windows x64 - 领先的 Web 渗透测试软件
535 4
Burp Suite Professional 2025.5 for Windows x64 - 领先的 Web 渗透测试软件
|
9月前
|
人工智能 物联网 测试技术
智能化测试基础架构:软件质量保障的新纪元
本文介绍了智能化测试基础架构的核心构成与优势。该架构融合AI、领域工程与自动化技术,包含智能测试平台、测试智能体、赋能引擎和自动化工具链四部分,能自动生成用例、调度执行、分析结果,显著提升测试效率与覆盖率。其核心优势在于实现专家经验规模化、质量前移和快速适应业务变化,助力企业构建新一代质量保障体系。建议从构建知识图谱和试点关键领域智能体起步,逐步推进测试智能化转型。
|
10月前
|
测试技术
软考软件测评师大题——案例分析之白盒测试
历年下午案例试题一固定考察白盒测试,主要包含三大核心问题:推导逻辑条件、绘制控制流图及计算环路复杂度、确定线性无关路径集合。内容涵盖覆盖层级标准(语句、分支、判定、条件覆盖等)、控制流图构建规范(顺序、分支、循环结构转换原则)、环路复杂度计算公式以及线性无关路径生成方法。通过典型题型示例解析,如代码路径分析与验证指标,帮助考生掌握解题思路和技巧。
|
10月前
|
测试技术
软考软件评测师——可靠性测试测试方法
软件可靠性是指软件在规定条件和时间内完成预定功能的能力,受运行环境、软件规模、内部结构、开发方法及可靠性投入等因素影响。失效概率指软件运行中出现失效的可能性,可靠度为不发生失效的概率,平均无失效时间(MTTF)体现软件可靠程度。案例分析显示,嵌入式软件需满足高可靠性要求,如机载软件的可靠度需达99.99%以上,通过定量指标评估其是否达标。