浅解 JUnit 4 第十二篇:如何生成 @Before 注解的替代品?(上)

背景

浅解 JUnit 4 第十一篇:@Before 注解和 @After 注解如何发挥作用? 一文中,我们初步探讨了 @Before 注解 和@After 注解是如何发挥作用的 。那么我们能否自己实现类似 @Before 注解的功能呢?(@After 注解和 @Before 注解的功能是对称的,如果能实现类似 @Before 注解的功能,那么应该也能实现类似 @After 注解的功能)

这个问题涉及的内容比较多,本文只是上篇。

正文

一个具体的例子

我在本地创建了一个小项目来以便探讨本文的问题,这个项目的结构如下 ⬇️

text 复制代码
.
├── pom.xml
└── src
    ├── main
    │   └── java
    │       └── org
    │           └── example
    │               └── SimpleAdder.java
    └── test
        └── java
            └── org
                └── study
                    ├── annotations
                    │   └── MyBefore.java
                    ├── rules
                    │   └── MyBeforeRule.java
                    └── SimpleAdderTest.java

SimpleAdder.java

SimpleAdder.java 的内容如下 ⬇️ 它的功能虽然很简单,但是用它来探讨本文的问题,也够用了。

java 复制代码
package org.example;

public class SimpleAdder {

    public int add(int a, int b) {
        return a + b;
    }
}

MyBefore.java

MyBefore.java 的内容如下 ⬇️

java 复制代码
package org.study.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyBefore {
}

我们会把 @MyBefore 作为 @Before 的替代品,而它的内容可以照抄 @Before,注意需要修改 package

MyBeforeRule.java

MyBeforeRule.java 的内容如下 ⬇️

java 复制代码
package org.study.rules;

import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
import org.junit.runners.model.TestClass;
import org.study.annotations.MyBefore;

import java.util.List;

public class MyBeforeRule implements MethodRule {

    @Override
    public Statement apply(Statement base, FrameworkMethod method, Object target) {
        TestClass testClass = new TestClass(target.getClass());
        List<FrameworkMethod> myBeforeMethods = testClass.getAnnotatedMethods(MyBefore.class);
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                for (var myBeforeMethod : myBeforeMethods) {
                    myBeforeMethod.invokeExplosively(target);
                }
                base.evaluate();
            }
        };
    }
}

MyBeforeRule 用于处理 @MyBefore 注解

SimpleAdderTest.java

SimpleAdderTest.java 的内容如下

java 复制代码
package org.study;

import org.example.SimpleAdder;
import org.junit.*;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import org.study.annotations.MyBefore;
import org.study.rules.MyBeforeRule;

import java.util.Random;

public class SimpleAdderTest {

    private int a;
    private int b;

    private final SimpleAdder adder = new SimpleAdder();

    private static final int BOUND = 10;

    @Rule
    public final MyBeforeRule myBeforeRule = new MyBeforeRule();

    @MyBefore
    public void prepare() {
        Random random = new Random();
        a = random.nextInt(BOUND);
        b = random.nextInt(BOUND);
        System.out.printf("Randomly generated a is: %s%n", a);
        System.out.printf("Randomly generated b is: %s%n", b);
    }

    @Test
    public void testAdd() {
        int expectedResult = a + b;
        Assert.assertEquals(expectedResult, adder.add(a, b));
        String description = String.format("%s = %s + %s", expectedResult, a, b);
        System.out.println(description);
    }

    public static void main(String[] args) {
        Result result = JUnitCore.runClasses(SimpleAdderTest.class);
        for (Failure failure : result.getFailures()) {
            System.out.println(failure);
        }
    }
}

pom.xml

pom.xml 的内容如下

xml 复制代码
<?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>org.example</groupId>
    <artifactId>junit-study</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>25</maven.compiler.source>
        <maven.compiler.target>25</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

项目中出现的各个类的类图如下 ⬇️

运行结果

我们运行 SimpleAdderTest 中的 main 方法,应该可以看到类似这样的结果 ⬇️

其中 红色框 的内容和 prepare() 方法有关,黄色框 的内容和 testAdd() 方法有关。由于 a/b 这两个数字是随机生成的,所以你自己运行时,看到的 a/b 可能是其他值。

这样看来,我们的确实现了类似 @Before 注解的功能。但是这背后发生了什么呢?我们到下一篇再进行探讨吧。

其他

PlantUML 画图,所用到的代码

画 "类图" 所用的代码

puml 复制代码
@startuml
'https://plantuml.com/class-diagram

title 类图

annotation org.study.annotations.MyBefore

class org.example.SimpleAdder {
    + int add(int a, int b)
}

interface org.junit.rules.MethodRule {
    + Statement apply(Statement base, FrameworkMethod method, Object target)
}

class org.study.rules.MyBeforeRule

org.junit.rules.MethodRule <|.. org.study.rules.MyBeforeRule

class org.study.rules.MyBeforeRule {
    + Statement apply(Statement base, FrameworkMethod method, Object target)
}

class org.study.SimpleAdderTest {
    - int a;
    - int b;
    - final SimpleAdder adder
    - {static} final int BOUND
    + final MyBeforeRule myBeforeRule
    + void prepare()
    + void testAdd()
    + {static} void main(String[] args)
}

@enduml
相关推荐
Python私教7 小时前
Codex 写出的代码能跑却算错钱:我用 3 个测试拆穿一次 AI 编程幻觉
python·单元测试·ai编程
難釋懷1 天前
Nginx内存缓存
nginx·缓存·junit
林间码客2 天前
全栈视角下的 Mock 指南:从后端单元测试到前端 Vue 3 联调
前端·vue.js·单元测试
sun༒2 天前
Logback 日志框架总结:从入门到实战
单元测试·logback
Nuanyt2 天前
SSM 学习记录 第二部分 Spring整合Mybatis&Junit AOP核心概念 Spring事务管理 SpringMVC 请求与响应 Rest风格
java·spring·junit·mybatis·restful
happyness442 天前
如何利用 AI 自动编写单元测试(Unit Test)来捕捉隐藏的边缘情况 Bug?
人工智能·单元测试·bug
木子杳衫2 天前
Java高级进阶 | 单元测试JUnit + 反射机制实战详解
java·junit·单元测试
烤代码的吐司君3 天前
Lua 脚本
开发语言·junit·lua
Dontla3 天前
冒烟测试介绍(Smoke Test、BVT构建验证测试、构建验收测试)与回归测试对比、与单元测试对比、与集成测试对比、Cypress
java·单元测试·集成测试
文水爱心帮扶4 天前
文明劝导暖街头,青春志愿护通行|文水县爱心帮扶志愿者协会开展暑期交通文明活动
junit