user1884155
user1884155

Reputation: 3736

Pass parameters to JUnit test from the TestSuite class

I want to create 2 JUnit TestSuites. They both utilize the same test classes, but they should each use different parameters. For example, in test suite A, I want my data to be collected from file A and to be written to database A. In test suite B, I want my data to be collected from file B and to be written to databaseB.

The reason I use testSuites for this is because:

  1. I can put all the specific parameters in the testsuite classes
  2. I can reuse the testclasses
  3. I can choose which testsuite to run. I do not want all tests to always run with all possible paramaters!

The problem is I cannot really pass the parameters. I understand the way the Parameterized class works with JUnit, but it does not allow point 3 in the list above. If I use the code below it will run my test class with both databse connections, which is not what I want to achieve.

@RunWith(value = Parameterized.class)
public class TestCheckData
{
    private File file;
    private DatabaseSource databaseSource;

    public TestCheckData(File file, DatabaseSource databaseSource)
    {
        this.file = file;
        this.databaseSource = databaseSource;
    }

    @Parameters
    public static Iterable<Object[]> data1()
    {
        return Arrays.asList(new Object[][]
        {
            { TestSuiteA.DATA_FILE_A, TestSuite1.DATABASE_A }, 
            { TestSuiteB.DATA_FILE_B, TestSuite1.DATABASE_B }
        });


    }

I already find some way of passing configurations in a spring context in this question, but I'm not using any special framework.

Upvotes: 3

Views: 2976

Answers (2)

dkatzel
dkatzel

Reputation: 31648

You could modify the tests so that they get the parameters from a config file. This way you would always only have 1 Suite.

The path of the config file can be looked up via a System property.

Then on the invocation of the test suite, you could pass in a different config file by changing the property using the -D option on the JVM.

So for example if you named the proprerty env.properties then your command would be:

 %java -Denv.properties=prod.config runMyTests

or

 %java -Denv.properties=dev.config runMyTests

etc

Upvotes: 0

Martin Serrano
Martin Serrano

Reputation: 3775

Well, this would be a little unconventional, but you could add a different Test class to the beginning of each suite run that would set the parameters you want to use for that test. So you'd have classes like:

public abstract class StaticParameters {
  public static File dataFileToUse = null;
  public static DatabaseSource databaseToUse = null;
}

public class Suite1Params extends StaticParameters {
  @BeforeClass
  public static void setParams() {
    dataFileToUse = DATA_FILE_A;
    databaseToUse = DATABASE_A;
  }
}

public class Suite2Params extends StaticParameters {
  @BeforeClass
  public static void setParams() {
    dataFileToUse = DATA_FILE_B;
    databaseToUse = DATABASE_B;
  }
}

Then you'd just make Suite1Params or Suite2Params the first in your suite list. You might have to add a fake @Test entry to the params classes, I'm not sure if the Suite runner requires that.

Upvotes: 1

Related Questions