我有多个单独的测试类,它们都使用相同的测试对象.目前,我在每个此类中都使用@Before,这显然不是一个很好的解决方案.
当然,一种选择是从创建该对象的抽象类继承.如here所述,这也不是一个好主意.
另一个选择是“外部资源”,但这就是-顾名思义-用于资源而不是测试对象.
我觉得我缺少一些东西,因为这必须是JUnit中的基本任务.
解决方法:
您可能正在寻找TestRule.要引用Javadoc:
A TestRule is an alteration in how a test method, or set of test methods, is run and reported. A TestRule may add additional checks that cause a test that would otherwise fail to pass, or it may perform necessary setup or cleanup for tests, or it may observe test execution to report it elsewhere. TestRules can do everything that could be done previously with methods annotated with Before, After, BeforeClass, or AfterClass, but they are more powerful, and more easily shared between projects and classes.
然后继续列出几种类型,其中ExternalResource是其中一种.但也有其他人.
鉴于您谈论的是比使用@Before方法做的更好,并且您希望在类之间共享,这听起来很像您在描述的内容.
标签:junit,java
来源: https://codeday.me/bug/20191026/1933442.html