# junit4 **Repository Path**: xi-jun/junit4 ## Basic Information - **Project Name**: junit4 - **Description**: 测试学习 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-10-11 - **Last Updated**: 2024-10-11 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Junit4 Junit4官网: #### 1.介绍 该模块是针对JUnit 4的学习。 测试在开发中占据着重要地位,测试用例尽可能的覆盖业务场景,帮助实现功能自测。 同时项目随着时间的腐化,重构的过程需要完善的测试用例来保障重构,同时修改代码后避免重复手动测试。 测试需要做到以下几种条件: 1. 测试集尽可能多的覆盖业务场景 2. 测试集可以反复执行 3. 测试需要和代码同时备份以便后续重构时使用 #### 2. Junit学习 ##### 2.1 开始 1. 引入依赖 ``` junit junit test ``` ##### 2.2 基本操作 1. RunWith注解
`org.junit.runner.RunWith` 是JUnit 4的一个注解,它用于指定运行测试用例的JUnit运行器。
该注解的参数是`org.junit.runner.Runner`的子类
例如,如果你想使用JUnit 4的参数化功能,你可以使用@RunWith(Parameterized.class)注解。
这是一个使用@RunWith注解的例子: ``` @RunWith(Parameterized.class) public class MyTest { @Parameters public static Collection data() { return Arrays.asList(new Object[][] { { 1, 1 }, { 2, 2 }, { 3, 3 } }); } private int input; private int expected; public MyTest(int input, int expected) { this.input = input; this.expected = expected; } @Test public void test() { assertEquals(expected, input); } } ``` 在这个例子中,**@RunWith(Parameterized.class)**注解告诉JUnit使用Parameterized运行器来运行测试用例。data()方法返回了一个包含测试数据的集合,每个测试数据都是一个对象数组。MyTest构造函数接收这个对象数组作为参数,然后在test()方法中使用这些参数来运行测试。
2. Before注解
@Before 注解用于标记一个方法,该方法在每个测试方法执行之前运行。用于设置测试环境,比如初始化对象、配置系统属性、准备测试数据等。
``` import org.junit.Before; import org.junit.Test; public class ExampleTest { private Calculator calculator; // 测试方法测试前初始化 calculator @Before public void setUp() { calculator = new Calculator(); } @Test public void testAddition() { int result = calculator.add(1, 2); assertEquals(3, result); } @Test public void testSubtraction() { int result = calculator.subtract(5, 2); assertEquals(3, result); } } ``` 3. Test注解
Test注解用于测试的方法前面 ``` import org.junit.Test; import static org.junit.Assert.assertEquals; public class ExampleTest { @Test public void testAddition() { int result = 1 + 2; assertEquals(3, result); } @Test public void testSubtraction() { int result = 5 - 2; assertEquals(3, result); } } ``` 4. Assertions类
`org.junit.jupiter.api.Assertions`用于验证测试结果是否符合预期的方法 ``` import org.junit.Test; import static org.junit.Assert.*; public class ExampleTest { @Test public void testAddition() { int result = 1 + 2; assertEquals(3, result); } @Test public void testSubtraction() { int result = 5 - 2; assertTrue(result > 0); } @Test public void testNullObject() { Object obj = null; assertNull(obj); } @Test public void testArrayEquals() { int[] expected = {1, 2, 3}; int[] actual = {1, 2, 3}; assertArrayEquals(expected, actual); } @Test public void testSameObject() { Object obj1 = new Object(); Object obj2 = obj1; assertSame(obj1, obj2); } @Test public void testNotSameObject() { Object obj1 = new Object(); Object obj2 = new Object(); assertNotSame(obj1, obj2); } @Test public void testFailure() { fail("This test is expected to fail"); } } ``` 5. Order注解
在 JUnit 5 中,@Order 注解用于指定测试方法的执行顺序。默认情况下,JUnit 5 会按照测试方法在类中的声明顺序来执行测试方法。
案例
``` import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Order; public class ExampleTest { @Test @Order(1) public void testFirst() { System.out.println("First test"); } @Test @Order(2) public void testSecond() { System.out.println("Second test"); } @Test @Order(3) public void testThird() { System.out.println("Third test"); } } ``` ##### 2.3 spring集成junit 添加依赖 ``` org.springframework.boot spring-boot-starter-test test junit junit test ``` 1. SpringJUnit4ClassRunner 运行器
`org.springframework.test.context.junit4.SpringJUnit4ClassRunner`该运行器是Spring框架提供的一个类,它是JUnit的一个运行器,用于在JUnit测试中集成Spring的功能。
SpringJUnit4ClassRunner的主要功能包括:
    a. 管理Spring上下文的生命周期:在测试开始之前初始化Spring上下文,在测试结束后销毁Spring上下文。
    b. 提供依赖注入:在测试类和测试方法中注入Spring管理的bean。
    c. 支持事务管理:在测试方法中支持事务的回滚。
要使用SpringJUnit4ClassRunner,你需要在测试类上使用@RunWith注解,并指定SpringJUnit4ClassRunner作为运行器。例如: ``` @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class MyTest { @Autowired private MyService myService; @Test public void test() { // 测试代码 } } ``` 在这个例子中,@RunWith(SpringJUnit4ClassRunner.class)注解告诉JUnit使用SpringJUnit4ClassRunner运行器来运行测试用例。@ContextConfiguration注解指定了Spring上下文配置文件的位置。@Autowired注解用于注入Spring管理的bean。 ##### 2.4 测试插件 1. maven-surefire-plugin ``` org.apache.maven.plugins maven-surefire-plugin 3.0.0-M5 -Dsun.java.command ``` Maven项目中用于运行测试的插件。
      该插件的主要功能是运行JUnit测试。
      它可以自动找到项目中的测试类并运行它们,从而帮助开发者进行单元测试。
      它可以在构建项目时自动运行测试,从而确保代码的质量和正确性。