【发布时间】:2021-12-12 00:40:29
【问题描述】:
我需要创建现有服务端点的版本 2。在创建单元测试时,我意识到测试新代码最有效的方法是扩展现有的单元测试类。
设置、拆卸和存根方法已经完成并且非常复杂。出于冗余原因,我不想将它们复制到新的单元测试中,并且我不能将它们移动到实用程序类中,因为某些存根类中有一些紧密耦合的逻辑。
当我运行我的新单元测试时,我看到继承的单元测试也在运行,这不是我想要的。有没有人在不运行任何 @Test 类的情况下成功地从基本单元测试继承?
这是我的基类的一个示例:
@SpringBootTest
@ContextConfiguration
@ExtendWith(MockitoExtension.class)
@AutoConfigureMockMvc
public class MyBaseClass extends TestBase {
@Value("${test.foo}")
private String foo
// setup, teardown, stubbing to pull in data to run the tests AND then the tests themselves
}
@SpringBootTest
@ContextConfiguration
@ExtendWith(MockitoExtension.class)
@AutoConfigureMockMvc
public class MyVersion2BaseClass extends MyBaseClass {
/* JUST THE TESTS, NO SETUP/TEAR DOWN/STUBBING NEEDED
AS THIS IS CONTAINED IN THE PARENT CLASS
*/
}
【问题讨论】:
标签: java spring-boot unit-testing junit