spring通过父元素的extendwith注解访问子元素?

rryofs0p  于 2021-07-16  发布在  Java
关注(0)|答案(0)|浏览(199)

也许有人能告诉我我的设置很糟糕。我正在编写一些实用程序来简化我们的集成测试工作,以减少集成测试的before/after方法的重复代码量。
ie—清除数据库中的数据。
根据集成测试需要哪些容器,我可以拥有n个测试线束类。
我想能跑得像 client.clearDatabase 但是从下面的父类 extendWith 注解。显然,在这种情况下,我无法访问客户端,而这只是一个实用程序模块。我想知道我怎样才能正确地思考这个问题。

测试a

@SpringBootTest(classes = ClassImTesting.class)
@ExtendWith(TestHarnessUtility.class)
public class MyCoolIntegrationTest {
  @Autowired private Database client;

  // This is what I want to exist in the ExtendWith class.. ie - TestHarnessUtility, then I can reuse this and people don't need to always worry about duplicating this block of code
  @AfterEach
  public void testCleanup() {
    client.clearDatabase();
  }

测试线束等级

/**
 * Automates the setup and configuration of all integration tests that require usage of the
 * Gremlin containers for Graph DB testing.
 */
public class TestHarnessUtility extends SuperTestHarnessUtility {

  @Override
  public void beforeAll(ExtensionContext context) {
    startTestsWithContainers(GREMLIN_SERVER);
    System
        .setProperty(GREMLIN_SERVER_KEY, SupportedContainers.gremlinServer.getContainerIpAddress());
    System.setProperty(GREMLIN_SERVER_PORT,
        String.valueOf(SupportedContainers.gremlinServer.getFirstMappedPort()));
  }

 @Override
  public void afterAll(ExtensionContext context) {
   // Method not implementented
 }

  @Override
  public void afterEach(ExtensionContext context) {
    // Method not implemented
 }
}

超级测试线束实用程序

public abstract class SuperTestHarnessUtility implements BeforeAllCallback, AfterAllCallback,
    AfterEachCallback {

  /**
   * Runs after all Spring tests execute - Cleans up the state for the next set.
   */
  @Override
  public void afterAll(ExtensionContext context) throws Exception {

  }

  /**
   * Runs after each Spring test executes - Cleans up the state for the next set.
   */
  @Override
  public void afterEach(ExtensionContext context) throws Exception {

  }

  /**
   * Runs before all Spring tests execute - Facilitates container initialization and environment
   * variable property injection.
   */

  @Override
  public void beforeAll(ExtensionContext context) throws Exception {

  }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题