How to access variables in a test case class from a test suite runner class

1.4k views Asked by At

I have an abstract class that extends the JUnit TestCase with related data

public abstract class AbstractTestClass extends TestCase
{

    public ArrayList<TestDetails> testList;
}

I then have a test class that extends the abstract classes

public class TestClass extends AbstractTestClass

This is included in a test suite

@RunWith(Suite.class)
@Suite.SuiteClasses({
   TestClass.class
})


public class TestSuite {


}

I then run the test suite from a runner class with a main function

public class TestRunner {

    public static void main(String[] args) {

        JUnitCore.runClasses(TestSuite.class);

    }
}

My question is how do I access the testList data from within the runner class, i.e. if I wanted to print out the details of each element in the list from the main. The data is created dynamically (during the tests are being run)

1

There are 1 answers

6
Anirudh On

One way out might be to declare the ArrayList Testlist as static and make the TestRunner class extend the AbstractTestCase class:

So now your AbstractTestCase class looks as below:

import java.util.ArrayList;

import junit.framework.TestCase;

public abstract class AbstractTestClass extends TestCase
{

    public static ArrayList<TestDetails> testList;
}

and your TestRunner class looks like:

import org.junit.runner.JUnitCore;

public class TestRunner extends AbstractTestClass{

    public static void main(String[] args) {

        JUnitCore.runClasses(TestSuite.class);
        System.out.println(testList);
    }
}

Now you can access testList from the TestRunner. It may not the most elegant solution but it works.