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())));
}
}
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();
}
}
};
}
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);
}
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));
}
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;
}
Aggregations