使用Junit 5进行沙盒测试(Junit4@RunWith是否比Junit5@ExtendWith更强大)

ix0qys7i  于 2022-11-24  发布在  其他
关注(0)|答案(1)|浏览(138)

所以在Junit 4中,我可以通过@RunWith注解用我自己的runner运行测试。这是超级强大的,允许我为每个测试使用我自己的“特殊”类加载器。这个“特殊”类加载器的工作是在访问类的子集时重新加载这些类(实际上它可以实现不同的重载策略,但这不是重点)。这有效地让我实现了在“沙箱”中运行每个测试。
我有多个设置/要求静态类变量的不同值的测试,这些测试并行运行而不会相互干扰。
我的问题是,是否有可能通过一个新的Junit 5来达到同样的涅槃状态?
非常感谢您抽出时间查看并回答此问题。
我尝试将@ExtendWith与TestInstanceFactory的自定义实现(重新加载类并返回“reloaded”类的示例)沿着使用,但最终失败,出现以下异常:

org.junit.jupiter.api.extension.TestInstantiationException message: TestInstanceFactory [<my implementation of TestInstanceFactory class name>] failed to return an instance of [<my-test-class>@<hash as loaded by original loader>] and instead returned an instance of [<my-test-class>@<hash as loaded by my special loader>].
b91juud3

b91juud31#

显然,这在6月5日是可能的。请参见github讨论和建议的解决方案:https://github.com/junit-team/junit5/issues/3028#issuecomment-1286880925

相关问题