我需要在使用它的类中的所有测试用例之前和之后使用扩展来运行代码。我的测试类需要访问扩展类中的字段。这可能吗?
鉴于:
@ExtendWith(MyExtension.class)
public class MyTestClass {
@Test
public void test() {
// get myField from extension and use it in the test
}
}
和
public class MyExtension implements
BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback {
private int myField;
public MyExtension() {
myField = someLogic();
}
...
}
如何访问 myField
从我的测试班?
1条答案
按热度按时间c7rzv4ha1#
您可以通过标记注解和
BeforeEachCallback
分机。创建一个特殊的标记注解,例如。
使用注解从扩展名中查找和设置值:
然后,在测试中,对目标字段进行注解,并使用扩展名扩展测试:
注意:您也可以扩展
BeforeAllCallback
它在类的所有测试方法之前执行一次,具体取决于您的实际需求。