java - 如何在两个测试类之间共享ExternalResource?

标签 java unit-testing junit junit4 junit-rule

我很难理解使用 ExternalResource 的好处。 documentation和其他帖子( How Junit @Rule works? )都提到能够在类内的测试之间共享代码和/或在测试类之间共享代码。

我正在尝试在功能/集成测试中使用 ExternalResource 进行数据库连接,但我不知道如何跨类共享该连接。事实上,在这种情况下,我并没有真正看到相对于 @Before/@After 的好处。我是否错误地使用了它或者我错过了什么?

public class some_IntegrationTest {

    private static String id;
    Connection connection = null;

    //...

    @Rule
    public ExternalResource DBConnectionResource = new ExternalResource() {
        @Override
        protected void before() throws SQLException {
            connection = DbUtil.openConnection();
        }

        @Override
        protected void after() {
            DbUtil.closeConnection(connection);
        }
    };

    @BeforeClass
    public static void setUpClass() throws SQLException {
        System.out.println("@BeforeClass setUpClass");
        cleanup(id);
    }

    //I want to do something like this
    @Test
    public void test01() {
        cleanupData(connection, id);
        // do stuff...
    }

    @Test
    public void test02() {
        cleanupTnxLog(connection, id);
        // do stuff...
    }

    //...


    private static void cleanup(String id) throws SQLException {
        LOGGER.info("Cleaning up records");
        Connection connection = null;
        try {
            connection = DbUtil.openConnection();
            cleanupData(connection, id);
            cleanupTnxLog(connection, id);
        } finally {
            DbUtil.closeConnection(connection);
        }
    }

    private static void cleanupData(Connection connection, String id)
        throws SQLException {
        dao1.delete(connection, id);
    }

    private static void cleanupTnxLog(Connection connection, String id)
        throws SQLException {
        dao2.delete(connection, id);
    }
}

最佳答案

我会做类似的事情:

public class DbConnectionRessource extends ExternalRessource {

    private Connection connection;

    @Override
    protected void before() throws SQLException {
        connection = DbUtil.openConnection();
    }

    @Override
    protected void after() {
        DbUtil.closeConnection(connection);
    }

    public Connection getConnection() {
        return connection;
    }
}

然后在测试中使用它,如下所示:

public class SomeIntegrationTest {
    @Rule
    public DbConnectionRessource dbConnectionResource = new DbConnectionRessource();

    // ...

    @Test
    public void test01() {
        cleanupData(dbConnectionResource.getConnection(), id);
        // do stuff...
    }

    // ...
}

[未测试]

关于java - 如何在两个测试类之间共享ExternalResource?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21053855/

相关文章:

java - 当我尝试发送请求时出现未知主机异常

java - 为什么java方法不支持多个返回值?

java - Junit,测试,Flyway : Can you combine @Before and @Flyway annotations?

java - Wiremock 单元测试在本地失败 - java.net.BindException : Address already in use: JVM_Bind

java - Android 中资源的动态命名,无需 getIdentifier

java - 我无法从数组输出中删除空值

c# - 如何使用 Entity Framework 测试 View ?

java - Android 单元测试未为 Mockito stub 方法提供模拟消息

angular - 如何在 Angular 服务中运行 toast 消息的单元测试用例?

java - Mockito:如何通过模拟测试我的服务?