首页 > 解决方案 > maven测试前后如何运行

问题描述

mvn test

我希望在使用 maven test 执行所有测试用例之前初始化某些资源,并在执行所有测试用例后销毁它们。

我研究了 jUnit @BeforeClass@AfterClass@Before@After,但它们都没有帮助。

我尝试使用 maven 生命周期阶段,即如下所示的预集成测试,但即使在这种情况下,预期的测试用例 (TestPostgresqlEmbedded) 也不会首先执行。

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>${maven.surefire.plugin.version}</version>
  <executions>
    <execution>
        <id>test-init</id>
        <configuration>
          <runOrder>alphabetical</runOrder>
          <includes>
            <include>**/TestPostgresqlEmbedded.java</include>
          </includes>
        </configuration>
        <phase>pre-integration-test</phase>
    </execution>
    <execution>
        <id>test-all</id>
        <configuration>
          <runOrder>alphabetical</runOrder>
        </configuration>
    </execution>
  </executions>
</plugin>

我怎样才能做到这一点?

标签: mavenjunit

解决方案


Use the integration-test phase for tests with databases. Then you have pre-integration-test to set up your database resources, and post-integration-test to destroy them.


推荐阅读