@Test public void shouldReturnAssertionErrorToClientWhenAfterThrowsException() throws Exception { Exception unexpectedException = new Exception("Not expected"); TestScenarios.exceptionThrownInAfter = unexpectedException; JUnitTestRunner runner = new JUnitTestRunner(); TestResult result = runner.execute(TestScenarios.class, "shouldFailOnException"); Assert.assertEquals(TestResult.Status.FAILED, result.getStatus()); Assert.assertNotNull(result.getThrowable()); Assert.assertEquals(AssertionError.class, result.getThrowable().getClass()); }
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)); }
/** * Invoke method after enrichment. Inexplicably, the {@code @Before} * and {@code @After} methods are not called when running this * executor. Calling them manually for now. * * @param testMethodExecutor Method executor. * @return Test result. */ public TestResult invoke(TestMethodExecutor testMethodExecutor) { RequestContextController controller = enricher.getRequestContextController(); try { controller.activate(); Object object = testMethodExecutor.getInstance(); Method method = testMethodExecutor.getMethod(); LOGGER.info("Invoking '" + method + "' on " + object); enricher.enrich(object); invokeAnnotated(object, Before.class); testMethodExecutor.invoke(enricher.resolve(method)); invokeAnnotated(object, After.class); } catch (Throwable t) { return TestResult.failed(t); } finally { controller.deactivate(); } return TestResult.passed(); }
@Test public void shouldPassResultIfNoExceptionCaughtAfterJunit() throws Exception { TestResult result = TestResult.failed(new NullPointerException()); new UpdateTestResultBeforeAfter().update(event, result); Assert.assertEquals(Status.PASSED, result.getStatus()); Assert.assertNull(result.getThrowable()); }
private TestResult runTestMethodInternal(String className, String methodName, Map<String, String> protocolProps) { currentCall.set(className + methodName); TestResult result = new TestResult(); try { TestRunner runner = mockTestRunner; if (runner == null) { runner = TestRunners.getTestRunner(getClass().getClassLoader()); } log.fine("Load test class: " + className); Class<?> testClass = testClassLoader.loadTestClass(className); log.fine("Test class loaded from: " + testClass.getClassLoader()); log.fine("Execute: " + className + "." + methodName); result = doRunTestMethod(runner, testClass, methodName, protocolProps); } catch (Throwable th) { result.setStatus(Status.FAILED); result.setEnd(System.currentTimeMillis()); result.setThrowable(th); } finally { log.fine("Result: " + result); if (result.getStatus() == Status.FAILED) { log.log(Level.SEVERE, "Failed: " + className + "." + methodName, result.getThrowable()); } } return result; }
public void execute(@Observes Test event) throws Exception { TestResult result = new TestResult(); try { event.getTestMethodExecutor().invoke( enrichArguments( event.getTestMethod(), serviceLoader.get().all(TestEnricher.class))); result.setStatus(Status.PASSED); } catch (Throwable e) { result.setStatus(Status.FAILED); result.setThrowable(e); } finally { result.setEnd(System.currentTimeMillis()); } testResult.set(result); }
@Test public void should_mark_test_result_as_failed_if_there_is_at_least_one_failure() throws Exception { // given final TestResult p1 = TestResult.passed("First test passed"); final TestResult s1 = TestResult.skipped("Not implemented yet"); final TestResult f1 = failed(new RuntimeException("Exception")); final List<TestResult> testResults = asList(p1, s1, f1); // when final TestResult result = TestResult.flatten(testResults); // then assertThat(result.getStatus()).isEqualTo(TestResult.Status.FAILED); }
void invoke(final TestRunnerAdaptor adaptor, final FrameworkMethod method, final Object test) throws Throwable { TestResult result = adaptor.test(new TestMethodExecutor() { @Override public void invoke(Object... parameters) throws Throwable { invokeMethod(parameters); } public Method getMethod() { return method.getMethod(); } public Object getInstance() { return test; } }); Throwable throwable = result.getThrowable(); if (throwable != null) { if (result.getStatus() == TestResult.Status.SKIPPED) { if (throwable instanceof SkippedTestExecutionException) { result.setThrowable(new AssumptionViolatedException(throwable.getMessage())); } } throw result.getThrowable(); } }
public TestResult execute(Class<?> testClass, String methodName) { TestResult testResult; ExpectedExceptionHolder exceptionHolder = new ExpectedExceptionHolder(); try { JUnitCore runner = new JUnitCore(); runner.addListener(exceptionHolder); for (RunListener listener : getRunListeners()) runner.addListener(listener); Result result = runner.run(Request.method(testClass, methodName)); if (result.getFailureCount() > 0) { testResult = TestResult.failed(exceptionHolder.getException()); } else if (result.getIgnoreCount() > 0) { testResult = TestResult.skipped(); // Will this ever happen incontainer? } else { testResult = TestResult.passed(); } if (testResult.getThrowable() == null) { testResult.setThrowable(exceptionHolder.getException()); } } catch (Throwable th) { testResult = TestResult.failed(th); } if (testResult.getThrowable() instanceof AssumptionViolatedException) { testResult = TestResult.skipped(testResult.getThrowable()); } testResult.setEnd(System.currentTimeMillis()); return testResult; }
@Test public void should_combine_skipped_and_passed_tests_descriptions() throws Exception { // given final TestResult passed = TestResult.passed("First test passed"); final TestResult skipped = TestResult.skipped("Not implemented yet"); final List<TestResult> testResults = asList(skipped, passed); // when final TestResult result = TestResult.flatten(testResults); // then assertThat(result.getDescription()).isEqualTo( String.format("SKIPPED: 'Not implemented yet'%nPASSED: 'First test passed'%n")); }
public TestResult getTestResult() { if (context.getFailedConfigurations().size() > 0) { return TestResult.failed( context.getFailedConfigurations().getAllResults().iterator().next().getThrowable()); } else if (context.getFailedTests().size() > 0) { return TestResult.failed( context.getFailedTests().getAllResults().iterator().next().getThrowable()); } else if (context.getSkippedTests().size() > 0) { return TestResult.skipped().setThrowable(context.getSkippedTests().getAllResults().iterator().next().getThrowable()); } if (context.getPassedTests().size() > 0) { return TestResult.passed().setThrowable( context.getPassedTests().getAllResults().iterator().next().getThrowable()); } return TestResult.failed( new RuntimeException("Unknown test result: " + context).fillInStackTrace()); } }
Throwable throwable = result.getThrowable(); if (throwable != null) { if (result.getStatus() == Status.SKIPPED) { if (throwable instanceof SkippedTestExecutionException) { result.setThrowable(new SkipException(throwable.getMessage())); testResult.setThrowable(result.getThrowable()); testResult.setEndMillis((result.getStart() - result.getEnd()) + testResult.getStartMillis()); } catch (Exception e) { testResult.setThrowable(e);
@Test public void shouldReturnTestResult() throws Exception { MockTestRunner.add(TestResult.passed()); ServletMethodExecutor executor = createExecutor(); TestResult result = executor.invoke(new MockTestExecutor()); Assert.assertEquals( "Should have returned a passed test", MockTestRunner.wantedResults.getStatus(), result.getStatus()); Assert.assertNull( "No Exception should have been thrown", result.getThrowable()); }
@Test public void should_flatten_successful_test_results() throws Exception { // given final TestResult p1 = TestResult.passed(); final TestResult p2 = TestResult.passed(); final List<TestResult> testResults = asList(p1, p2); // when TestResult result = TestResult.flatten(testResults); // then assertThat(result.getStatus()).isEqualTo(TestResult.Status.PASSED); }
@Test public void should_combine_exceptions_for_all_failures() throws Exception { // given final RuntimeException ex1 = new RuntimeException("Exception 1"); final TestResult e1 = failed(ex1); final RuntimeException ex2 = new RuntimeException("Exception 2"); final TestResult e2 = failed(ex2); final List<TestResult> testResults = asList(e1, e2); // when final TestResult result = TestResult.flatten(testResults); // then assertThat(result.getThrowable()).isInstanceOf(CombinedException.class); assertThat(((CombinedException) result.getThrowable()).getCauses()).containsExactly(ex1, ex2); }
@Override public TestResult invoke(TestMethodExecutor arg0) { return new TestResult(Status.SKIPPED); } };
@SuppressWarnings("deprecation") public void enforce(@Observes(precedence = -10000) TestResult result) { result.setStatus(Status.PASSED); result.setThrowable(null); } }
@Test public void should_mark_test_result_as_failed_if_there_are_only_failures() throws Exception { //given final TestResult e1 = failed(new RuntimeException("Exception 1")); final TestResult e2 = failed(new RuntimeException("Exception 2")); final List<TestResult> testResults = asList(e1, e2); // when final TestResult result = TestResult.flatten(testResults); // then assertThat(result.getStatus()).isEqualTo(TestResult.Status.FAILED); }
/** * Convert a JUnit Result object to Arquillian TestResult * * @param result JUnit Test Run Result * @return The TestResult representation of the JUnit Result */ private TestResult convertToTestResult(Result result) { TestResult newResult = TestResult.passed(); Throwable throwable = null; if (result.getFailureCount() > 0) { throwable = result.getFailures().get(0).getException(); newResult = TestResult.failed(throwable); } if (result.getIgnoreCount() > 0) { newResult = TestResult.skipped(throwable); } return newResult; } }
public void execute(@Observes Test event) throws Exception { TestResult result = TestResult.passed(); try { event.getTestMethodExecutor().invoke( enrichArguments( event.getTestMethod(), serviceLoader.get().all(TestEnricher.class))); } catch (Throwable e) { result = TestResult.failed(e); } finally { result.setEnd(System.currentTimeMillis()); } testResult.set(result); }