1 package test.parameters;
2 
3 import org.testng.Assert;
4 import org.testng.TestListenerAdapter;
5 import org.testng.TestNG;
6 import org.testng.annotations.Test;
7 
8 import test.SimpleBaseTest;
9 
10 import java.io.ByteArrayOutputStream;
11 import java.io.IOException;
12 import java.io.OutputStream;
13 import java.io.PrintStream;
14 import java.util.Arrays;
15 
16 public class ParamInheritanceTest extends SimpleBaseTest {
17 
18   @Test(description = "When verbose is set to >1, TNG prints test results on CLI which are printed "
19       + "using SuiteResultCounts.calculateResultCounts(). This method has been throwing NPE "
20       + "because it's unable to find SuiteRunner in HashMap, because the list of parameters in "
21       + "SuiteRunner changed" + " during execution. This test makes sure we dont run into any NPEs")
noNPEInCountingResults()22   public void noNPEInCountingResults() {
23     TestListenerAdapter tla = new TestListenerAdapter();
24     TestNG tng = create();
25     tng.setTestSuites(Arrays.asList(getPathToResource("param-inheritance/parent-suite.xml")));
26     tng.setVerbose(2);
27     tng.addListener(tla);
28     OutputStream os = new ByteArrayOutputStream();
29     PrintStream out = System.out;
30     PrintStream err = System.err;
31     try {
32       /*
33        * Changing system print streams so that exception or results stmt is not logged
34        * while running test (avoid confusing person running tests)
35        */
36       System.setOut(new PrintStream(os));
37       System.setErr(new PrintStream(os));
38       tng.run();
39       Assert.assertEquals(tla.getPassedTests().size(), 1);
40     }
41     finally {
42       try {
43         os.close();
44       }
45       catch (IOException e) {
46         //no need to handle this
47       }
48       System.setOut(out);
49       System.setErr(err);
50     }
51   }
52 
53   @Test(description = "Checks to make sure parameters are inherited and overridden properly")
parameterInheritanceAndOverriding()54   public void parameterInheritanceAndOverriding() {
55     TestListenerAdapter tla = new TestListenerAdapter();
56     TestNG tng = create();
57     tng.setUseDefaultListeners(false);
58     tng.setTestSuites(Arrays.asList(getPathToResource("parametertest/parent-suite.xml")));
59     tng.addListener(tla);
60     tng.run();
61     Assert.assertEquals(tla.getPassedTests().size(), 3);
62   }
63 }
64