0



I have a jUnit 4/5-project and I run my test classes via test suites.

I use the com.github.peterwippermann.junit4.parameterizedsuite.ParameterizedSuite.class runner for the annotation "RunWith()" at the test suites but my test classes are use with same annotation the JUnitPlatform.class runner.
For this reason there it's not possible to read the - in the test suite - declared parameters (@Parameters) in my test classes. It works fine, if I use the @RunWith(Parameterized.class) runner at my test classes, but my goal is to use the junit 5 JUnitPlatform.class runner at my test classes and the jUnit 4 ParameterizedSuite.class runner to parameterize my test suites.

E.g. my test suite:

package TestSuite.Dummy;
//...some imports...

@RunWith(ParameterizedSuite.class)
@SuiteClasses({ 
T5.class
})
public class TestSuite_Dummy_01
{
    public static Object[] parameters = new Object[] {"A", "B", "C"};

    @Parameters(name = "Parameter of suite are {0}")
    public static Object[] params() throws NoSuchMethodException, SecurityException 
    {
       System.out.println(parameters.length);
       System.out.println(parameters.getClass().getTypeName());

       return parameters;
    }

    @Parameter(0)
    public static String myStringParameter;

    public static XYZQA QAE = null;

    public static XYZQA getXYZQA()
    {
       // Create a new XYZQA object
       try
       {
           QAE = new XYZQA();
           System.out.println("QAE created");
           return QAE;
       }
       catch(MalformedURLException e)
       {
           e.printStackTrace();
           return null;
       }
    }

    public static String getParameter()
    {
       System.out.println("Parameter available?" + ParameterContext.isParameterSet());
       return myStringParameter;
    }
}

And my example test class:

package Main.Demo;
//...some imports...

@RunWith(JUnitPlatform.class) //is necessary to run jUnit 5 tests with non-junit5-implemented IDEs
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@ExtendWith(ClientDescriptionParameterResolver.class)
public class T5
{    
    public static XYZQA QAE;
    public static String parameters;

    public T5()
    {
        //...
    }

    @BeforeAll
    static void setUpBeforeClass() throws Exception
    {
        QAE = TestSuite_Dummy_01.getXYZQA();
        parameters = TestSuite_Dummy_01.getParameter();
        System.out.println("Parameters from Suite: " + parameters);
    }

    @Test
    public void test01(ClientDescription client)
    {
        //...
    }

    //...
}

Question:
The following code line:

QAE = TestSuite_Dummy_01.getXYZQA(); //to import/use the QAE object of the test suite class into the current test class Blockquote


works fine for me, but it's fixed defined to the one test suite class "TestSuite_Dummy_01". But all of my test suites should worked with my test classes and not only this test suite. My idea is to use the current instance of a running test suite class (runner) of a test class instead of the fixed TestSuite_Dummy_01.class.
HOW I can get the current instance of a test class run and get the reference to the respective test suite class wihtin the test class?

SlitEye
  • 43
  • 6
  • If you switched to JUnit Jupiter you could write an extension to store the object in the root extension context and inject it into all the test methods that need it. – Marc Philipp Mar 09 '18 at 07:57
  • @MarcPhilipp, do have a sample code for my case? – SlitEye Mar 09 '18 at 13:05
  • AFAIK, there is no built-in mechanism for accessing the instance of the runner used to run an enclosing test suite. Thus, a potential workaround (assuming you are not executing tests concurrently) would be to store the current test suite instance in a `static` `ThreadLocal` variable in a common utility class and access that `ThreadLocal` from your various concrete test classes. – Sam Brannen Mar 10 '18 at 16:11

0 Answers0