You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何判断JUnit测试是否执行通过?

判断JUnit测试是否通过的正确实现方式

你想在测试通过时执行清理操作(比如删除视频),下面分JUnit 5和JUnit 4两种常用版本给出正确实现:

JUnit 5(推荐使用)

方式一:通过ExtensionContext直接获取状态

在@AfterEach方法中注入ExtensionContext,通过它的getExecutionException()方法判断测试是否成功(空值表示测试无异常,即通过):

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.extension.ExtensionContext;

import java.nio.file.Files;

class TestClass {
    // 假设这是你的视频路径对象
    private Page page;

    @AfterEach
    void afterEach(ExtensionContext context) throws Exception {
        boolean testPassed = context.getExecutionException().isEmpty();
        if (testPassed) {
            Files.delete(page.video().path());
        }
    }

    // 你的测试方法
    // @Test
    // void test() {
    //     // 测试逻辑,比如初始化page和录制视频
    // }
}

方式二:自定义扩展记录状态(适合复杂场景)

如果需要在多个测试类复用状态判断逻辑,可以自定义扩展:

import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;

public class TestStatusTracker implements AfterTestExecutionCallback {
    private static final Namespace NAMESPACE = Namespace.create(TestStatusTracker.class);

    @Override
    public void afterTestExecution(ExtensionContext context) {
        boolean passed = context.getExecutionException().isEmpty();
        context.getStore(NAMESPACE).put("testPassed", passed);
    }

    public static boolean isTestPassed(ExtensionContext context) {
        return context.getStore(NAMESPACE).get("testPassed", Boolean.class);
    }
}

然后在测试类中使用:

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;

@ExtendWith(TestStatusTracker.class)
class TestClass {
    private Page page;

    @AfterEach
    void afterEach(ExtensionContext context) throws Exception {
        if (TestStatusTracker.isTestPassed(context)) {
            Files.delete(page.video().path());
        }
    }
}

JUnit 4实现

如果还在使用JUnit 4,可以通过TestWatcher规则监听测试结果:

import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;

import java.nio.file.Files;

public class TestClass {
    private Page page;
    private boolean testPassed;

    @Rule
    public TestWatcher testWatcher = new TestWatcher() {
        @Override
        protected void succeeded(Description description) {
            testPassed = true;
        }

        @Override
        protected void failed(Throwable e, Description description) {
            testPassed = false;
        }
    };

    @After
    public void after() throws Exception {
        if (testPassed) {
            Files.delete(page.video().path());
        }
    }

    @Test
    public void test() {
        // 测试逻辑
    }
}

注意事项

  • 你提到的JUnit.passes(TestClass.test)这种静态方法调用是不存在的,JUnit没有提供此类API
  • 尽量避免手动维护passed变量(比如在测试方法里设置passed = true),这种方式容易遗漏异常场景(比如测试抛出未捕获的RuntimeException),优先使用JUnit官方提供的API获取执行状态

内容的提问来源于stack exchange,提问作者principal-ideal-domain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 02:23:32