0

I am learning JUnit and I have to test a method multiple times. I have to test the method based on two parameters (Environment and Case#). I am working on two environments where I have to check if the same Case# yields the same results between different environments. This is the test case:

public class AppStartTest
{

/**
 * Test method for {@link archive.AppStart#beginOper(java.lang.String)}.
 */

List<String> actualSections = new ArrayList<String>();
List<String> environments = new ArrayList<String>();
List<String> cases = new ArrayList<String>();

@Before
public void prepareTest()
{
    environments.add("env1");
    environments.add("env2");
    cases.add("case1");
    //cases.add("case2");
    cases.add("case3");
    cases.add("case32");
    cases.add("case4");
    cases.add("emp3");

}

@Test
public void testBeginOper()
{
    for (String caseStr : cases)
    {
        @SuppressWarnings("unchecked")
        Map<String, Integer> mapList[] = new HashMap[2];
        int i = 0;
        for (String env : environments)
        {
            System.out.println("Starting " + env + "-" + caseStr);
            AppStart a = new AppStart();
            mapList[i] = a.beginOper(env + "-" + caseStr, true);
            printMap(mapList[i++]);             
        }
                    //Using assert in this method
        compareResults(mapList[0], mapList[1], caseStr);
    }
}   
}

The result yields as a single test case, but I would be requiring the results as:

testBeginOper[0]
testBeginOper[1]
testBeginOper[2]
.....

I tried using parameterized test cases, but the test method would be executed independently. I have to compare the results between two environments and I would need the method with @Test to return some value (Method should be void) and then assert. Please advise.

apaul
  • 16,092
  • 8
  • 47
  • 82
dmachop
  • 824
  • 1
  • 21
  • 39

1 Answers1

0

The easiest solution I can think of would be to not have testBeginOper() be annotated with @Test at all but just be a standard, non-test, value returning, parameter accepting function. Then you have one @Test annotated function which runs your various versions of testBeginOper(), collects the results and then compares them. It could look something like this:

private Map<String, Integer> testBeginOper(String environment, String case) {
    // do your actual tests using the given parameters "environment" and "case"
}

@Test
public void runTests() {
    List<Map<String, Integer>> results = new ArrayList<>();
    for(String environment : environments) {
        for(String case : cases) {
            results.add(testBeginOper(environment, case));
        }
    }
    // now compare your results
}

There are alternatives of course, one being to write your own TestRunner, but this is probably by far the easiest solution.

blalasaadri
  • 5,990
  • 5
  • 38
  • 58
  • Thanks, but the solution is similar to the approach in the question. I get that I can call the function by passing arguments, but will I be able to analyze each of the cases? This is the reason I require individual output formats. – dmachop Jun 26 '14 at 21:43
  • @dmachop Of course you will be able to analyze each case and compare them - that's why my example collects all results in the `results` list. I'm not quite sure what you mean with "individual output formats" though - could you clarify? Are you talking about the printing of the maps? – blalasaadri Jun 26 '14 at 21:46
  • I meant the format as in the JUnit test run panel. If I run a parameterized test case, I would obtain the output as fn[0], fn[1], ... and so on. Given the constraint, is it possible to run the @ Test function varying the parameters in JUnit? I agree with you the alternatives does aid in obtaining the maps details individually. – dmachop Jun 26 '14 at 21:49
  • @dmachop Ah, ok. So, you want to run the tests for each case for every environment and then compare the results of `case1` between all environments, the results of `case3` between all environments, etc. But you're not interested in comparing the result of `case1` on `env1` with that of `case3` on `env2`? – blalasaadri Jun 26 '14 at 21:51
  • @dmachop Or do you want to run every set of arguments (so every environment combined with every case) as an own test case (which will be shown in the JUnit test run panel) but then also have a separate test that somehow compares the values after all of these tests are done? – blalasaadri Jun 26 '14 at 22:02
  • No, not to that detail. "_the results of case3 between all environments, etc. But you're not interested in comparing the result of case1 on env1 with that of case3 on env2?_" This is what I wanted. – dmachop Jun 27 '14 at 00:09