0

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)

Anirudh
  • 2,286
  • 4
  • 38
  • 64
doblo7
  • 11
  • 7

1 Answers1

0

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.

Anirudh
  • 2,286
  • 4
  • 38
  • 64
  • Hi, thanks for the reply. I did think of this but unfortunately I have many instances of AbstractTestClass so therefore making the list static wouldn't work. Is there anything else you can think of? – doblo7 Nov 18 '14 at 09:14
  • I don't think you could get the access to TestList data unless in some way the TestRunner class has access to it via Inheritance, other workarounds won't exist as it would violate the OOPS concept of Inheritance, the only work around of Multiple Inheritance are Interfaces so you would need to have interface instead of abstract class and make the TestCase class implement them at least the getter methods but then again it's not a good fix, it's strongly advisable to keep all test data at one place and not distributed. – Anirudh Nov 18 '14 at 10:12
  • If you still wish to do it you may keep all the abstract classes in one place or package you may use static imports for TestRunner class and keep the testList as static – Anirudh Nov 18 '14 at 10:32
  • Thanks Anirudh, I think I'm going to write the data thats in the testList to a file from within the AbstractTestClass and then extract it, would you consider this better practice? – doblo7 Nov 18 '14 at 13:27
  • Is this test data your input data for test cases? if so You could keep them in excel file and use POI API for extraction...if data is built during the time of execution of test case then that is processed data and not exactly test data. – Anirudh Nov 19 '14 at 06:33
  • I already have test data in an excel csv file which I extract into the program. The 'testList' data is similar to a test review/log for each test, hence why it is created dynamically – doblo7 Nov 20 '14 at 10:44