1

I am struggling a bit to understand the benefit of using ExternalResource. The documentation and other posts (How Junit @Rule works?) have all alluded to being able to share code between tests within a class and/or share code between test classes.

I am trying to use ExternalResource for a DB connection in a functional/integration test, but I don't see how I can share that connection across classes. In fact, I don't really see the benefit over @Before/@After in this case. Am I using this incorrectly or what am I missing?

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);
    }
}
Community
  • 1
  • 1
user1766760
  • 631
  • 2
  • 8
  • 26
  • The idea behind resources is to have something self-contained (like start a webserver before() and stop it after()) –  Jan 10 '14 at 21:27
  • @RC. I don't really want to share the `Connection` object between test classes. But I don't see how I can avoid copying the same Rule into every other class. The doc specifically states "ExternalResource is a base class for Rules (like TemporaryFolder) that set up an external resource before a test (a file, socket, server, database connection, etc.), and guarantee to tear it down afterward", so I assumed I can use it for database connections. – user1766760 Jan 10 '14 at 21:34
  • I don't see why you could not use a rule here. –  Jan 10 '14 at 21:38

1 Answers1

3

I would do something like that:

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;
    }
}

then use it in your test like this:

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

    // ...

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

    // ...
}

[not tested]

  • I tried do this earlier within the `ExternalResource` rule created within the test (which now I see of course wouldn't work because I wasn't extending... pfft) – user1766760 Jan 10 '14 at 21:54