Search in sources :

Example 81 with Statement

use of org.junit.runners.model.Statement in project junit4 by junit-team.

the class ExternalResourceRuleTest method shouldWrapAssumptionFailuresWhenClosingResourceFails.

@Test
public void shouldWrapAssumptionFailuresWhenClosingResourceFails() throws Throwable {
    // given
    final AtomicReference<Throwable> externalResourceException = new AtomicReference<Throwable>();
    ExternalResource resourceRule = new ExternalResource() {

        @Override
        protected void after() {
            RuntimeException runtimeException = new RuntimeException("simulating resource tear down failure");
            externalResourceException.set(runtimeException);
            throw runtimeException;
        }
    };
    final AtomicReference<Throwable> assumptionViolatedException = new AtomicReference<Throwable>();
    Statement skippedTest = new Statement() {

        @Override
        public void evaluate() throws Throwable {
            AssumptionViolatedException assumptionFailure = new AssumptionViolatedException("skip it");
            assumptionViolatedException.set(assumptionFailure);
            throw assumptionFailure;
        }
    };
    Description dummyDescription = Description.createTestDescription("dummy test class name", "dummy test name");
    try {
        resourceRule.apply(skippedTest, dummyDescription).evaluate();
        fail("ExternalResource should throw");
    } catch (MultipleFailureException e) {
        assertThat(e.getFailures(), hasItems(instanceOf(TestCouldNotBeSkippedException.class), sameInstance(externalResourceException.get())));
        assertThat(e.getFailures(), hasItems(hasCause(sameInstance(assumptionViolatedException.get())), sameInstance(externalResourceException.get())));
    }
}
Also used : Description(org.junit.runner.Description) AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) MultipleFailureException(org.junit.runners.model.MultipleFailureException) Statement(org.junit.runners.model.Statement) AtomicReference(java.util.concurrent.atomic.AtomicReference) TestCouldNotBeSkippedException(org.junit.TestCouldNotBeSkippedException) Test(org.junit.Test)

Example 82 with Statement

use of org.junit.runners.model.Statement in project qualitymatters by artem-zinnatullin.

the class MockWebServerRule method apply.

@Override
@NonNull
public Statement apply(@NonNull Statement base, @NonNull Description description) {
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            final NeedsMockWebServer needsMockWebServer = description.getAnnotation(NeedsMockWebServer.class);
            if (needsMockWebServer != null) {
                final MockWebServer mockWebServer = new MockWebServer();
                mockWebServer.start();
                TestUtils.app().applicationComponent().changeableBaseUrl().setBaseUrl(mockWebServer.url("").toString());
                if (!needsMockWebServer.setupMethod().isEmpty()) {
                    final Method setupMethod = testClassInstance.getClass().getDeclaredMethod(needsMockWebServer.setupMethod(), MockWebServer.class);
                    setupMethod.invoke(testClassInstance, mockWebServer);
                }
                // Try to evaluate the test and anyway shutdown the MockWebServer.
                try {
                    base.evaluate();
                } finally {
                    mockWebServer.shutdown();
                }
            } else {
                // No need to setup a MockWebServer, just evaluate the test.
                base.evaluate();
            }
        }
    };
}
Also used : Statement(org.junit.runners.model.Statement) MockWebServer(okhttp3.mockwebserver.MockWebServer) Method(java.lang.reflect.Method) NonNull(android.support.annotation.NonNull)

Example 83 with Statement

use of org.junit.runners.model.Statement in project restfuse by eclipsesource.

the class Destination_Test method testApplyReturnsBaseWhenNoAnnotationsPresent.

@Test
public void testApplyReturnsBaseWhenNoAnnotationsPresent() {
    Statement base = mock(Statement.class);
    Description description = mock(Description.class);
    Statement statement = destination.apply(base, description);
    assertEquals(base, statement);
}
Also used : Description(org.junit.runner.Description) Statement(org.junit.runners.model.Statement) HttpTestStatement(com.eclipsesource.restfuse.internal.HttpTestStatement) HttpTest(com.eclipsesource.restfuse.annotation.HttpTest) Test(org.junit.Test)

Example 84 with Statement

use of org.junit.runners.model.Statement in project restfuse by eclipsesource.

the class HttpTestStatement_Test method testRemovesProxyProperties.

@Test
public void testRemovesProxyProperties() throws Throwable {
    Statement base = mock(Statement.class);
    Description description = mock(Description.class);
    HttpTest annotation = createAnnotation();
    when(description.getAnnotation(HttpTest.class)).thenReturn(annotation);
    Object target = new Object();
    HttpTestStatement statement = new HttpTestStatement(base, description, target, "http://localhost", "http://proxy.com", 8080, null);
    statement.evaluate();
    assertNull(System.getProperty(HttpTestStatement.HTTP_PROXY_HOST));
    assertNull(System.getProperty(HttpTestStatement.HTTP_PROXY_PORT));
}
Also used : HttpTest(com.eclipsesource.restfuse.annotation.HttpTest) Description(org.junit.runner.Description) Statement(org.junit.runners.model.Statement) CallbackStatement(com.eclipsesource.restfuse.internal.callback.CallbackStatement) Test(org.junit.Test) HttpTest(com.eclipsesource.restfuse.annotation.HttpTest)

Example 85 with Statement

use of org.junit.runners.model.Statement in project restfuse by eclipsesource.

the class Destination method apply.

@Override
public /**
   * <p><b>Not meant for public use. This method will be invoked by the JUnit framework.</b></p>
   */
Statement apply(Statement base, Description description) {
    Statement result;
    if (hasAnnotation(description)) {
        requestStatement = new HttpTestStatement(base, description, testObject, baseUrl, proxyHost, proxyPort, context);
        result = requestStatement;
    } else {
        result = base;
    }
    return result;
}
Also used : HttpTestStatement(com.eclipsesource.restfuse.internal.HttpTestStatement) Statement(org.junit.runners.model.Statement) HttpTestStatement(com.eclipsesource.restfuse.internal.HttpTestStatement)

Aggregations

Statement (org.junit.runners.model.Statement)138 Test (org.junit.Test)22 FrameworkMethod (org.junit.runners.model.FrameworkMethod)15 Method (java.lang.reflect.Method)11 AssumptionViolatedException (org.junit.internal.AssumptionViolatedException)11 Fail (org.junit.internal.runners.statements.Fail)9 Description (org.junit.runner.Description)8 TestRule (org.junit.rules.TestRule)7 MultipleFailureException (org.junit.runners.model.MultipleFailureException)6 ExecutorService (java.util.concurrent.ExecutorService)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 ReflectiveCallable (org.junit.internal.runners.model.ReflectiveCallable)5 MethodRule (org.junit.rules.MethodRule)5 ArrayList (java.util.ArrayList)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 EmptyStatement (com.hazelcast.util.EmptyStatement)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 TestExecutorService (org.apache.beam.fn.harness.test.TestExecutors.TestExecutorService)3 CompositeConfiguration (org.apache.logging.log4j.core.config.composite.CompositeConfiguration)3 LoggerContextRule (org.apache.logging.log4j.junit.LoggerContextRule)3