1 package test;
2 
3 import org.testng.Assert;
4 import org.testng.TestListenerAdapter;
5 import org.testng.TestNG;
6 import org.testng.annotations.BeforeMethod;
7 import org.testng.annotations.Test;
8 import org.testng.xml.XmlSuite;
9 import org.testng.xml.XmlTest;
10 
11 import java.util.Arrays;
12 
13 public class ReturnValueTest extends SimpleBaseTest {
14 
15   private XmlSuite m_suite;
16   private XmlTest m_test;
17 
18   @BeforeMethod
before()19   public void before() {
20     m_suite = createXmlSuite("suite");
21     m_test = createXmlTest(m_suite, "test", ReturnValueSampleTest.class.getName());
22   }
23 
24   @Test
suiteReturnValueTestShouldBeRun()25   public void suiteReturnValueTestShouldBeRun() {
26     m_suite.setAllowReturnValues(true);
27     runTest(true);
28   }
29 
30   @Test
suiteReturnValueTestShouldNotBeRun()31   public void suiteReturnValueTestShouldNotBeRun() {
32     runTest(false);
33   }
34 
35   @Test
testReturnValueTestShouldBeRun()36   public void testReturnValueTestShouldBeRun() {
37     m_test.setAllowReturnValues(true);
38     runTest(true);
39   }
40 
runTest(boolean allowed)41   private void runTest(boolean allowed) {
42     TestNG tng = create();
43     tng.setXmlSuites(Arrays.asList(m_suite));
44     TestListenerAdapter tla = new TestListenerAdapter();
45     tng.addListener(tla);
46     tng.run();
47 
48     if (allowed) {
49       Assert.assertEquals(tla.getFailedTests().size(), 0);
50       Assert.assertEquals(tla.getSkippedTests().size(), 0);
51       assertTestResultsEqual(tla.getPassedTests(), Arrays.asList("shouldRun"));
52     } else {
53       Assert.assertEquals(tla.getFailedTests().size(), 0);
54       Assert.assertEquals(tla.getPassedTests().size(), 0);
55       Assert.assertEquals(tla.getSkippedTests().size(), 0);
56     }
57   }
58 
59 }
60