protected void executeAllLifeCycles(TestRunnerAdaptor adaptor) throws Exception { doAnswer(new ExecuteLifecycle()).when(adaptor).fireCustomLifecycle(isA(BeforeRules.class)); doAnswer(new ExecuteLifecycle()).when(adaptor).fireCustomLifecycle(isA(AfterRules.class)); doAnswer(new ExecuteLifecycle()).when(adaptor).beforeClass(any(Class.class), any(LifecycleMethodExecutor.class)); doAnswer(new ExecuteLifecycle()).when(adaptor).afterClass(any(Class.class), any(LifecycleMethodExecutor.class)); doAnswer(new ExecuteLifecycle()).when(adaptor) .before(any(Object.class), any(Method.class), any(LifecycleMethodExecutor.class)); doAnswer(new ExecuteLifecycle()).when(adaptor) .after(any(Object.class), any(Method.class), any(LifecycleMethodExecutor.class)); doAnswer(new TestExecuteLifecycle(TestResult.passed())).when(adaptor).test(any(TestMethodExecutor.class)); }
protected void executeAllLifeCycles(TestRunnerAdaptor adaptor) throws Exception { doAnswer(new ExecuteLifecycle(Cycle.BEFORE_SUITE)).when(adaptor).beforeSuite(); doAnswer(new ExecuteLifecycle(Cycle.AFTER_SUITE)).when(adaptor).afterSuite(); doAnswer(new ExecuteLifecycle(Cycle.BEFORE_CLASS)).when(adaptor) .beforeClass(any(Class.class), any(LifecycleMethodExecutor.class)); doAnswer(new ExecuteLifecycle(Cycle.AFTER_CLASS)).when(adaptor) .afterClass(any(Class.class), any(LifecycleMethodExecutor.class)); doAnswer(new ExecuteLifecycle(Cycle.BEFORE)).when(adaptor) .before(any(Object.class), any(Method.class), any(LifecycleMethodExecutor.class)); doAnswer(new ExecuteLifecycle(Cycle.AFTER)).when(adaptor) .after(any(Object.class), any(Method.class), any(LifecycleMethodExecutor.class)); doAnswer(new TestExecuteLifecycle(new TestResult(Status.PASSED))).when(adaptor) .test(any(TestMethodExecutor.class)); }
@Test public void shouldOnlyCallBeforeAfterSuiteOnce() throws Exception { TestRunnerAdaptor adaptor = mock(TestRunnerAdaptor.class); executeAllLifeCycles(adaptor); Result result = run(adaptor, ClassWithArquillianClassAndMethodRule.class, ClassWithArquillianClassAndMethodRule.class, ClassWithArquillianClassAndMethodRule.class, ClassWithArquillianClassAndMethodRule.class); Assert.assertTrue(result.wasSuccessful()); verify(adaptor, times(4)).beforeSuite(); verify(adaptor, times(4)).afterSuite(); }
@Test public void shouldCallAllMethods() throws Exception { TestRunnerAdaptor adaptor = mock(TestRunnerAdaptor.class); executeAllLifeCycles(adaptor); Result result = run(adaptor, ClassWithArquillianRunner.class); Assert.assertTrue(result.wasSuccessful()); assertCycle(1, Cycle.basics()); verify(adaptor, times(1)).fireCustomLifecycle(isA(BeforeRules.class)); verify(adaptor, times(1)).fireCustomLifecycle(isA(AfterRules.class)); verify(adaptor, times(1)).beforeSuite(); verify(adaptor, times(1)).afterSuite(); }
@Test public void shouldCallBeforeClassAndAfterClassWithoutLifecycleHandlers() throws Exception { TestRunnerAdaptor adaptor = mock(TestRunnerAdaptor.class); when(adaptor.test(isA(TestMethodExecutor.class))).thenReturn(TestResult.passed()); Result result = run(adaptor, ClassWithArquillianClassAndMethodRule.class); Assert.assertTrue(result.wasSuccessful()); assertCycle(1, Cycle.BEFORE_CLASS, Cycle.AFTER_CLASS); assertCycle(0, Cycle.BEFORE, Cycle.AFTER, Cycle.AFTER_RULE, Cycle.BEFORE_RULE); verify(adaptor, times(1)).beforeSuite(); verify(adaptor, times(1)).afterSuite(); }
Method testMethod = method.getMethod(); adaptor.fireCustomLifecycle( new RulesEnrichment(target, testClass, testMethod, LifecycleMethodExecutor.NO_OP)); adaptor.fireCustomLifecycle( new BeforeRules(target, testClass, base, testMethod, LifecycleMethodExecutor.NO_OP)); adaptor.before(target, testMethod, LifecycleMethodExecutor.NO_OP); adaptor.after(target, testMethod, LifecycleMethodExecutor.NO_OP); try { adaptor.fireCustomLifecycle( new AfterRules(target, testMethod, LifecycleMethodExecutor.NO_OP)); } catch (Throwable e) {
@AfterSuite(groups = "arquillian", inheritGroups = true, alwaysRun = true) public void arquillianAfterSuite() throws Exception { if (deployableTest.get() == null) { return; // beforeSuite failed } if (cycleStack.get().empty()) { return; } if (cycleStack.get().peek() != Cycle.BEFORE_SUITE) { return; // Arquillian lifecycle called out of order, expected " + Cycle.BEFORE_SUITE } else { cycleStack.get().pop(); } deployableTest.get().afterSuite(); deployableTest.get().shutdown(); deployableTest.set(null); deployableTest.remove(); cycleStack.set(null); cycleStack.remove(); }
@BeforeClass(groups = "arquillian", inheritGroups = true) public void arquillianBeforeClass() throws Exception { verifyTestRunnerAdaptorHasBeenSet(); cycleStack.get().push(Cycle.BEFORE_CLASS); deployableTest.get().beforeClass(getClass(), LifecycleMethodExecutor.NO_OP); }
@Override public void interceptFeatureMethod(final IMethodInvocation invocation) throws Throwable { TestResult result = getTestRunner().test(new TestMethodExecutor() { @Override public Method getMethod() { return invocation.getFeature().getFeatureMethod().getReflection(); } @Override public Object getInstance() { return invocation.getTarget(); } @Override public void invoke(Object... parameters) throws Throwable { invocation.proceed(); } }); if (result.getThrowable() != null) { throw result.getThrowable(); } }
@BeforeMethod(groups = "arquillian", inheritGroups = true) public void arquillianBeforeTest(Method testMethod) throws Exception { verifyTestRunnerAdaptorHasBeenSet(); cycleStack.get().push(Cycle.BEFORE); deployableTest.get().before(this, testMethod, LifecycleMethodExecutor.NO_OP); }
@AfterClass(groups = "arquillian", inheritGroups = true, alwaysRun = true) public void arquillianAfterClass() throws Exception { if (cycleStack.get().empty()) { return; } if (cycleStack.get().peek() != Cycle.BEFORE_CLASS) { return; // Arquillian lifecycle called out of order, expected " + Cycle.BEFORE_CLASS } else { cycleStack.get().pop(); } verifyTestRunnerAdaptorHasBeenSet(); deployableTest.get().afterClass(getClass(), LifecycleMethodExecutor.NO_OP); }
@AfterMethod(groups = "arquillian", inheritGroups = true, alwaysRun = true) public void arquillianAfterTest(Method testMethod) throws Exception { if (cycleStack.get().empty()) { return; } if (cycleStack.get().peek() != Cycle.BEFORE) { return; // Arquillian lifecycle called out of order, expected " + Cycle.BEFORE_CLASS } else { cycleStack.get().pop(); } verifyTestRunnerAdaptorHasBeenSet(); deployableTest.get().after(this, testMethod, LifecycleMethodExecutor.NO_OP); }
@BeforeSuite(groups = "arquillian", inheritGroups = true) public void arquillianBeforeSuite() throws Exception { if (deployableTest.get() == null) { TestRunnerAdaptor adaptor = TestRunnerAdaptorBuilder.build(); adaptor.beforeSuite(); deployableTest.set(adaptor); // don't set TestRunnerAdaptor if beforeSuite fails cycleStack.get().push(Cycle.BEFORE_SUITE); } }
adaptor.fireCustomLifecycle( new BeforeRules(test, getTestClass(), stmtWithRules, method.getMethod(), new LifecycleMethodExecutor() { } finally { try { adaptor.fireCustomLifecycle( new AfterRules(test, method.getMethod(), LifecycleMethodExecutor.NO_OP)); } catch (Throwable t) {
@Test public void shouldThrowMultipleExceptionIfFailureInBeforeAndAfterRule() throws Exception { TestRunnerAdaptor adaptor = mock(TestRunnerAdaptor.class); doAnswer(new ThrowsException(new RuntimeException("AfterRuleException"))).when(adaptor) .fireCustomLifecycle(isA(AfterRules.class)); doAnswer(new ExecuteLifecycle()).when(adaptor).fireCustomLifecycle(isA(BeforeRules.class)); doAnswer(new ExecuteLifecycle()).when(adaptor).beforeClass(any(Class.class), any(LifecycleMethodExecutor.class)); doAnswer(new ExecuteLifecycle()).when(adaptor).afterClass(any(Class.class), any(LifecycleMethodExecutor.class)); doAnswer(new ExecuteLifecycle()).when(adaptor) .before(any(Object.class), any(Method.class), any(LifecycleMethodExecutor.class)); doAnswer(new ExecuteLifecycle()).when(adaptor) .after(any(Object.class), any(Method.class), any(LifecycleMethodExecutor.class)); doAnswer(new TestExecuteLifecycle(TestResult.passed())).when(adaptor).test(any(TestMethodExecutor.class)); Result result = run(adaptor, ClassWithArquillianClassAndMethodRuleWithExceptionInAfterAndAfterRule.class); Assert.assertFalse(result.wasSuccessful()); Assert.assertEquals(2, result.getFailureCount()); Assert.assertTrue(result.getFailures().get(0).getMessage().equals("AfterException")); Assert.assertTrue(result.getFailures().get(1).getMessage().equals("AfterRuleException")); assertCycle(1, Cycle.basics()); verify(adaptor, times(1)).fireCustomLifecycle(isA(BeforeRules.class)); verify(adaptor, times(1)).fireCustomLifecycle(isA(AfterRules.class)); verify(adaptor, times(1)).beforeSuite(); verify(adaptor, times(1)).afterSuite(); }
@Test public void shouldOnlyCallBeforeAfterSuiteOnce() throws Exception { TestRunnerAdaptor adaptor = mock(TestRunnerAdaptor.class); executeAllLifeCycles(adaptor); Result result = run(adaptor, ClassWithArquillianRunner.class, ClassWithArquillianRunner.class, ClassWithArquillianRunner.class, ClassWithArquillianRunner.class); Assert.assertTrue(result.wasSuccessful()); verify(adaptor, times(1)).beforeSuite(); verify(adaptor, times(1)).afterSuite(); }
@Test public void shouldCallAllMethods() throws Exception { TestRunnerAdaptor adaptor = mock(TestRunnerAdaptor.class); executeAllLifeCycles(adaptor); Result result = run(adaptor, ClassWithArquillianClassAndMethodRule.class); Assert.assertTrue(result.wasSuccessful()); assertCycle(1, Cycle.basics()); verify(adaptor, times(1)).fireCustomLifecycle(isA(BeforeRules.class)); verify(adaptor, times(1)).fireCustomLifecycle(isA(AfterRules.class)); verify(adaptor, times(1)).beforeSuite(); verify(adaptor, times(1)).afterSuite(); }
@Test public void shouldCallBeforeClassAndAfterClassWithoutLifecycleHandlers() throws Exception { TestRunnerAdaptor adaptor = mock(TestRunnerAdaptor.class); when(adaptor.test(isA(TestMethodExecutor.class))).thenReturn(TestResult.passed()); Result result = run(adaptor, ClassWithArquillianClassAndMethodRule.class); Assert.assertTrue(result.wasSuccessful()); assertCycle(1, Cycle.BEFORE_CLASS, Cycle.AFTER_CLASS); assertCycle(0, Cycle.BEFORE, Cycle.AFTER, Cycle.AFTER_RULE, Cycle.BEFORE_RULE); verify(adaptor, times(1)).beforeSuite(); verify(adaptor, times(1)).afterSuite(); }
Method testMethod = method.getMethod(); adaptor.fireCustomLifecycle( new RulesEnrichment(target, testClass, testMethod, LifecycleMethodExecutor.NO_OP)); adaptor.fireCustomLifecycle( new BeforeRules(target, testClass, base, testMethod, LifecycleMethodExecutor.NO_OP)); adaptor.before(target, testMethod, LifecycleMethodExecutor.NO_OP); adaptor.after(target, testMethod, LifecycleMethodExecutor.NO_OP); try { adaptor.fireCustomLifecycle( new AfterRules(target, testMethod, LifecycleMethodExecutor.NO_OP)); } catch (Throwable e) {
void shutdown(TestRunnerAdaptor adaptor) { State.runnerFinished(); try { if (State.isLastRunner()) { try { if (adaptor != null) { adaptor.afterSuite(); adaptor.shutdown(); } } finally { State.clean(); } } setAdaptor(null); } catch (Exception e) { throw new RuntimeException("Could not run @AfterSuite", e); } }