简体   繁体   English

如何获取测试类实例JUnit从ClassRunner内部运行测试用例?

[英]How do I get the test class instance JUnit is running test cases on from inside the ClassRunner?

我有一个扩展BlockJUnit4ClassRunner的自定义测试运行器,我想获得在我的自定义测试运行器中的BlockJUnit4ClassRunner中实例化的测试类的实例。

I figured this one out. 我想出了这一个。

What you can do is override createTest() in BlockJUnit4ClassRunner, and pass the result of super.createTest() to a method of your choice. 你可以做的是覆盖BlockJUnit4ClassRunner中的createTest() ,并将super.createTest()的结果传递给你选择的方法。 For example: 例如:

public class CustomTestRunner extends AbstractTestRunner {

    @Override
    public abstract void prepareTest(final Object test) {
     // have your way with the test object
    }
}

public abstract class AbstractTestRunner extends BlockJUnit4ClassRunner {

    @Override
    public Object createTest() throws Exception {
         Object test = super.createTest();
         prepareTest(test);
         return test;
    }

    public abstract void prepareTest(final Object test);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 JUnit 测试用例中的接口获取 class 实例 - How to i get class instance from interface in JUnit test case 如何从TestNG获得一份关于所有测试用例的junit报告? - How do I get one junit report from TestNG for all my test cases? 如果我没有实现Junit测试用例,可以在Junit中使用Assert类吗? - Can I use Assert class in Junit if I am not implementing Junit test cases and how to do that? 测试用例不在JUnit中运行 - Test cases are not running in JUnit 当我做mvn clean install时,Junit测试用例没有运行 - Junit test cases are not running when i do mvn clean install JUnit 5如何从嵌套测试类中获取父测试的实例 - JUnit 5 How to get the instance of parent test form a nested test class 初始化错误在移动或删除类后使用SpringJUnit4ClassRunner运行junit测试 - initializationError running junit test with SpringJUnit4ClassRunner after moving or deleting a class 如何为JUnit中的接口准备测试用例? - How do I prepare test cases for Interfaces in JUnit? 如何为Thread类编写Junit测试用例 - How to write Junit test cases for a Thread class 如何在Android的JUnit测试用例中获取MainActivity的上下文和活动? - How do I get the context and activity of MainActivity in JUnit test cases in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM