【问题标题】:jUnit and ignore inherited testsjUnit 并忽略继承的测试
【发布时间】: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


    【解决方案1】:

    我最终将所有设置/拆卸/存根代码删除到一个抽象类中。

    现在我的两个版本的测试只是从抽象扩展而来,所有的管理代码都可供他们使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-26
      • 2012-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-21
      相关资源
      最近更新 更多