Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share object reference between different JUnit tests

Tags:

java

junit

I have a couple of JUnit tests which need a reference for a expensive resource (a WALA class hierachie), which needs about 30s to be created. I would like to share this reference in my whole test suite.

I thought about a static member in a base class which is laziely initiated with a @BeforeClass method. After test is run the JVM should be determined anyway.

Is there any other way to accomplish this? Or any other best practice?

like image 726
markusw Avatar asked Oct 21 '22 09:10

markusw


1 Answers

Create an explicit test suite (cf. this answer) to run these tests, and use @BeforeClass and @AfterClass on the suite itself (cf. this answer):

@RunWith(Suite.class)
@Suite.SuiteClasses({Test1.class, Test2.class})
public class MySuite {
    @BeforeClass
    public static void initResource() {
        MyExpensiveResource.init();
    }

    @AfterClass
    public static void disposeResource() {
        MyExpensiveResource.dispose();
    }
}
like image 158
David Moles Avatar answered Oct 23 '22 01:10

David Moles