Junit——白盒测试

Java单元测试框架,主要用于测试Java程序中的各个单元。

1.验证代码功能是否符合预期

2.及时 发现+修复 代码中的缺陷,提高代码质量

入门

最早学习java,代码对不对,通过main 方法运行,观看结果是否符合预期。

java 复制代码
package hello;

public class ArithTestUtil{

	public static void main(String[] args) {
		int result = add(1,2);
		System.out.println(result);
	}
  // 重载加法方法 - 两个整数加法  A
	public static int add(int i, int j) {
		return i+j;
	}
	   // 重载加法方法 - 两个浮点数加法   B
    public static double add(double a, double b) {
        return a + b;
    }

    // 重载加法方法 - 三个整数加法  C
    public static int add(int a, int b, int c) {
        return a + b + c;
    }
}

但是问题来了,我的add方法 A 参数l两个,以后需要浮点方法B ,三个参数方法C ,怎么测试,

还是要写到main方法中,以后很多方法情况下 结果对不对要一一对应查看,肉眼久了不废了吗。这时候 使用junit 测试框架来进行测试工作

任何开发都是先导包 junit-4.9.jar【以后学到springboot才能解脱】

java 复制代码
package hello;

import org.junit.Test;

import junit.framework.Assert;

public class ArtichTestUtilTest{

    @Test
    public void testAdd() {
    	int result = ArithTestUtil.add(1, 2);
    	Assert.assertEquals(result, 3);
     }
    @Test
    public void testAdd2() {
    	int result = ArithTestUtil.add(1, 2,,3);
    	Assert.assertEquals(result, 5);
    }
    @Test
    public void testAdd3() {
    	doubleresult = ArithTestUtil.add(1.5, 2.3);
    	Assert.assertEquals(result, 3.8);
    }

}

鼠标在方法上,右键运行对应的测试方法。在类上,可运行该类中所有的测试方法。

注意:运行成功显示绿色,失败显示红色 +提示你的代码 类名+ 错误的代码行号

eclipse 类似这样

idea 类似这样 图只是参考,实际测试时 数值 或调用方法随意写的

junit的好处

对比前面所说,测试放到main方法中。用juint后,发现:

1.新增的测试,对原测试不影响

2.此时失败,会变化立马看出来结果\效果

相关注解

  • 测试类 :使用 @Test 注解标记的类。
  • 测试方法 :使用 @Test 注解标记的方法,用于执行具体的测试。
  • 断言 :使用 JUnit 提供的断言方法(如 assertEqualsassertTrue 等)来验证测试结果。

========================================================================

  • @Test:标记测试方法。
  • @Before@After:分别在测试方法之前和之后执行操作。
  • @BeforeEach@AfterEach:分别在每个测试方法之前和之后执行操作。 JUnit 5
  • @BeforeAll@AfterAll:分别在所有测试方法之前和之后执行操作。 JUnit 5
  • @BeforeClass 必须是 static 方法** **/ /只在所有测试方法之前执行一次

会发现有的注解 的作用一样啊。包不一样,列出说明学会一种,另一种方法调用照葫芦画瓢即可。

TestSuite

之前案例中只有一个 测试类 ArtichTestUtilTest。往后多测试类如 Test1,Test2 ...,开发者手动去挨个点击执行 ,比较麻烦。

java 复制代码
//在 JUnit 4 中,使用 @RunWith 和 @Suite.SuiteClasses 来定义测试套件

@RunWith(Suite.class)
@Suite.SuiteClasses({TestClass1.class, TestClass2.class})
public class MyTestSuite {
    // 测试套件运行时,TestClass1 和 TestClass2 中的所有测试都会被执行
  // MyTestSuite 本身不含任何测试方法,测试方法来自于 TestClass1 TestClass2
}



//JUnit 5 中的 Test Suite 用法
@Suite
@SelectClasses({TestClass1.class, TestClass2.class})  // 选择特定的测试类
public class MyTestSuite {
    // 这个类本身不包含任何测试方法,所有的测试都来自于 TestClass1 和 TestClass2
}


@Suite
@SelectPackages("com.hello.tests")  // 选择整个包中的所有测试类
public class MyTestSuite {
    // 不需要测试方法,所有的测试都来自 com.hello.tests 包
}

maven项目使用

对比前面的独立jar包,现在maven里如何使用

1.pom.xml中加上引用包

XML 复制代码
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13.2</version>
    <scope>test</scope>
</dependency>

2.同上

相关推荐
陈天cjq2 天前
Redis 实用型限流与延时队列:从 Lua 固定/滑动窗口到 Streams 消费组(含脚本与压测)
redis·junit·lua
Warren982 天前
Lua 脚本在 Redis 中的应用
java·前端·网络·vue.js·redis·junit·lua
yh云想4 天前
《多级缓存架构设计与实现全解析》
缓存·junit
cui_win5 天前
redis 内存使用率高居高不下,如何分析 key占用情况
数据库·redis·junit·rdb
Volunteer Technology8 天前
Lua基础+Lua数据类型
开发语言·junit·lua
安卓开发者14 天前
Android JUnit 测试框架详解:从基础到高级实践
android·junit·sqlserver
万能小锦鲤16 天前
《软件测试与质量控制》实验报告二 单元测试
junit·eclipse·单元测试·测试用例·实验报告·软件测试与质量控制
健康平安的活着16 天前
junit中@InjectMocks作用详解
junit
健康平安的活着16 天前
junit总@mockbaen与@mock的区别与联系
junit
Savvy..19 天前
Day05 Maven
java·junit·maven·注解