下面列出了怎么用org.junit.runners.model.MultipleFailureException的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public Statement apply(final Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
try {
base.evaluate();
} catch (Throwable t) {
firstException = t;
try {
teardown();
} catch (Throwable t2) {
throw new MultipleFailureException(Arrays.asList(t, t2));
}
throw t;
}
teardown();
if (firstException != null) {
throw firstException;
}
}
};
}
/**
* Implementation based on {@link TestWatcher}.
*/
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
List<Throwable> errors = new ArrayList<Throwable>();
startingQuietly(description, errors);
try {
base.evaluate();
succeededQuietly(description, errors);
} catch (AssumptionViolatedException e) {
errors.add(e);
skippedQuietly(e, description, errors);
} catch (Throwable t) {
errors.add(t);
failedQuietly(t, description, errors);
} finally {
finishedQuietly(description, errors);
}
MultipleFailureException.assertEmpty(errors);
}
};
}
/**
* Implementation based on {@link TestWatcher}.
*/
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
List<Throwable> errors = new ArrayList<Throwable>();
startingQuietly(description, errors);
try {
base.evaluate();
succeededQuietly(description, errors);
} catch (AssumptionViolatedException e) {
errors.add(e);
skippedQuietly(e, description, errors);
} catch (Throwable t) {
errors.add(t);
failedQuietly(t, description, errors);
} finally {
finishedQuietly(description, errors);
}
MultipleFailureException.assertEmpty(errors);
}
};
}
/**
* Implementation based on {@link TestWatcher}.
*/
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
List<Throwable> errors = new ArrayList<Throwable>();
startingQuietly(description, errors);
try {
base.evaluate();
succeededQuietly(description, errors);
} catch (AssumptionViolatedException e) {
errors.add(e);
skippedQuietly(e, description, errors);
} catch (Throwable t) {
errors.add(t);
failedQuietly(t, description, errors);
} finally {
finishedQuietly(description, errors);
}
MultipleFailureException.assertEmpty(errors);
}
};
}
/**
* Implementation based on {@link TestWatcher}.
*/
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
List<Throwable> errors = new ArrayList<Throwable>();
startingQuietly(description, errors);
try {
base.evaluate();
succeededQuietly(description, errors);
} catch (AssumptionViolatedException e) {
errors.add(e);
skippedQuietly(e, description, errors);
} catch (Throwable t) {
errors.add(t);
failedQuietly(t, description, errors);
} finally {
finishedQuietly(description, errors);
}
MultipleFailureException.assertEmpty(errors);
}
};
}
private Statement statement(final Statement base, final Description description) {
return new Statement() {
public void evaluate() throws Throwable {
before(description);
try {
List<Throwable> errors = new ArrayList<>();
try {
base.evaluate();
succeededQuietly(description, errors);
} catch (Throwable e) {
errors.add(e);
failedQuietly(e, description, errors);
}
MultipleFailureException.assertEmpty(errors);
} finally {
after();
}
}
};
}
@Override
public Statement apply(Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
final List<Throwable> errors = Lists.newArrayList();
starting();
try {
base.evaluate();
} catch (Throwable e) {
errors.add(e);
}
MultipleFailureException.assertEmpty(errors);
}
};
}
/**
* Implementation based on {@link TestWatcher}.
*/
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
List<Throwable> errors = new ArrayList<>();
startingQuietly(description, errors);
try {
base.evaluate();
succeededQuietly(description, errors);
} catch (AssumptionViolatedException e) {
errors.add(e);
skippedQuietly(e, description, errors);
} catch (Throwable t) {
errors.add(t);
failedQuietly(t, description, errors);
} finally {
finishedQuietly(description, errors);
}
MultipleFailureException.assertEmpty(errors);
}
};
}
/**
* Implementation based on {@link TestWatcher}.
*/
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
List<Throwable> errors = new ArrayList<>();
startingQuietly(description, errors);
try {
base.evaluate();
succeededQuietly(description, errors);
} catch (AssumptionViolatedException e) {
errors.add(e);
skippedQuietly(e, description, errors);
} catch (Throwable t) {
errors.add(t);
failedQuietly(t, description, errors);
}
MultipleFailureException.assertEmpty(errors);
}
};
}
/**
* Implementation based on {@link TestWatcher}.
*/
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
List<Throwable> errors = new ArrayList<Throwable>();
startingQuietly(description, errors);
try {
base.evaluate();
succeededQuietly(description, errors);
} catch (AssumptionViolatedException e) {
errors.add(e);
skippedQuietly(e, description, errors);
} catch (Throwable t) {
errors.add(t);
failedQuietly(t, description, errors);
} finally {
finishedQuietly(description, errors);
}
MultipleFailureException.assertEmpty(errors);
}
};
}
/**
* Implementation based on {@link TestWatcher}.
*/
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
List<Throwable> errors = new ArrayList<>();
startingQuietly(description, errors);
try {
base.evaluate();
succeededQuietly(description, errors);
} catch (AssumptionViolatedException e) {
errors.add(e);
skippedQuietly(e, description, errors);
} catch (Throwable t) {
errors.add(t);
failedQuietly(t, description, errors);
} finally {
finishedQuietly(description, errors);
}
MultipleFailureException.assertEmpty(errors);
}
};
}
/**
* Implementation based on {@link TestWatcher}.
*/
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
List<Throwable> errors = new ArrayList<>();
startingQuietly(description, errors);
try {
base.evaluate();
succeededQuietly(description, errors);
} catch (AssumptionViolatedException e) {
errors.add(e);
skippedQuietly(e, description, errors);
} catch (Throwable t) {
errors.add(t);
failedQuietly(t, description, errors);
} finally {
finishedQuietly(description, errors);
}
MultipleFailureException.assertEmpty(errors);
}
};
}
/**
* Implementation based on {@link TestWatcher}.
*/
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
List<Throwable> errors = new ArrayList<>();
startingQuietly(description, errors);
try {
base.evaluate();
succeededQuietly(description, errors);
} catch (AssumptionViolatedException e) {
errors.add(e);
skippedQuietly(e, description, errors);
} catch (Throwable t) {
errors.add(t);
failedQuietly(t, description, errors);
} finally {
finishedQuietly(description, errors);
}
MultipleFailureException.assertEmpty(errors);
}
};
}
/**
* Implementation based on {@link TestWatcher}.
*/
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
List<Throwable> errors = new ArrayList<>();
startingQuietly(description, errors);
try {
base.evaluate();
succeededQuietly(description, errors);
} catch (AssumptionViolatedException e) {
errors.add(e);
skippedQuietly(e, description, errors);
} catch (Throwable t) {
errors.add(t);
failedQuietly(t, description, errors);
} finally {
finishedQuietly(description, errors);
}
MultipleFailureException.assertEmpty(errors);
}
};
}
/**
* Implementation based on {@link TestWatcher}.
*/
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
List<Throwable> errors = new ArrayList<>();
startingQuietly(description, errors);
try {
base.evaluate();
succeededQuietly(description, errors);
} catch (AssumptionViolatedException e) {
errors.add(e);
skippedQuietly(e, description, errors);
} catch (Throwable t) {
errors.add(t);
failedQuietly(t, description, errors);
} finally {
finishedQuietly(description, errors);
}
MultipleFailureException.assertEmpty(errors);
}
};
}
/**
* Implementation based on {@link TestWatcher}.
*/
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
List<Throwable> errors = new ArrayList<>();
startingQuietly(description, errors);
try {
base.evaluate();
succeededQuietly(description, errors);
} catch (AssumptionViolatedException e) {
errors.add(e);
skippedQuietly(e, description, errors);
} catch (Throwable t) {
errors.add(t);
failedQuietly(t, description, errors);
} finally {
finishedQuietly(description, errors);
}
MultipleFailureException.assertEmpty(errors);
}
};
}
@Override
public Statement apply(Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
List<Throwable> errors = new ArrayList<Throwable>();
try {
starting(description);
base.evaluate();
succeeded(description);
} catch (Throwable e) {
errors.add(e);
failed(e, description);
} finally {
finished(description);
}
MultipleFailureException.assertEmpty(errors);
}
};
}
@Override
public void evaluate() {
int attempts = 0;
final List<Throwable> causes = new ArrayList<>();
while (++attempts <= maxTries) {
try {
base.evaluate();
return;
} catch (Throwable throwable) {
log.warn("Retrying @Flaky-annotated test: {}", description.getDisplayName());
causes.add(throwable);
}
}
throw new IllegalStateException(
"@Flaky-annotated test failed despite retries.",
new MultipleFailureException(causes));
}
@Override
public Statement apply(final Statement base, final Description description, final Object[] params) {
return new Statement() {
public void evaluate() throws Throwable {
ArrayList<Throwable> errors = new ArrayList<Throwable>();
ParameterizedTestWatcher.this.startingQuietly(description, errors, params);
try {
base.evaluate();
ParameterizedTestWatcher.this.succeededQuietly(description, errors, params);
} catch (AssumptionViolatedException var7) {
errors.add(var7);
ParameterizedTestWatcher.this.skippedQuietly(var7, description, errors, params);
} catch (Throwable var8) {
errors.add(var8);
ParameterizedTestWatcher.this.failedQuietly(var8, description, errors, params);
} finally {
ParameterizedTestWatcher.this.finishedQuietly(description, errors, params);
}
MultipleFailureException.assertEmpty(errors);
}
};
}
@Override
public Statement apply(Statement base, Description description) {
return cluster.apply(new Statement() {
@Override
public void evaluate() throws Throwable {
before();
try {
base.evaluate();
after();
} catch (Throwable t) {
try {
after();
throw t;
} catch (Exception e) {
throw new MultipleFailureException(asList(t, e));
}
}
}
}, description);
}
@Override
public Statement apply(final Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
try {
base.evaluate();
} catch (Throwable t) {
firstException = t;
try {
teardown();
} catch (Throwable t2) {
throw new MultipleFailureException(Arrays.asList(t, t2));
}
throw t;
}
teardown();
if (firstException != null) {
throw firstException;
}
}
};
}
@Before
public void setUp() throws Throwable {
assumeTrue("Test disabled", test.isEnabled());
realm.initialize(new NullConsole(), test);
realm.get().createGlobalProperties(new Print(), Print.class);
exceptionHandler.setExecutionContext(realm.get().defaultContext());
// Apply scripted conditions
scriptConditions();
// Filter disabled tests (may have changed after applying scripted conditions)
assumeTrue("Test disabled", test.isEnabled());
if (test.negative) {
expected.expect(
Matchers.either(StandardErrorHandler.defaultMatcher()).or(ScriptExceptionHandler.defaultMatcher())
.or(Matchers.instanceOf(MultipleFailureException.class)));
} else {
errorHandler.match(StandardErrorHandler.defaultMatcher());
exceptionHandler.match(ScriptExceptionHandler.defaultMatcher());
}
}
@Before
public void setUp() throws Throwable {
assumeTrue("Test disabled", test.isEnabled());
realm.initialize(new NullConsole(), test);
realm.get().createGlobalProperties(new Print(), Print.class);
exceptionHandler.setExecutionContext(realm.get().defaultContext());
if (test.negative) {
expected.expect(
Matchers.either(StandardErrorHandler.defaultMatcher()).or(ScriptExceptionHandler.defaultMatcher())
.or(Matchers.instanceOf(MultipleFailureException.class)));
} else {
errorHandler.match(StandardErrorHandler.defaultMatcher());
exceptionHandler.match(ScriptExceptionHandler.defaultMatcher());
}
}
@Override
public Statement apply(Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
List<Throwable> errors = new ArrayList<Throwable>();
System.out.println("Starting test ... " + description.getMethodName());
try {
base.evaluate();
} catch (Throwable e) {
errors.add(e);
} finally {
System.out.println("... test finished. " + description.getMethodName());
}
MultipleFailureException.assertEmpty(errors);
}
};
}
@Override
public Statement apply(Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
List<Throwable> errors = new ArrayList<Throwable>();
System.out.println("Starting test ... " + description.getMethodName());
try {
base.evaluate();
} catch (Throwable e) {
errors.add(e);
} finally {
System.out.println("... test finished. " + description.getMethodName());
}
MultipleFailureException.assertEmpty(errors);
}
};
}
@Override
public org.junit.runners.model.Statement apply(final org.junit.runners.model.Statement base,
Description description){
return new org.junit.runners.model.Statement(){
@Override
public void evaluate() throws Throwable{
try{
setup();
}catch(SQLException e){
throw new SetupFailureException(e);
}
List<Throwable> errors = new LinkedList<>();
try{
base.evaluate();
}catch(Throwable t){
errors.add(t);
}
MultipleFailureException.assertEmpty(errors);
}
};
}
private void closeResultSets() {
List<Throwable> t = Lists.newArrayListWithExpectedSize(0);
for (ResultSet r : resultSets) {
try {
if (!r.isClosed()) {
r.close();
}
} catch (Exception e) {
e.printStackTrace();
t.add(e);
}
}
try {
MultipleFailureException.assertEmpty(t);
} catch (Throwable throwable) {
throw new RuntimeException(throwable);
}
}
/**
* Adds a new problem.
*
* @param problem
* the {@link Throwable} to be added, must not be {@code null}
*/
public final void addProblem(final Throwable problem) {
if (problem instanceof MultipleTestProblems) {
addProblems((MultipleTestProblems) problem);
} else if (problem instanceof MultipleFailureException) {
addProblems(((MultipleFailureException) problem).getFailures());
} else {
problems.add(problem);
}
}
@Test
public void testCancellable() throws Exception {
// infinite item loops
when(componentDocsIterator.hasNext()).thenReturn(true);
when(componentDocsIterator.next()).thenReturn(mock(ODocument.class));
when(assetIterator.hasNext()).thenReturn(true);
when(assetIterator.next()).thenReturn(mock(Asset.class));
List<Runnable> cancelables = Arrays.asList(
() -> underTest.deleteUnusedComponents(new Date()),
() -> underTest.deleteUnusedAssets(new Date())
);
for (Runnable cancelable : cancelables) {
AtomicBoolean canceled = new AtomicBoolean(false);
Thread t = createTaskThread(cancelable, canceled);
t.start();
sleep((long) (Math.random() * 1000)); // sleep for up to a second (emulate task running)
canceled.set(true); // cancel the task
t.join(5000); // ensure task thread ends
if (t.isAlive()) {
fail("Task did not cancel");
}
if (uncaught.size() > 0) {
throw new MultipleFailureException(uncaught);
}
}
}
@Test
public void multipleExceptionReportedWhenTheyOccur() {
final Async async = new Async();
final IllegalStateException exception1 = new IllegalStateException("exception-1");
final IllegalStateException exception2 = new IllegalStateException("exception-2");
final IllegalStateException exception3 = new IllegalStateException("exception-3");
async.run(() -> {
throw exception1;
});
async.run(() -> {
throw exception2;
});
async.run(() -> {
throw exception3;
});
try {
async.verify();
fail("Exception should have been thrown in verify");
} catch (MultipleFailureException mfe) {
final Set<String> errorMessages = mfe.getFailures().stream()
.map(Throwable::getCause)
.map(Throwable::getMessage)
.collect(Collectors.toSet());
assertThat(errorMessages).isEqualTo(Sets.newHashSet("exception-1", "exception-2", "exception-3"));
assertThat(mfe.getMessage()).contains("Error while running Async: exception-1");
} catch (Throwable e) {
fail("Expected MultipleFailureException but got: " + e.getClass().getName());
}
}