下面列出了怎么用org.junit.internal.runners.model.MultipleFailureException的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void evaluate() throws Throwable {
try {
statement.evaluate();
} catch (Throwable t) {
if (t instanceof MultipleFailureException) {
t = ((MultipleFailureException) t).getFailures().get(0);
}
if (t instanceof InvocationTargetException) {
t = ((InvocationTargetException) t).getTargetException();
}
String assertMsg;
if (t instanceof AssertionError) {
throw t;
} else if ((!isConnectionExpected) && t.getMessage() != null
&& t.getMessage().contains(DATASOURCE_ERROR)) {
// if we throw because of missing data-source and the db server isn't available, ignore it
return;
} else if (tClass == null) {
assertMsg = "Test threw unexpected exception: " + t;
} else if (tClass == t.getClass()) {
// we matched our expected exception
return;
} else {
assertMsg = "Expected test to throw " + tClass + " but it threw: " + t;
}
Error error = new AssertionError(assertMsg);
error.initCause(t);
throw error;
}
// can't be in the throw block
if (tClass != null) {
throw new AssertionError("Expected test to throw " + tClass);
}
}
private boolean isMissingCredsException( final Throwable t ) {
if ( t instanceof AmazonClientException ) {
final AmazonClientException ace = ( AmazonClientException ) t;
if ( ace.getMessage().contains( "could not get aws access key" ) || ace.getMessage().contains(
"could not get aws secret key from system properties" ) ) {
//swallow
return true;
}
}
if( t instanceof AwsPropertiesNotFoundException ){
return true;
}
/**
* Handle the multiple failure junit trace
*/
if( t instanceof MultipleFailureException ){
for(final Throwable failure : ((MultipleFailureException)t).getFailures()){
final boolean isMissingCreds = isMissingCredsException( failure );
if(isMissingCreds){
return true;
}
}
}
final Throwable cause = t.getCause();
if ( cause == null ) {
return false;
}
return isMissingCredsException( cause );
}
private boolean isMissingCredsException( final Throwable t ) {
// either no filename was provided or the filename provided doesn't actually exist on the file system
if ( t instanceof FileNotFoundException || t instanceof NullPointerException ) {
return true;
}
/**
* Handle the multiple failure junit trace
*/
if( t instanceof MultipleFailureException ){
for(final Throwable failure : ((MultipleFailureException)t).getFailures()){
final boolean isMissingCreds = isMissingCredsException( failure );
if(isMissingCreds){
return true;
}
}
}
final Throwable cause = t.getCause();
if ( cause == null ) {
return false;
}
return isMissingCredsException( cause );
}
private boolean isMissingCredsException( final Throwable t ) {
if ( t instanceof AmazonClientException ) {
final AmazonClientException ace = ( AmazonClientException ) t;
if ( ace.getMessage().contains( "could not get aws access key" ) || ace.getMessage().contains(
"could not get aws secret key from system properties" ) ) {
//swallow
return true;
}
}
/**
* Handle the multiple failure junit trace
*/
if( t instanceof MultipleFailureException ){
for(final Throwable failure : ((MultipleFailureException)t).getFailures()){
final boolean isMissingCreds = isMissingCredsException( failure );
if(isMissingCreds){
return true;
}
}
}
final Throwable cause = t.getCause();
if ( cause == null ) {
return false;
}
return isMissingCredsException( cause );
}
private Statement statement(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
Throwable caughtThrowable = null;
for (; currentRetry <= retryCount; currentRetry++) {
try {
testReport.appendHeader(description.getClassName() + "." + description.getMethodName()
+ " - Execution " + (exceptions.size() + 1) + "/" + getRetryCount());
base.evaluate();
testReport.flushExtraInfoHtml();
testReport.appendSuccess("Test ok");
testReport.flushExtraInfoHtml();
testReport.appendLine();
return;
} catch (Throwable t) {
if (t instanceof MultipleFailureException) {
MultipleFailureException m = (MultipleFailureException) t;
for (Throwable throwable : m.getFailures()) {
log.warn("Multiple exception element", throwable);
}
}
exceptions.add(t);
if (testReport != null) {
testReport.appendWarning("Test failed in retry " + exceptions.size());
testReport.appendException(t, testScenario);
testReport.flushExtraInfoHtml();
testReport.flushExtraErrorHtml();
}
caughtThrowable = t;
log.error(SEPARATOR);
log.error("{}: run {} failed", description.getDisplayName(), currentRetry, t);
log.error(SEPARATOR);
}
}
String errorMessage = "TEST ERROR: " + description.getMethodName() + " (giving up after "
+ retryCount + " retries)";
if (exceptions.size() > 0 && testReport != null) {
testReport.appendError(errorMessage);
testReport.appendLine();
}
throw caughtThrowable;
}
};
}