Search code examples
javaunit-testingjunitjunit4junit-rule

How to share an ExternalResource between two test classes?


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

Solution

  • 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]