0001: /*
0002: * Copyright 2006-2007, Unitils.org
0003: *
0004: * Licensed under the Apache License, Version 2.0 (the "License");
0005: * you may not use this file except in compliance with the License.
0006: * You may obtain a copy of the License at
0007: *
0008: * http://www.apache.org/licenses/LICENSE-2.0
0009: *
0010: * Unless required by applicable law or agreed to in writing, software
0011: * distributed under the License is distributed on an "AS IS" BASIS,
0012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013: * See the License for the specific language governing permissions and
0014: * limitations under the License.
0015: */
0016: package org.unitils;
0017:
0018: import static junit.framework.Assert.assertEquals;
0019: import static junit.framework.Assert.assertFalse;
0020: import junit.framework.TestResult;
0021: import junit.framework.TestSuite;
0022: import junit.textui.TestRunner;
0023: import org.junit.AfterClass;
0024: import org.junit.Before;
0025: import org.junit.BeforeClass;
0026: import org.junit.Test;
0027: import org.junit.internal.runners.InitializationError;
0028: import org.junit.runner.Result;
0029: import org.junit.runner.notification.RunNotifier;
0030: import org.testng.TestListenerAdapter;
0031: import org.testng.TestNG;
0032: import static org.unitils.TracingTestListener.*;
0033: import org.unitils.core.TestListener;
0034: import org.unitils.core.Unitils;
0035: import static org.unitils.inject.util.InjectionUtils.injectIntoStatic;
0036: import org.unitils.util.ReflectionUtils;
0037:
0038: import java.util.Iterator;
0039:
0040: /**
0041: * Test for the flows in case an exception occurs in one of the listener or test methods for JUnit3 ({@link UnitilsJUnit3}),
0042: * JUnit4 (@link UnitilsJUnit4TestClassRunner}) and TestNG ({@link UnitilsTestNG}).
0043: * <p/>
0044: * Except for some minor differences, the flows for all these test frameworks
0045: * are expected to be the same (see assertInvocationOrder* methods).
0046: *
0047: * @author Tim Ducheyne
0048: * @author Filip Neven
0049: * @see UnitilsJUnit3Test_TestClass1
0050: * @see UnitilsJUnit4Test_TestClass1
0051: * @see UnitilsTestNGTest_TestClass1
0052: */
0053: public class UnitilsInvocationExceptionTest {
0054:
0055: /* Listener that records all method invocations during the tests */
0056: private static TracingTestListener tracingTestListener;
0057:
0058: /* Temporary holder so that the test listener that was replaced during the test can be place back */
0059: private static TestListener oldTestListenerUnitilsJUnit3;
0060:
0061: /* Temporary holder so that the test listener that was replaced during the test can be place back */
0062: private static TestListener oldTestListenerUnitilsJUnit4;
0063:
0064: /**
0065: * Sets up the test by installing the tracing test listener that will record all method invocations during the test.
0066: * The current test listeners are stored so that they can be restored during the class tear down.
0067: */
0068: @BeforeClass
0069: public static void classSetUp() {
0070: oldTestListenerUnitilsJUnit3 = (TestListener) ReflectionUtils
0071: .getFieldValue(null, ReflectionUtils.getFieldWithName(
0072: UnitilsJUnit3.class, "testListener", true));
0073: oldTestListenerUnitilsJUnit4 = (TestListener) ReflectionUtils
0074: .getFieldValue(null, ReflectionUtils.getFieldWithName(
0075: UnitilsJUnit4TestClassRunner.class,
0076: "testListener", true));
0077:
0078: injectIntoStatic(null, UnitilsJUnit3.class, "testListener");
0079: injectIntoStatic(null, UnitilsJUnit4TestClassRunner.class,
0080: "testListener");
0081:
0082: tracingTestListener = new TracingTestListener();
0083:
0084: UnitilsJUnit3Test_TestClass1
0085: .setTracingTestListener(tracingTestListener);
0086: UnitilsJUnit4Test_TestClass1
0087: .setTracingTestListener(tracingTestListener);
0088: UnitilsTestNGTest_TestClass1
0089: .setTracingTestListener(tracingTestListener);
0090: }
0091:
0092: /**
0093: * This will put back the old test listeners that were replaced by the tracing test listener.
0094: */
0095: @AfterClass
0096: public static void classTearDown() {
0097: injectIntoStatic(oldTestListenerUnitilsJUnit3,
0098: UnitilsJUnit3.class, "testListener");
0099: injectIntoStatic(oldTestListenerUnitilsJUnit4,
0100: UnitilsJUnit4TestClassRunner.class, "testListener");
0101: }
0102:
0103: /**
0104: * Sets up the test by clearing the previous recorded method invocations. This will also re-initiliaze
0105: * the base-classes so that, for example beforeAll() will be called another time.
0106: */
0107: @Before
0108: public void setUp() throws Exception {
0109: tracingTestListener.getCallList().clear();
0110:
0111: // clear state so that beforeAll is called
0112: injectIntoStatic(false, UnitilsJUnit3.class, "beforeAllCalled");
0113: injectIntoStatic(null, UnitilsJUnit3.class, "lastTestClass");
0114: injectIntoStatic(false, UnitilsJUnit4TestClassRunner.class,
0115: "beforeAllCalled");
0116: }
0117:
0118: /**
0119: * Test the flow when a runtime exception is thrown during a {@link TestListener#beforeAll} call of a module.
0120: *
0121: * @see #assertInvocationOrder_beforeAll
0122: */
0123: @Test
0124: public void testUnitilsJUnit3_beforeAll_RuntimeException() {
0125: tracingTestListener.setExceptionMethod(BEFORE_ALL, false);
0126: TestResult result = TestRunner.run(new TestSuite(
0127: UnitilsJUnit3Test_TestClass1.class));
0128:
0129: assertInvocationOrder_beforeAll("JUnit3", tracingTestListener);
0130: assertEquals(0, result.failureCount());
0131: assertEquals(1, result.errorCount());
0132: }
0133:
0134: /**
0135: * Test the flow when an assertion error is thrown during a {@link TestListener#beforeAll} call of a module.
0136: *
0137: * @see #assertInvocationOrder_beforeAll
0138: */
0139: @Test
0140: public void testUnitilsJUnit3_beforeAll_AssertionFailedError() {
0141: tracingTestListener.setExceptionMethod(BEFORE_ALL, true);
0142: TestResult result = TestRunner.run(new TestSuite(
0143: UnitilsJUnit3Test_TestClass1.class));
0144:
0145: assertInvocationOrder_beforeAll("JUnit3", tracingTestListener);
0146: assertEquals(1, result.failureCount()); // failures instead of errors
0147: assertEquals(0, result.errorCount());
0148: }
0149:
0150: /**
0151: * Test the flow when a runtime exception is thrown during a {@link TestListener#beforeAll} call of a module.
0152: *
0153: * @see #assertInvocationOrder_beforeAll
0154: */
0155: @Test
0156: public void testUnitilsJUnit4_beforeAll() throws Exception {
0157: tracingTestListener.setExceptionMethod(BEFORE_ALL, false);
0158: Result result = performJUnit4Test();
0159:
0160: assertInvocationOrder_beforeAll("JUnit4", tracingTestListener);
0161: assertEquals(1, result.getRunCount());
0162: assertEquals(1, result.getFailureCount());
0163: assertEquals(0, result.getIgnoreCount());
0164: }
0165:
0166: /**
0167: * Test the flow when a runtime exception is thrown during a {@link TestListener#beforeAll} call of a module.
0168: *
0169: * @see #assertInvocationOrder_beforeAll
0170: */
0171: @Test
0172: public void testUnitilsTestNG_beforeAll() throws Exception {
0173: tracingTestListener.setExceptionMethod(BEFORE_ALL, false);
0174: TestListenerAdapter testListenerAdapter = performTestNGTest();
0175:
0176: assertInvocationOrder_beforeAll("TestNG", tracingTestListener);
0177: assertEquals(1, testListenerAdapter.getFailedTests().size());
0178: }
0179:
0180: /**
0181: * Test the flow when a runtime exception is thrown during a {@link TestListener#beforeTestClass} call of a module.
0182: *
0183: * @see #assertInvocationOrder_beforeTestClass
0184: */
0185: @Test
0186: public void testUnitilsJUnit3_beforeTestClass_RuntimeException() {
0187: tracingTestListener
0188: .setExceptionMethod(BEFORE_TEST_CLASS, false);
0189: TestResult result = TestRunner.run(new TestSuite(
0190: UnitilsJUnit3Test_TestClass1.class));
0191:
0192: assertInvocationOrder_beforeTestClass("JUnit3",
0193: tracingTestListener);
0194: assertEquals(0, result.failureCount());
0195: assertEquals(2, result.errorCount());
0196: }
0197:
0198: /**
0199: * Test the flow when an assertion error is thrown during a {@link TestListener#beforeTestClass} call of a module.
0200: *
0201: * @see #assertInvocationOrder_beforeTestClass
0202: */
0203: @Test
0204: public void testUnitilsJUnit3_beforeTestClass_AssertionFailedError() {
0205: tracingTestListener.setExceptionMethod(BEFORE_TEST_CLASS, true);
0206: TestResult result = TestRunner.run(new TestSuite(
0207: UnitilsJUnit3Test_TestClass1.class));
0208:
0209: assertInvocationOrder_beforeTestClass("JUnit3",
0210: tracingTestListener);
0211: assertEquals(2, result.failureCount()); // failures instead of errors
0212: assertEquals(0, result.errorCount());
0213: }
0214:
0215: /**
0216: * Test the flow when a runtime exception is thrown during a {@link TestListener#beforeTestClass} call of a module.
0217: *
0218: * @see #assertInvocationOrder_beforeTestClass
0219: */
0220: @Test
0221: public void testUnitilsJUnit4_beforeTestClass() throws Exception {
0222: tracingTestListener
0223: .setExceptionMethod(BEFORE_TEST_CLASS, false);
0224: Result result = performJUnit4Test();
0225:
0226: assertInvocationOrder_beforeTestClass("JUnit4",
0227: tracingTestListener);
0228: assertEquals(0, result.getRunCount());
0229: assertEquals(1, result.getFailureCount());
0230: assertEquals(0, result.getIgnoreCount());
0231: }
0232:
0233: /**
0234: * Test the flow when a runtime exception is thrown during a {@link TestListener#beforeTestClass} call of a module.
0235: *
0236: * @see #assertInvocationOrder_beforeTestClass
0237: */
0238: @Test
0239: public void testUnitilsTestNG_beforeTestClass() throws Exception {
0240: tracingTestListener
0241: .setExceptionMethod(BEFORE_TEST_CLASS, false);
0242: TestListenerAdapter testListenerAdapter = performTestNGTest();
0243:
0244: assertInvocationOrder_beforeTestClass("TestNG",
0245: tracingTestListener);
0246: assertEquals(1, testListenerAdapter.getFailedTests().size());
0247: }
0248:
0249: // no JUnit 3 versions of testBeforeClass (does not exist for JUnit3)
0250:
0251: /**
0252: * Test the flow when a runtime exception is thrown during a {@link org.junit.BeforeClass} call of a test.
0253: *
0254: * @see #assertInvocationOrder_testBeforeClass
0255: */
0256: @Test
0257: public void testUnitilsJUnit4_testBeforeClass() throws Exception {
0258: tracingTestListener
0259: .setExceptionMethod(TEST_BEFORE_CLASS, false);
0260: Result result = performJUnit4Test();
0261:
0262: assertInvocationOrder_testBeforeClass("JUnit4",
0263: tracingTestListener);
0264: assertEquals(0, result.getRunCount());
0265: assertEquals(1, result.getFailureCount());
0266: assertEquals(0, result.getIgnoreCount());
0267: }
0268:
0269: /**
0270: * Test the flow when a runtime exception is thrown during a {@link org.testng.annotations.BeforeClass} call of a test.
0271: *
0272: * @see #assertInvocationOrder_testBeforeClass
0273: */
0274: @Test
0275: public void testUnitilsTestNG_testBeforeClass() throws Exception {
0276: tracingTestListener
0277: .setExceptionMethod(TEST_BEFORE_CLASS, false);
0278: TestListenerAdapter testListenerAdapter = performTestNGTest();
0279:
0280: assertInvocationOrder_testBeforeClass("TestNG",
0281: tracingTestListener);
0282: assertEquals(1, testListenerAdapter.getFailedTests().size());
0283: }
0284:
0285: /**
0286: * Test the flow when a runtime exception is thrown during a {@link TestListener#beforeTestSetUp} call of a module.
0287: *
0288: * @see #assertInvocationOrder_beforeTestSetUp
0289: */
0290: @Test
0291: public void testUnitilsJUnit3_beforeTestSetUp_RuntimeException() {
0292: tracingTestListener.setExceptionMethod(BEFORE_TEST_SET_UP,
0293: false);
0294: TestResult result = TestRunner.run(new TestSuite(
0295: UnitilsJUnit3Test_TestClass1.class));
0296:
0297: assertInvocationOrder_beforeTestSetUp("JUnit3",
0298: tracingTestListener);
0299: assertEquals(0, result.failureCount());
0300: assertEquals(2, result.errorCount());
0301: }
0302:
0303: /**
0304: * Test the flow when an assertion error is thrown during a {@link TestListener#beforeTestSetUp} call of a module.
0305: *
0306: * @see #assertInvocationOrder_beforeTestSetUp
0307: */
0308: @Test
0309: public void testUnitilsJUnit3_beforeTestSetUp_AssertionFailedError() {
0310: tracingTestListener
0311: .setExceptionMethod(BEFORE_TEST_SET_UP, true);
0312: TestResult result = TestRunner.run(new TestSuite(
0313: UnitilsJUnit3Test_TestClass1.class));
0314:
0315: assertInvocationOrder_beforeTestSetUp("JUnit3",
0316: tracingTestListener);
0317: assertEquals(2, result.failureCount());
0318: assertEquals(0, result.errorCount());
0319: }
0320:
0321: /**
0322: * Test the flow when a runtime exception is thrown during a {@link TestListener#beforeTestSetUp} call of a module.
0323: *
0324: * @see #assertInvocationOrder_beforeTestSetUp
0325: */
0326: @Test
0327: public void testUnitilsJUnit4_beforeTestSetUp() throws Exception {
0328: tracingTestListener.setExceptionMethod(BEFORE_TEST_SET_UP,
0329: false);
0330: Result result = performJUnit4Test();
0331:
0332: assertInvocationOrder_beforeTestSetUp("JUnit4",
0333: tracingTestListener);
0334: assertEquals(2, result.getRunCount());
0335: assertEquals(2, result.getFailureCount());
0336: assertEquals(1, result.getIgnoreCount());
0337: }
0338:
0339: /**
0340: * Test the flow when a runtime exception is thrown during a {@link TestListener#beforeTestSetUp} call of a module.
0341: *
0342: * @see #assertInvocationOrder_beforeTestSetUp
0343: */
0344: @Test
0345: public void testUnitilsTestNG_beforeTestSetUp() {
0346: tracingTestListener.setExceptionMethod(BEFORE_TEST_SET_UP,
0347: false);
0348: TestListenerAdapter testListenerAdapter = performTestNGTest();
0349:
0350: assertInvocationOrder_beforeTestSetUp("TestNG",
0351: tracingTestListener);
0352: assertEquals(1, testListenerAdapter.getFailedTests().size());
0353: }
0354:
0355: /**
0356: * Test the flow when a runtime exception is thrown during a {@link junit.framework.TestCase#setUp} call of a test.
0357: *
0358: * @see #assertInvocationOrder_testSetUp
0359: */
0360: @Test
0361: public void testUnitilsJUnit3_testSetUp_RuntimeException() {
0362: tracingTestListener.setExceptionMethod(TEST_SET_UP, false);
0363: TestResult result = TestRunner.run(new TestSuite(
0364: UnitilsJUnit3Test_TestClass1.class));
0365:
0366: assertInvocationOrder_testSetUp("JUnit3", tracingTestListener);
0367: assertEquals(0, result.failureCount());
0368: assertEquals(2, result.errorCount());
0369: }
0370:
0371: /**
0372: * Test the flow when an assertion error is thrown during a {@link junit.framework.TestCase#setUp} call of a test.
0373: *
0374: * @see #assertInvocationOrder_testSetUp
0375: */
0376: @Test
0377: public void testUnitilsJUnit3_testSetUp_AssertionFailedError() {
0378: tracingTestListener.setExceptionMethod(TEST_SET_UP, true);
0379: TestResult result = TestRunner.run(new TestSuite(
0380: UnitilsJUnit3Test_TestClass1.class));
0381:
0382: assertInvocationOrder_testSetUp("JUnit3", tracingTestListener);
0383: assertEquals(2, result.failureCount());
0384: assertEquals(0, result.errorCount());
0385: }
0386:
0387: /**
0388: * Test the flow when a runtime exception is thrown during a {@link org.junit.Before} call of a test.
0389: *
0390: * @see #assertInvocationOrder_testSetUp
0391: */
0392: @Test
0393: public void testUnitilsJUnit4_testSetUp() throws Exception {
0394: tracingTestListener.setExceptionMethod(TEST_SET_UP, false);
0395: Result result = performJUnit4Test();
0396:
0397: assertInvocationOrder_testSetUp("JUnit4", tracingTestListener);
0398: assertEquals(2, result.getRunCount());
0399: assertEquals(2, result.getFailureCount());
0400: assertEquals(1, result.getIgnoreCount());
0401: }
0402:
0403: /**
0404: * Test the flow when a runtime exception is thrown during a {@link org.testng.annotations.BeforeMethod} call of a test.
0405: *
0406: * @see #assertInvocationOrder_testSetUp
0407: */
0408: @Test
0409: public void testUnitilsTestNG_testSetUp() throws Exception {
0410: tracingTestListener.setExceptionMethod(TEST_SET_UP, false);
0411: TestListenerAdapter testListenerAdapter = performTestNGTest();
0412:
0413: assertInvocationOrder_testSetUp("TestNG", tracingTestListener);
0414: assertEquals(1, testListenerAdapter.getFailedTests().size());
0415: }
0416:
0417: /**
0418: * Test the flow when a runtime exception is thrown during a {@link TestListener#beforeTestMethod} call of a module.
0419: *
0420: * @see #assertInvocationOrder_beforeTestMethod
0421: */
0422: @Test
0423: public void testUnitilsJUnit3_beforeTestMethod_RuntimeException() {
0424: tracingTestListener.setExceptionMethod(BEFORE_TEST_METHOD,
0425: false);
0426: TestResult result = TestRunner.run(new TestSuite(
0427: UnitilsJUnit3Test_TestClass1.class));
0428:
0429: assertInvocationOrder_beforeTestMethod("JUnit3",
0430: tracingTestListener);
0431: assertEquals(0, result.failureCount());
0432: assertEquals(2, result.errorCount());
0433: }
0434:
0435: /**
0436: * Test the flow when an assertion error is thrown during a {@link TestListener#beforeTestMethod} call of a module.
0437: *
0438: * @see #assertInvocationOrder_beforeTestMethod
0439: */
0440: @Test
0441: public void testUnitilsJUnit3_beforeTestMethod_AssertionFailedError() {
0442: tracingTestListener
0443: .setExceptionMethod(BEFORE_TEST_METHOD, true);
0444: TestResult result = TestRunner.run(new TestSuite(
0445: UnitilsJUnit3Test_TestClass1.class));
0446:
0447: assertInvocationOrder_beforeTestMethod("JUnit3",
0448: tracingTestListener);
0449: assertEquals(2, result.failureCount()); // failures instead of errors
0450: assertEquals(0, result.errorCount());
0451: }
0452:
0453: /**
0454: * Test the flow when a runtime exception is thrown during a {@link TestListener#beforeTestMethod} call of a module.
0455: *
0456: * @see #assertInvocationOrder_beforeTestMethod
0457: */
0458: @Test
0459: public void testUnitilsJUnit4_beforeTestMethod() throws Exception {
0460: tracingTestListener.setExceptionMethod(BEFORE_TEST_METHOD,
0461: false);
0462: Result result = performJUnit4Test();
0463:
0464: assertInvocationOrder_beforeTestMethod("JUnit4",
0465: tracingTestListener);
0466: assertEquals(2, result.getRunCount());
0467: assertEquals(2, result.getFailureCount());
0468: assertEquals(1, result.getIgnoreCount());
0469: }
0470:
0471: /**
0472: * Test the flow when a runtime exception is thrown during a {@link TestListener#beforeTestMethod} call of a module.
0473: *
0474: * @see #assertInvocationOrder_beforeTestMethod
0475: */
0476: @Test
0477: public void testUnitilsTestNG_beforeTestMethod() {
0478: tracingTestListener.setExceptionMethod(BEFORE_TEST_METHOD,
0479: false);
0480: TestListenerAdapter testListenerAdapter = performTestNGTest();
0481:
0482: assertInvocationOrder_beforeTestMethod("TestNG",
0483: tracingTestListener);
0484: assertEquals(2, testListenerAdapter.getFailedTests().size());
0485: }
0486:
0487: /**
0488: * Test the flow when a runtime exception is thrown during a test.
0489: *
0490: * @see #assertInvocationOrder
0491: */
0492: @Test
0493: public void testUnitilsJUnit3_testMethod_RuntimeException() {
0494: tracingTestListener.setExceptionMethod(TEST_METHOD, false);
0495: TestResult result = TestRunner.run(new TestSuite(
0496: UnitilsJUnit3Test_TestClass1.class));
0497:
0498: assertInvocationOrder("JUnit3", tracingTestListener);
0499: assertEquals(0, result.failureCount());
0500: assertEquals(2, result.errorCount());
0501: }
0502:
0503: /**
0504: * Test the flow when an assertion error is thrown during a test.
0505: *
0506: * @see #assertInvocationOrder
0507: */
0508: @Test
0509: public void testUnitilsJUnit3_testMethod_AssertionFailedError() {
0510: tracingTestListener.setExceptionMethod(TEST_METHOD, true);
0511: TestResult result = TestRunner.run(new TestSuite(
0512: UnitilsJUnit3Test_TestClass1.class));
0513:
0514: assertInvocationOrder("JUnit3", tracingTestListener);
0515: assertEquals(2, result.failureCount());
0516: assertEquals(0, result.errorCount());
0517: }
0518:
0519: /**
0520: * Test the flow when a runtime exception is thrown during a test.
0521: *
0522: * @see #assertInvocationOrder
0523: */
0524: @Test
0525: public void testUnitilsJUnit4_testMethod() throws Exception {
0526: tracingTestListener.setExceptionMethod(TEST_METHOD, false);
0527: Result result = performJUnit4Test();
0528:
0529: assertInvocationOrder("JUnit4", tracingTestListener);
0530: assertEquals(2, result.getRunCount());
0531: assertEquals(2, result.getFailureCount());
0532: assertEquals(1, result.getIgnoreCount());
0533: }
0534:
0535: /**
0536: * Test the flow when a runtime exception is thrown during a test.
0537: *
0538: * @see #assertInvocationOrder
0539: */
0540: @Test
0541: public void testUnitilsTestNG_testMethod() {
0542: tracingTestListener.setExceptionMethod(TEST_METHOD, false);
0543: TestListenerAdapter testListenerAdapter = performTestNGTest();
0544:
0545: assertInvocationOrder("TestNG", tracingTestListener);
0546: assertEquals(2, testListenerAdapter.getFailedTests().size());
0547: }
0548:
0549: /**
0550: * Test the flow when a runtime exception is thrown during a {@link TestListener#afterTestMethod} call of a module.
0551: *
0552: * @see #assertInvocationOrder
0553: */
0554: @Test
0555: public void testUnitilsJUnit3_afterTestMethod_RuntimeException() {
0556: tracingTestListener
0557: .setExceptionMethod(AFTER_TEST_METHOD, false);
0558: TestResult result = TestRunner.run(new TestSuite(
0559: UnitilsJUnit3Test_TestClass1.class));
0560:
0561: assertInvocationOrder("JUnit3", tracingTestListener);
0562: assertEquals(0, result.failureCount());
0563: assertEquals(2, result.errorCount());
0564: }
0565:
0566: /**
0567: * Test the flow when an assertion error is thrown during a {@link TestListener#afterTestMethod} call of a module.
0568: *
0569: * @see #assertInvocationOrder
0570: */
0571: @Test
0572: public void testUnitilsJUnit3_afterTestMethod_AssertionFailedError() {
0573: tracingTestListener.setExceptionMethod(AFTER_TEST_METHOD, true);
0574: TestResult result = TestRunner.run(new TestSuite(
0575: UnitilsJUnit3Test_TestClass1.class));
0576:
0577: assertInvocationOrder("JUnit3", tracingTestListener);
0578: assertEquals(2, result.failureCount());
0579: assertEquals(0, result.errorCount());
0580: }
0581:
0582: /**
0583: * Test the flow when a runtime exception is thrown during a {@link TestListener#afterTestMethod} call of a module.
0584: *
0585: * @see #assertInvocationOrder
0586: */
0587: @Test
0588: public void testUnitilsJUnit4_afterTestMethod() throws Exception {
0589: tracingTestListener
0590: .setExceptionMethod(AFTER_TEST_METHOD, false);
0591: Result result = performJUnit4Test();
0592:
0593: assertInvocationOrder("JUnit4", tracingTestListener);
0594: assertEquals(2, result.getRunCount());
0595: assertEquals(2, result.getFailureCount());
0596: assertEquals(1, result.getIgnoreCount());
0597: }
0598:
0599: /**
0600: * Test the flow when a runtime exception is thrown during a {@link TestListener#afterTestMethod} call of a module.
0601: *
0602: * @see #assertInvocationOrder
0603: */
0604: @Test
0605: public void testUnitilsTestNG_afterTestMethod() {
0606: tracingTestListener
0607: .setExceptionMethod(AFTER_TEST_METHOD, false);
0608: TestListenerAdapter testListenerAdapter = performTestNGTest();
0609:
0610: assertInvocationOrder("TestNG", tracingTestListener);
0611: assertEquals(2, testListenerAdapter.getFailedTests().size());
0612: }
0613:
0614: /**
0615: * Test the flow when a runtime exception is thrown during a {@link junit.framework.TestCase#tearDown} call of a test.
0616: *
0617: * @see #assertInvocationOrder
0618: */
0619: @Test
0620: public void testUnitilsJUnit3_testTearDown_RuntimeException() {
0621: tracingTestListener.setExceptionMethod(TEST_TEAR_DOWN, false);
0622: TestResult result = TestRunner.run(new TestSuite(
0623: UnitilsJUnit3Test_TestClass1.class));
0624:
0625: assertInvocationOrder_testTearDown("JUnit3",
0626: tracingTestListener);
0627: assertEquals(0, result.failureCount());
0628: assertEquals(2, result.errorCount());
0629: }
0630:
0631: /**
0632: * Test the flow when an assertion error is thrown during a {@link junit.framework.TestCase#tearDown} call of a test.
0633: *
0634: * @see #assertInvocationOrder
0635: */
0636: @Test
0637: public void testUnitilsJUnit3_testTearDown_AssertionFailedError() {
0638: tracingTestListener.setExceptionMethod(TEST_TEAR_DOWN, true);
0639: TestResult result = TestRunner.run(new TestSuite(
0640: UnitilsJUnit3Test_TestClass1.class));
0641:
0642: assertInvocationOrder_testTearDown("JUnit3",
0643: tracingTestListener);
0644: assertEquals(2, result.failureCount());
0645: assertEquals(0, result.errorCount());
0646: }
0647:
0648: /**
0649: * Test the flow when a runtime exception is thrown during a {@link org.junit.After} call of a test.
0650: *
0651: * @see #assertInvocationOrder
0652: */
0653: @Test
0654: public void testUnitilsJUnit4_testTearDown() throws Exception {
0655: tracingTestListener.setExceptionMethod(TEST_TEAR_DOWN, false);
0656: Result result = performJUnit4Test();
0657:
0658: assertInvocationOrder_testTearDown("JUnit4",
0659: tracingTestListener);
0660: assertEquals(2, result.getRunCount());
0661: assertEquals(2, result.getFailureCount());
0662: assertEquals(1, result.getIgnoreCount());
0663: }
0664:
0665: /**
0666: * Test the flow when a runtime exception is thrown during a {@link org.testng.annotations.AfterMethod} call of a test.
0667: *
0668: * @see #assertInvocationOrder
0669: */
0670: @Test
0671: public void testUnitilsTestNG_testTearDown() throws Exception {
0672: tracingTestListener.setExceptionMethod(TEST_TEAR_DOWN, false);
0673: TestListenerAdapter testListenerAdapter = performTestNGTest();
0674:
0675: assertInvocationOrder_testTearDown("TestNG",
0676: tracingTestListener);
0677: assertEquals(1, testListenerAdapter.getFailedTests().size());
0678: }
0679:
0680: /**
0681: * Test the flow when a runtime exception is thrown during a {@link TestListener#afterTestTearDown} call of a module.
0682: *
0683: * @see #assertInvocationOrder_afterTestTearDown
0684: */
0685: @Test
0686: public void testUnitilsJUnit3_afterTestTearDown_RuntimeException() {
0687: tracingTestListener.setExceptionMethod(AFTER_TEST_TEAR_DOWN,
0688: false);
0689: TestResult result = TestRunner.run(new TestSuite(
0690: UnitilsJUnit3Test_TestClass1.class));
0691:
0692: assertInvocationOrder_afterTestTearDown("JUnit3",
0693: tracingTestListener);
0694: assertEquals(0, result.failureCount());
0695: assertEquals(2, result.errorCount());
0696: }
0697:
0698: /**
0699: * Test the flow when an assertion error is thrown during a {@link TestListener#afterTestTearDown} call of a module.
0700: *
0701: * @see #assertInvocationOrder_afterTestTearDown
0702: */
0703: @Test
0704: public void testUnitilsJUnit3_afterTestTearDown_AssertionFailedError() {
0705: tracingTestListener.setExceptionMethod(AFTER_TEST_TEAR_DOWN,
0706: true);
0707: TestResult result = TestRunner.run(new TestSuite(
0708: UnitilsJUnit3Test_TestClass1.class));
0709:
0710: assertInvocationOrder_afterTestTearDown("JUnit3",
0711: tracingTestListener);
0712: assertEquals(2, result.failureCount());
0713: assertEquals(0, result.errorCount());
0714: }
0715:
0716: /**
0717: * Test the flow when a runtime exception is thrown during a {@link TestListener#afterTestTearDown} call of a module.
0718: *
0719: * @see #assertInvocationOrder_afterTestTearDown
0720: */
0721: @Test
0722: public void testUnitilsJUnit4_afterTestTearDown() throws Exception {
0723: tracingTestListener.setExceptionMethod(AFTER_TEST_TEAR_DOWN,
0724: false);
0725: Result result = performJUnit4Test();
0726:
0727: assertInvocationOrder_afterTestTearDown("JUnit4",
0728: tracingTestListener);
0729: assertEquals(2, result.getRunCount());
0730: assertEquals(2, result.getFailureCount());
0731: assertEquals(1, result.getIgnoreCount());
0732: }
0733:
0734: /**
0735: * Test the flow when a runtime exception is thrown during a {@link TestListener#afterTestTearDown} call of a module.
0736: *
0737: * @see #assertInvocationOrder_afterTestTearDown
0738: */
0739: @Test
0740: public void testUnitilsTestNG_afterTestTearDown() {
0741: tracingTestListener.setExceptionMethod(AFTER_TEST_TEAR_DOWN,
0742: false);
0743: TestListenerAdapter testListenerAdapter = performTestNGTest();
0744:
0745: assertInvocationOrder_afterTestTearDown("TestNG",
0746: tracingTestListener);
0747: assertEquals(1, testListenerAdapter.getFailedTests().size());
0748: }
0749:
0750: // no JUnit 3 versions of testAfterClass (does not exist for JUnit3)
0751:
0752: /**
0753: * Test the flow when a runtime exception is thrown during a {@link org.junit.AfterClass} call of a test.
0754: *
0755: * @see #assertInvocationOrder
0756: */
0757: @Test
0758: public void testUnitilsJUnit4_testAfterClass() throws Exception {
0759: tracingTestListener.setExceptionMethod(TEST_AFTER_CLASS, false);
0760: Result result = performJUnit4Test();
0761:
0762: assertInvocationOrder("JUnit4", tracingTestListener);
0763: assertEquals(2, result.getRunCount());
0764: assertEquals(1, result.getFailureCount());
0765: assertEquals(1, result.getIgnoreCount());
0766: }
0767:
0768: /**
0769: * Test the flow when a runtime exception is thrown during a {@link org.testng.annotations.AfterClass} call of a test.
0770: *
0771: * @see #assertInvocationOrder
0772: */
0773: @Test
0774: public void testUnitilsTestNG_testAfterClass() throws Exception {
0775: tracingTestListener.setExceptionMethod(TEST_AFTER_CLASS, false);
0776: TestListenerAdapter testListenerAdapter = performTestNGTest();
0777:
0778: assertInvocationOrder("TestNG", tracingTestListener);
0779: assertEquals(1, testListenerAdapter.getFailedTests().size());
0780: }
0781:
0782: /**
0783: * Test the flow when a runtime exception is thrown during a {@link TestListener#afterTestClass} call of a module.
0784: *
0785: * @see #assertInvocationOrder
0786: */
0787: @Test
0788: public void testUnitilsJUnit4_afterTestClass() throws Exception {
0789: tracingTestListener.setExceptionMethod(AFTER_TEST_CLASS, false);
0790: Result result = performJUnit4Test();
0791:
0792: assertInvocationOrder("JUnit4", tracingTestListener);
0793: assertEquals(2, result.getRunCount());
0794: assertEquals(1, result.getFailureCount());
0795: assertEquals(1, result.getIgnoreCount());
0796: }
0797:
0798: /**
0799: * Test the flow when a runtime exception is thrown during a {@link TestListener#afterTestClass} call of a module.
0800: *
0801: * @see #assertInvocationOrder
0802: */
0803: @Test
0804: public void testUnitilsTestNG_afterTestClass() {
0805: tracingTestListener.setExceptionMethod(AFTER_TEST_CLASS, false);
0806: TestListenerAdapter testListenerAdapter = performTestNGTest();
0807:
0808: assertInvocationOrder("TestNG", tracingTestListener);
0809: assertEquals(1, testListenerAdapter.getFailedTests().size());
0810: }
0811:
0812: /**
0813: * Test the flow when a runtime exception is thrown during a {@link TestListener#afterAll} call of a module.
0814: *
0815: * @see #assertInvocationOrder
0816: */
0817: @Test
0818: public void testUnitilsTestNG_afterAll() {
0819: tracingTestListener.setExceptionMethod(AFTER_ALL, false);
0820: TestListenerAdapter testListenerAdapter = performTestNGTest();
0821:
0822: assertInvocationOrder("TestNG", tracingTestListener);
0823: assertEquals(1, testListenerAdapter.getFailedTests().size());
0824: }
0825:
0826: /**
0827: * Asserts the flow when an exception is thrown during a {@link TestListener#beforeAll} call of a module.
0828: *
0829: * @param type JUnit3, JUnit4 or TestNG
0830: * @param tracingTestListener the recorded invocations
0831: */
0832: private void assertInvocationOrder_beforeAll(String type,
0833: TracingTestListener tracingTestListener) {
0834: Iterator<?> iterator = tracingTestListener.getCallList()
0835: .iterator();
0836:
0837: if ("JUnit3".equals(type)) {
0838: assertEquals("[Unitils] beforeAll", iterator.next());
0839: // afterAll will be called when the runtime exits
0840: }
0841: if ("JUnit4".equals(type)) {
0842: assertEquals("[Unitils] beforeAll", iterator.next());
0843: // afterAll will be called when the runtime exits
0844: }
0845: if ("TestNG".equals(type)) {
0846: assertEquals("[Unitils] beforeAll", iterator.next());
0847: assertEquals("[Unitils] afterAll", iterator.next());
0848: }
0849: assertFalse(iterator.hasNext());
0850: }
0851:
0852: /**
0853: * Asserts the flow when an exception is thrown during a {@link TestListener#beforeTestClass} call of a module.
0854: *
0855: * @param type JUnit3, JUnit4 or TestNG
0856: * @param tracingTestListener the recorded invocations
0857: */
0858: private void assertInvocationOrder_beforeTestClass(String type,
0859: TracingTestListener tracingTestListener) {
0860: Iterator<?> iterator = tracingTestListener.getCallList()
0861: .iterator();
0862:
0863: if ("JUnit3".equals(type)) {
0864: assertEquals("[Unitils] beforeAll", iterator.next());
0865: assertEquals("[Unitils] beforeTestClass - TestClass1",
0866: iterator.next());
0867: assertEquals("[Unitils] beforeTestClass - TestClass1",
0868: iterator.next()); // 2 times, once for each test
0869: // The last afterTestClass will be called when the runtime exits
0870: // afterAll will be called when the runtime exits
0871: }
0872: if ("JUnit4".equals(type)) {
0873: assertEquals("[Unitils] beforeAll", iterator.next());
0874: assertEquals("[Unitils] beforeTestClass - TestClass1",
0875: iterator.next());
0876: assertEquals("[Unitils] afterTestClass - TestClass1",
0877: iterator.next());
0878: // afterAll will be called when the runtime exits
0879: }
0880: if ("TestNG".equals(type)) {
0881: assertEquals("[Unitils] beforeAll", iterator.next());
0882: assertEquals("[Unitils] beforeTestClass - TestClass1",
0883: iterator.next());
0884: assertEquals("[Unitils] afterTestClass - TestClass1",
0885: iterator.next());
0886: assertEquals("[Unitils] afterAll", iterator.next());
0887: }
0888: assertFalse(iterator.hasNext());
0889: }
0890:
0891: /**
0892: * Asserts the flow when an exception is thrown during a before class call of a test.
0893: *
0894: * @param type JUnit3, JUnit4 or TestNG
0895: * @param tracingTestListener the recorded invocations
0896: */
0897: private void assertInvocationOrder_testBeforeClass(String type,
0898: TracingTestListener tracingTestListener) {
0899: Iterator<?> iterator = tracingTestListener.getCallList()
0900: .iterator();
0901:
0902: // does not exist for JUnit3
0903: if ("JUnit4".equals(type)) {
0904: assertEquals("[Unitils] beforeAll", iterator.next());
0905: assertEquals("[Unitils] beforeTestClass - TestClass1",
0906: iterator.next());
0907: assertEquals("[Test] testBeforeClass - TestClass1",
0908: iterator.next());
0909: assertEquals("[Test] testAfterClass - TestClass1",
0910: iterator.next());
0911: assertEquals("[Unitils] afterTestClass - TestClass1",
0912: iterator.next());
0913: // afterAll will be called when the runtime exits
0914: }
0915: if ("TestNG".equals(type)) {
0916: assertEquals("[Unitils] beforeAll", iterator.next());
0917: assertEquals("[Unitils] beforeTestClass - TestClass1",
0918: iterator.next());
0919: assertEquals("[Test] testBeforeClass - TestClass1",
0920: iterator.next());
0921: // setup and teardown are still called even though tests are skipped
0922: assertEquals("[Unitils] beforeTestSetUp - TestClass1",
0923: iterator.next()); // difference with JUnit
0924: assertEquals("[Unitils] afterTestTearDown - TestClass1",
0925: iterator.next()); // difference with JUnit
0926: assertEquals("[Unitils] beforeTestSetUp - TestClass1",
0927: iterator.next()); // difference with JUnit
0928: assertEquals("[Unitils] afterTestTearDown - TestClass1",
0929: iterator.next()); // difference with JUnit
0930: // testAfterClass is skipped // difference with JUnit
0931: assertEquals("[Unitils] afterTestClass - TestClass1",
0932: iterator.next());
0933: assertEquals("[Unitils] afterAll", iterator.next());
0934: }
0935: assertFalse(iterator.hasNext());
0936: }
0937:
0938: /**
0939: * Asserts the flow when an exception is thrown during a {@link TestListener#beforeTestSetUp} call of a module.
0940: *
0941: * @param type JUnit3, JUnit4 or TestNG
0942: * @param tracingTestListener the recorded invocations
0943: */
0944: private void assertInvocationOrder_beforeTestSetUp(String type,
0945: TracingTestListener tracingTestListener) {
0946: Iterator<?> iterator = tracingTestListener.getCallList()
0947: .iterator();
0948:
0949: if ("JUnit3".equals(type)) {
0950: assertEquals("[Unitils] beforeAll", iterator.next());
0951: assertEquals("[Unitils] beforeTestClass - TestClass1",
0952: iterator.next());
0953: // testBeforeClass does not exist
0954: assertEquals("[Unitils] beforeTestSetUp - TestClass1",
0955: iterator.next());
0956: assertEquals("[Unitils] afterTestTearDown - TestClass1",
0957: iterator.next()); // still called
0958: assertEquals("[Unitils] beforeTestSetUp - TestClass1",
0959: iterator.next());
0960: assertEquals("[Unitils] afterTestTearDown - TestClass1",
0961: iterator.next()); // still called
0962: // testAfterClass does not exist
0963: // The last afterTestClass will be called when the runtime exits
0964: // afterAll will be called when the runtime exits
0965: }
0966: if ("JUnit4".equals(type)) {
0967: assertEquals("[Unitils] beforeAll", iterator.next());
0968: assertEquals("[Unitils] beforeTestClass - TestClass1",
0969: iterator.next());
0970: assertEquals("[Test] testBeforeClass - TestClass1",
0971: iterator.next());
0972: assertEquals("[Unitils] beforeTestSetUp - TestClass1",
0973: iterator.next());
0974: assertEquals("[Unitils] afterTestTearDown - TestClass1",
0975: iterator.next()); // still called
0976: assertEquals("[Unitils] beforeTestSetUp - TestClass1",
0977: iterator.next());
0978: assertEquals("[Unitils] afterTestTearDown - TestClass1",
0979: iterator.next()); // still called
0980: assertEquals("[Test] testAfterClass - TestClass1",
0981: iterator.next());
0982: assertEquals("[Unitils] afterTestClass - TestClass1",
0983: iterator.next());
0984: // afterAll will be called when the runtime exits
0985: }
0986: if ("TestNG".equals(type)) {
0987: assertEquals("[Unitils] beforeAll", iterator.next());
0988: assertEquals("[Unitils] beforeTestClass - TestClass1",
0989: iterator.next());
0990: assertEquals("[Test] testBeforeClass - TestClass1",
0991: iterator.next());
0992: assertEquals("[Unitils] beforeTestSetUp - TestClass1",
0993: iterator.next());
0994: assertEquals("[Unitils] afterTestTearDown - TestClass1",
0995: iterator.next()); // still called
0996: // second beforeTestSetUp is skipped // difference with JUnit
0997: // second afterTestTearDown is skipped // difference with JUnit
0998: // testAfterClass is skipped // difference with JUnit
0999: assertEquals("[Unitils] afterTestClass - TestClass1",
1000: iterator.next());
1001: assertEquals("[Unitils] afterAll", iterator.next());
1002: }
1003: assertFalse(iterator.hasNext());
1004: }
1005:
1006: /**
1007: * Asserts the flow when an exception is thrown during a setUp call of a test.
1008: *
1009: * @param type JUnit3, JUnit4 or TestNG
1010: * @param tracingTestListener the recorded invocations
1011: */
1012: private void assertInvocationOrder_testSetUp(String type,
1013: TracingTestListener tracingTestListener) {
1014: Iterator<?> iterator = tracingTestListener.getCallList()
1015: .iterator();
1016:
1017: if ("JUnit3".equals(type)) {
1018: assertEquals("[Unitils] beforeAll", iterator.next());
1019: assertEquals("[Unitils] beforeTestClass - TestClass1",
1020: iterator.next());
1021: // testBeforeClass does not exist
1022: assertEquals("[Unitils] beforeTestSetUp - TestClass1",
1023: iterator.next());
1024: assertEquals("[Test] testSetUp - TestClass1",
1025: iterator.next());
1026: // testTearDown is skipped // difference with JUnit4
1027: assertEquals("[Unitils] afterTestTearDown - TestClass1",
1028: iterator.next());
1029: assertEquals("[Unitils] beforeTestSetUp - TestClass1",
1030: iterator.next());
1031: assertEquals("[Test] testSetUp - TestClass1",
1032: iterator.next());
1033: // testTearDown is skipped // difference with JUnit4
1034: assertEquals("[Unitils] afterTestTearDown - TestClass1",
1035: iterator.next());
1036: // testAfterClass does not exist
1037: // The last afterTestClass will be called when the runtime exits
1038: // afterAll will be called when the runtime exits
1039: }
1040: if ("JUnit4".equals(type)) {
1041: assertEquals("[Unitils] beforeAll", iterator.next());
1042: assertEquals("[Unitils] beforeTestClass - TestClass1",
1043: iterator.next());
1044: assertEquals("[Test] testBeforeClass - TestClass1",
1045: iterator.next());
1046: assertEquals("[Unitils] beforeTestSetUp - TestClass1",
1047: iterator.next());
1048: assertEquals("[Test] testSetUp - TestClass1",
1049: iterator.next());
1050: assertEquals("[Test] testTearDown - TestClass1",
1051: iterator.next()); // still called
1052: assertEquals("[Unitils] afterTestTearDown - TestClass1",
1053: iterator.next());
1054: assertEquals("[Unitils] beforeTestSetUp - TestClass1",
1055: iterator.next());
1056: assertEquals("[Test] testSetUp - TestClass1",
1057: iterator.next());
1058: assertEquals("[Test] testTearDown - TestClass1",
1059: iterator.next()); // still called
1060: assertEquals("[Unitils] afterTestTearDown - TestClass1",
1061: iterator.next());
1062: assertEquals("[Test] testAfterClass - TestClass1",
1063: iterator.next());
1064: assertEquals("[Unitils] afterTestClass - TestClass1",
1065: iterator.next());
1066: // afterAll will be called when the runtime exits
1067: }
1068: if ("TestNG".equals(type)) {
1069: assertEquals("[Unitils] beforeAll", iterator.next());
1070: assertEquals("[Unitils] beforeTestClass - TestClass1",
1071: iterator.next());
1072: assertEquals("[Test] testBeforeClass - TestClass1",
1073: iterator.next());
1074: assertEquals("[Unitils] beforeTestSetUp - TestClass1",
1075: iterator.next());
1076: assertEquals("[Test] testSetUp - TestClass1",
1077: iterator.next());
1078: // testTearDown is skipped // difference with JUnit4
1079: assertEquals("[Unitils] afterTestTearDown - TestClass1",
1080: iterator.next());
1081: assertEquals("[Unitils] beforeTestSetUp - TestClass1",
1082: iterator.next());
1083: // second testSetUp is skipped // difference with JUnit
1084: // testTearDown is skipped // difference with JUnit4
1085: assertEquals("[Unitils] afterTestTearDown - TestClass1",
1086: iterator.next());
1087: // testAfterClass is skipped // difference with JUnit
1088: assertEquals("[Unitils] afterTestClass - TestClass1",
1089: iterator.next());
1090: assertEquals("[Unitils] afterAll", iterator.next());
1091: }
1092: assertFalse(iterator.hasNext());
1093: }
1094:
1095: /**
1096: * Asserts the flow when an exception is thrown during a {@link TestListener#beforeTestMethod} call of a module.
1097: *
1098: * @param type JUnit3, JUnit4 or TestNG
1099: * @param tracingTestListener the recorded invocations
1100: */
1101: private void assertInvocationOrder_beforeTestMethod(String type,
1102: TracingTestListener tracingTestListener) {
1103: Iterator<?> iterator = tracingTestListener.getCallList()
1104: .iterator();
1105:
1106: if ("JUnit3".equals(type)) {
1107: assertEquals("[Unitils] beforeAll", iterator.next());
1108: assertEquals("[Unitils] beforeTestClass - TestClass1",
1109: iterator.next());
1110: // testBeforeClass does not exist
1111: assertEquals("[Unitils] beforeTestSetUp - TestClass1",
1112: iterator.next());
1113: assertEquals("[Test] testSetUp - TestClass1",
1114: iterator.next());
1115: assertEquals("[Unitils] beforeTestMethod - TestClass1",
1116: iterator.next());
1117: assertEquals("[Unitils] afterTestMethod - TestClass1",
1118: iterator.next()); // still called
1119: assertEquals("[Test] testTearDown - TestClass1",
1120: iterator.next());
1121: assertEquals("[Unitils] afterTestTearDown - TestClass1",
1122: iterator.next());
1123: assertEquals("[Unitils] beforeTestSetUp - TestClass1",
1124: iterator.next());
1125: assertEquals("[Test] testSetUp - TestClass1",
1126: iterator.next());
1127: assertEquals("[Unitils] beforeTestMethod - TestClass1",
1128: iterator.next());
1129: assertEquals("[Unitils] afterTestMethod - TestClass1",
1130: iterator.next()); // still called
1131: assertEquals("[Test] testTearDown - TestClass1",
1132: iterator.next());
1133: assertEquals("[Unitils] afterTestTearDown - TestClass1",
1134: iterator.next());
1135: // testAfterClass does not exist
1136: // The last afterTestClass will be called when the runtime exits
1137: // afterAll will be called when the runtime exits
1138: }
1139: if ("JUnit4".equals(type)) {
1140: assertEquals("[Unitils] beforeAll", iterator.next());
1141: assertEquals("[Unitils] beforeTestClass - TestClass1",
1142: iterator.next());
1143: assertEquals("[Test] testBeforeClass - TestClass1",
1144: iterator.next());
1145: assertEquals("[Unitils] beforeTestSetUp - TestClass1",
1146: iterator.next());
1147: assertEquals("[Test] testSetUp - TestClass1",
1148: iterator.next());
1149: assertEquals("[Unitils] beforeTestMethod - TestClass1",
1150: iterator.next());
1151: assertEquals("[Unitils] afterTestMethod - TestClass1",
1152: iterator.next()); // still called
1153: assertEquals("[Test] testTearDown - TestClass1",
1154: iterator.next());
1155: assertEquals("[Unitils] afterTestTearDown - TestClass1",
1156: iterator.next());
1157: assertEquals("[Unitils] beforeTestSetUp - TestClass1",
1158: iterator.next());
1159: assertEquals("[Test] testSetUp - TestClass1",
1160: iterator.next());
1161: assertEquals("[Unitils] beforeTestMethod - TestClass1",
1162: iterator.next());
1163: assertEquals("[Unitils] afterTestMethod - TestClass1",
1164: iterator.next()); // still called
1165: assertEquals("[Test] testTearDown - TestClass1",
1166: iterator.next());
1167: assertEquals("[Unitils] afterTestTearDown - TestClass1",
1168: iterator.next());
1169: assertEquals("[Test] testAfterClass - TestClass1",
1170: iterator.next());
1171: assertEquals("[Unitils] afterTestClass - TestClass1",
1172: iterator.next());
1173: // afterAll will be called when the runtime exits
1174: }
1175: if ("TestNG".equals(type)) {
1176: assertEquals("[Unitils] beforeAll", iterator.next());
1177: assertEquals("[Unitils] beforeTestClass - TestClass1",
1178: iterator.next());
1179: assertEquals("[Test] testBeforeClass - TestClass1",
1180: iterator.next());
1181: assertEquals("[Unitils] beforeTestSetUp - TestClass1",
1182: iterator.next());
1183: assertEquals("[Test] testSetUp - TestClass1",
1184: iterator.next());
1185: assertEquals("[Unitils] beforeTestMethod - TestClass1",
1186: iterator.next());
1187: assertEquals("[Unitils] afterTestMethod - TestClass1",
1188: iterator.next()); // still called
1189: assertEquals("[Test] testTearDown - TestClass1",
1190: iterator.next());
1191: assertEquals("[Unitils] afterTestTearDown - TestClass1",
1192: iterator.next());
1193: assertEquals("[Unitils] beforeTestSetUp - TestClass1",
1194: iterator.next());
1195: assertEquals("[Test] testSetUp - TestClass1",
1196: iterator.next());
1197: assertEquals("[Unitils] beforeTestMethod - TestClass1",
1198: iterator.next());
1199: assertEquals("[Unitils] afterTestMethod - TestClass1",
1200: iterator.next()); // still called
1201: assertEquals("[Test] testTearDown - TestClass1",
1202: iterator.next());
1203: assertEquals("[Unitils] afterTestTearDown - TestClass1",
1204: iterator.next());
1205: assertEquals("[Test] testAfterClass - TestClass1",
1206: iterator.next());
1207: assertEquals("[Unitils] afterTestClass - TestClass1",
1208: iterator.next());
1209: assertEquals("[Unitils] afterAll", iterator.next());
1210: }
1211: assertFalse(iterator.hasNext());
1212: }
1213:
1214: /**
1215: * Asserts the main flow of invocation calls.
1216: *
1217: * @param type JUnit3, JUnit4 or TestNG
1218: * @param tracingTestListener the recorded invocations
1219: */
1220: private void assertInvocationOrder(String type,
1221: TracingTestListener tracingTestListener) {
1222: Iterator<?> iterator = tracingTestListener.getCallList()
1223: .iterator();
1224:
1225: if ("JUnit3".equals(type)) {
1226: assertEquals("[Unitils] beforeAll", iterator.next());
1227: assertEquals("[Unitils] beforeTestClass - TestClass1",
1228: iterator.next());
1229: // testBeforeClass does not exist
1230: assertEquals("[Unitils] beforeTestSetUp - TestClass1",
1231: iterator.next());
1232: assertEquals("[Test] testSetUp - TestClass1",
1233: iterator.next());
1234: assertEquals("[Unitils] beforeTestMethod - TestClass1",
1235: iterator.next());
1236: assertEquals("[Test] testMethod - TestClass1",
1237: iterator.next());
1238: assertEquals("[Unitils] afterTestMethod - TestClass1",
1239: iterator.next());
1240: assertEquals("[Test] testTearDown - TestClass1",
1241: iterator.next());
1242: assertEquals("[Unitils] afterTestTearDown - TestClass1",
1243: iterator.next());
1244: assertEquals("[Unitils] beforeTestSetUp - TestClass1",
1245: iterator.next());
1246: assertEquals("[Test] testSetUp - TestClass1",
1247: iterator.next());
1248: assertEquals("[Unitils] beforeTestMethod - TestClass1",
1249: iterator.next());
1250: assertEquals("[Test] testMethod - TestClass1",
1251: iterator.next());
1252: assertEquals("[Unitils] afterTestMethod - TestClass1",
1253: iterator.next());
1254: assertEquals("[Test] testTearDown - TestClass1",
1255: iterator.next());
1256: assertEquals("[Unitils] afterTestTearDown - TestClass1",
1257: iterator.next());
1258: // testAfterClass does not exist
1259: // The last afterTestClass will be called when the runtime exits
1260: // afterAll will be called when the runtime exits
1261: }
1262: if ("JUnit4".equals(type)) {
1263: assertEquals("[Unitils] beforeAll", iterator.next());
1264: assertEquals("[Unitils] beforeTestClass - TestClass1",
1265: iterator.next());
1266: assertEquals("[Test] testBeforeClass - TestClass1",
1267: iterator.next());
1268: assertEquals("[Unitils] beforeTestSetUp - TestClass1",
1269: iterator.next());
1270: assertEquals("[Test] testSetUp - TestClass1",
1271: iterator.next());
1272: assertEquals("[Unitils] beforeTestMethod - TestClass1",
1273: iterator.next());
1274: assertEquals("[Test] testMethod - TestClass1",
1275: iterator.next());
1276: assertEquals("[Unitils] afterTestMethod - TestClass1",
1277: iterator.next());
1278: assertEquals("[Test] testTearDown - TestClass1",
1279: iterator.next());
1280: assertEquals("[Unitils] afterTestTearDown - TestClass1",
1281: iterator.next());
1282: assertEquals("[Unitils] beforeTestSetUp - TestClass1",
1283: iterator.next());
1284: assertEquals("[Test] testSetUp - TestClass1",
1285: iterator.next());
1286: assertEquals("[Unitils] beforeTestMethod - TestClass1",
1287: iterator.next());
1288: assertEquals("[Test] testMethod - TestClass1",
1289: iterator.next());
1290: assertEquals("[Unitils] afterTestMethod - TestClass1",
1291: iterator.next());
1292: assertEquals("[Test] testTearDown - TestClass1",
1293: iterator.next());
1294: assertEquals("[Unitils] afterTestTearDown - TestClass1",
1295: iterator.next());
1296: assertEquals("[Test] testAfterClass - TestClass1",
1297: iterator.next());
1298: assertEquals("[Unitils] afterTestClass - TestClass1",
1299: iterator.next());
1300: // afterAll will be called when the runtime exits
1301: }
1302: if ("TestNG".equals(type)) {
1303: assertEquals("[Unitils] beforeAll", iterator.next());
1304: assertEquals("[Unitils] beforeTestClass - TestClass1",
1305: iterator.next());
1306: assertEquals("[Test] testBeforeClass - TestClass1",
1307: iterator.next());
1308: assertEquals("[Unitils] beforeTestSetUp - TestClass1",
1309: iterator.next());
1310: assertEquals("[Test] testSetUp - TestClass1",
1311: iterator.next());
1312: assertEquals("[Unitils] beforeTestMethod - TestClass1",
1313: iterator.next());
1314: assertEquals("[Test] testMethod - TestClass1",
1315: iterator.next());
1316: assertEquals("[Unitils] afterTestMethod - TestClass1",
1317: iterator.next());
1318: assertEquals("[Test] testTearDown - TestClass1",
1319: iterator.next());
1320: assertEquals("[Unitils] afterTestTearDown - TestClass1",
1321: iterator.next());
1322: assertEquals("[Unitils] beforeTestSetUp - TestClass1",
1323: iterator.next());
1324: assertEquals("[Test] testSetUp - TestClass1",
1325: iterator.next());
1326: assertEquals("[Unitils] beforeTestMethod - TestClass1",
1327: iterator.next());
1328: assertEquals("[Test] testMethod - TestClass1",
1329: iterator.next());
1330: assertEquals("[Unitils] afterTestMethod - TestClass1",
1331: iterator.next());
1332: assertEquals("[Test] testTearDown - TestClass1",
1333: iterator.next());
1334: assertEquals("[Unitils] afterTestTearDown - TestClass1",
1335: iterator.next());
1336: assertEquals("[Test] testAfterClass - TestClass1",
1337: iterator.next());
1338: assertEquals("[Unitils] afterTestClass - TestClass1",
1339: iterator.next());
1340: assertEquals("[Unitils] afterAll", iterator.next());
1341: }
1342: assertFalse(iterator.hasNext());
1343: }
1344:
1345: /**
1346: * Asserts the flow when an exception is thrown during a tear down call of a test.
1347: *
1348: * @param type JUnit3, JUnit4 or TestNG
1349: * @param tracingTestListener the recorded invocations
1350: */
1351: private void assertInvocationOrder_testTearDown(String type,
1352: TracingTestListener tracingTestListener) {
1353: if ("JUnit3".equals(type) || "JUnit4".equals(type)) {
1354: assertInvocationOrder(type, tracingTestListener);
1355: return;
1356: }
1357:
1358: Iterator<?> iterator = tracingTestListener.getCallList()
1359: .iterator();
1360: if ("TestNG".equals(type)) {
1361: assertEquals("[Unitils] beforeAll", iterator.next());
1362: assertEquals("[Unitils] beforeTestClass - TestClass1",
1363: iterator.next());
1364: assertEquals("[Test] testBeforeClass - TestClass1",
1365: iterator.next());
1366: assertEquals("[Unitils] beforeTestSetUp - TestClass1",
1367: iterator.next());
1368: assertEquals("[Test] testSetUp - TestClass1",
1369: iterator.next());
1370: assertEquals("[Unitils] beforeTestMethod - TestClass1",
1371: iterator.next());
1372: assertEquals("[Test] testMethod - TestClass1",
1373: iterator.next());
1374: assertEquals("[Unitils] afterTestMethod - TestClass1",
1375: iterator.next());
1376: assertEquals("[Test] testTearDown - TestClass1",
1377: iterator.next());
1378: assertEquals("[Unitils] afterTestTearDown - TestClass1",
1379: iterator.next());
1380: assertEquals("[Unitils] beforeTestSetUp - TestClass1",
1381: iterator.next()); // difference with JUnit
1382: // second testSetUp is skipped // difference with JUnit
1383: // second beforeTestMethod is skipped // difference with JUnit
1384: // second testMethod 2 is skipped // difference with JUnit
1385: // second afterTestMethod is skipped // difference with JUnit
1386: assertEquals("[Unitils] afterTestTearDown - TestClass1",
1387: iterator.next()); // difference with JUnit
1388: // testAfterClass is skipped // difference with JUnit
1389: assertEquals("[Unitils] afterTestClass - TestClass1",
1390: iterator.next());
1391: assertEquals("[Unitils] afterAll", iterator.next());
1392: }
1393: assertFalse(iterator.hasNext());
1394: }
1395:
1396: /**
1397: * Asserts the flow when an exception is thrown during a {@link TestListener#afterTestTearDown} call of a module.
1398: *
1399: * @param type JUnit3, JUnit4 or TestNG
1400: * @param tracingTestListener the recorded invocations
1401: */
1402: private void assertInvocationOrder_afterTestTearDown(String type,
1403: TracingTestListener tracingTestListener) {
1404: if ("JUnit3".equals(type) || "JUnit4".equals(type)) {
1405: assertInvocationOrder(type, tracingTestListener);
1406: return;
1407: }
1408:
1409: Iterator<?> iterator = tracingTestListener.getCallList()
1410: .iterator();
1411: if ("TestNG".equals(type)) {
1412: assertEquals("[Unitils] beforeAll", iterator.next());
1413: assertEquals("[Unitils] beforeTestClass - TestClass1",
1414: iterator.next());
1415: assertEquals("[Test] testBeforeClass - TestClass1",
1416: iterator.next());
1417: assertEquals("[Unitils] beforeTestSetUp - TestClass1",
1418: iterator.next());
1419: assertEquals("[Test] testSetUp - TestClass1",
1420: iterator.next());
1421: assertEquals("[Unitils] beforeTestMethod - TestClass1",
1422: iterator.next());
1423: assertEquals("[Test] testMethod - TestClass1",
1424: iterator.next());
1425: assertEquals("[Unitils] afterTestMethod - TestClass1",
1426: iterator.next());
1427: assertEquals("[Test] testTearDown - TestClass1",
1428: iterator.next());
1429: assertEquals("[Unitils] afterTestTearDown - TestClass1",
1430: iterator.next());
1431: // second beforeTestSetUp is skipped // difference with JUnit
1432: // second testSetUp is skipped // difference with JUnit
1433: // second beforeTestMethod is skipped // difference with JUnit
1434: // second testMethod 2 is skipped // difference with JUnit
1435: // second afterTestMethod is skipped // difference with JUnit
1436: // testAfterClass is skipped // difference with JUnit
1437: assertEquals("[Unitils] afterTestClass - TestClass1",
1438: iterator.next());
1439: assertEquals("[Unitils] afterAll", iterator.next());
1440: }
1441: assertFalse(iterator.hasNext());
1442: }
1443:
1444: /**
1445: * Runs the {@link UnitilsJUnit4Test_TestClass1} JUnit 4 test.
1446: *
1447: * @return the test result
1448: */
1449: private Result performJUnit4Test() throws Exception {
1450: Result result = new Result();
1451: RunNotifier runNotifier = new RunNotifier();
1452: runNotifier.addListener(result.createListener());
1453: TestUnitilsJUnit4TestClassRunner testRunner1 = new TestUnitilsJUnit4TestClassRunner(
1454: UnitilsJUnit4Test_TestClass1.class);
1455: testRunner1.run(runNotifier);
1456: return result;
1457: }
1458:
1459: /**
1460: * Runs the {@link UnitilsTestNGTest_TestClass1} TestNG test.
1461: *
1462: * @return the test result
1463: */
1464: private TestListenerAdapter performTestNGTest() {
1465: TestListenerAdapter testListenerAdapter = new TestListenerAdapter();
1466: TestNG testng = new TestNG();
1467: testng
1468: .setTestClasses(new Class[] { UnitilsTestNGTest_TestClass1.class });
1469: testng.addListener(testListenerAdapter);
1470: testng.run();
1471: return testListenerAdapter;
1472: }
1473:
1474: /**
1475: * Overridden test class runner to be able to use the {@link TracingTestListener} as test listener.
1476: */
1477: private class TestUnitilsJUnit4TestClassRunner extends
1478: UnitilsJUnit4TestClassRunner {
1479:
1480: public TestUnitilsJUnit4TestClassRunner(Class<?> testClass)
1481: throws InitializationError {
1482: super (testClass);
1483: }
1484:
1485: @Override
1486: protected Unitils getUnitils() {
1487:
1488: return new Unitils() {
1489:
1490: @Override
1491: public TestListener createTestListener() {
1492: return tracingTestListener;
1493: }
1494: };
1495: }
1496: }
1497:
1498: }
|