Java Code Examples for org.junit.Test#expected()

The following examples show how to use org.junit.Test#expected() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RetryRule.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public Statement apply(Statement statement, Description description) {
	RetryOnFailure retryOnFailure = description.getAnnotation(RetryOnFailure.class);
	RetryOnException retryOnException = description.getAnnotation(RetryOnException.class);

	// sanity check that we don't use expected exceptions with the RetryOnX annotations
	if (retryOnFailure != null || retryOnException != null) {
		Test test = description.getAnnotation(Test.class);
		if (test.expected() != Test.None.class) {
			throw new IllegalArgumentException("You cannot combine the RetryOnFailure " +
					"annotation with the Test(expected) annotation.");
		}
	}

	// sanity check that we don't use both annotations
	if (retryOnFailure != null && retryOnException != null) {
		throw new IllegalArgumentException(
				"You cannot combine the RetryOnFailure and RetryOnException annotations.");
	}

	if (retryOnFailure != null) {
		return new RetryOnFailureStatement(retryOnFailure.times(), statement);
	}
	else if (retryOnException != null) {
		return new RetryOnExceptionStatement(retryOnException.times(), retryOnException.exception(), statement);
	}
	else {
		return statement;
	}
}
 
Example 2
Source File: RetryRule.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public Statement apply(Statement statement, Description description) {
	RetryOnFailure retryOnFailure = description.getAnnotation(RetryOnFailure.class);
	RetryOnException retryOnException = description.getAnnotation(RetryOnException.class);

	// sanity check that we don't use expected exceptions with the RetryOnX annotations
	if (retryOnFailure != null || retryOnException != null) {
		Test test = description.getAnnotation(Test.class);
		if (test.expected() != Test.None.class) {
			throw new IllegalArgumentException("You cannot combine the RetryOnFailure " +
					"annotation with the Test(expected) annotation.");
		}
	}

	// sanity check that we don't use both annotations
	if (retryOnFailure != null && retryOnException != null) {
		throw new IllegalArgumentException(
				"You cannot combine the RetryOnFailure and RetryOnException annotations.");
	}

	if (retryOnFailure != null) {
		return new RetryOnFailureStatement(retryOnFailure.times(), statement);
	}
	else if (retryOnException != null) {
		return new RetryOnExceptionStatement(retryOnException.times(), retryOnException.exception(), statement);
	}
	else {
		return statement;
	}
}
 
Example 3
Source File: ParameterTest.java    From openCypher with Apache License 2.0 5 votes vote down vote up
private static Class<? extends Throwable> getExpectedException( Test annotation )
{
    if ( annotation == null || annotation.expected() == Test.None.class )
    {
        return null;
    }
    else
    {
        return annotation.expected();
    }
}
 
Example 4
Source File: RetryRule.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public Statement apply(Statement statement, Description description) {
	RetryOnFailure retryOnFailure = description.getAnnotation(RetryOnFailure.class);
	RetryOnException retryOnException = description.getAnnotation(RetryOnException.class);

	// sanity check that we don't use expected exceptions with the RetryOnX annotations
	if (retryOnFailure != null || retryOnException != null) {
		Test test = description.getAnnotation(Test.class);
		if (test.expected() != Test.None.class) {
			throw new IllegalArgumentException("You cannot combine the RetryOnFailure " +
					"annotation with the Test(expected) annotation.");
		}
	}

	// sanity check that we don't use both annotations
	if (retryOnFailure != null && retryOnException != null) {
		throw new IllegalArgumentException(
				"You cannot combine the RetryOnFailure and RetryOnException annotations.");
	}

	if (retryOnFailure != null) {
		return new RetryOnFailureStatement(retryOnFailure.times(), statement);
	}
	else if (retryOnException != null) {
		return new RetryOnExceptionStatement(retryOnException.times(), retryOnException.exception(), statement);
	}
	else {
		return statement;
	}
}
 
Example 5
Source File: LoadTimeWeavableTestRunner.java    From rice with Educational Community License v2.0 5 votes vote down vote up
private Class<? extends Throwable> getExpectedException(Test annotation) {
    if (annotation == null || annotation.expected() == Test.None.class) {
        return null;
    } else {
        return annotation.expected();
    }
}
 
Example 6
Source File: LogLevelTestRule.java    From mapper with Apache License 2.0 5 votes vote down vote up
@Override
public Statement apply(final Statement statement, final Description description) {
  return new Statement() {
    @Override
    public void evaluate() throws Throwable {
      Test test = description.getAnnotation(Test.class);
      boolean exceptionExpected = test != null && test.expected() != Test.None.class;

      Level level = getLevel(description.getAnnotation(LogLevel.class));
      if (level == null && exceptionExpected) {
        level = Level.OFF;
      }

      Level prevLevel;
      if (level != null) {
        prevLevel = resetLogsLevel(level);
      } else {
        prevLevel = null;
      }

      try {
        statement.evaluate();
      } finally {
        if (prevLevel != null) {
          resetLogsLevel(prevLevel);
        }
      }
    }
  };
}
 
Example 7
Source File: EndToEndRunner.java    From buck with Apache License 2.0 5 votes vote down vote up
private Statement withExpectedExceptions(EndToEndTestDescriptor child, Statement statement) {
  FrameworkMethod verificationMethod = child.getMethod();
  Test annotation = verificationMethod.getAnnotation(Test.class);
  Class<? extends Throwable> expectedException = annotation.expected();
  // ExpectException doesn't account for the default Test.None.class, so skip expecting an
  // exception if it is Test.None.class
  if (expectedException.isAssignableFrom(Test.None.class)) {
    return statement;
  }
  return new ExpectException(statement, expectedException);
}
 
Example 8
Source File: SpringJUnit4ClassRunner.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Get the {@code exception} that the supplied {@linkplain FrameworkMethod
 * test method} is expected to throw.
 * <p>Supports JUnit's {@link Test#expected() @Test(expected=...)} annotation.
 * <p>Can be overridden by subclasses.
 * @return the expected exception, or {@code null} if none was specified
 */
@Nullable
protected Class<? extends Throwable> getExpectedException(FrameworkMethod frameworkMethod) {
	Test test = frameworkMethod.getAnnotation(Test.class);
	return (test != null && test.expected() != Test.None.class ? test.expected() : null);
}
 
Example 9
Source File: SpringJUnit4ClassRunner.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Get the {@code exception} that the supplied {@linkplain FrameworkMethod
 * test method} is expected to throw.
 * <p>Supports JUnit's {@link Test#expected() @Test(expected=...)} annotation.
 * <p>Can be overridden by subclasses.
 * @return the expected exception, or {@code null} if none was specified
 */
@Nullable
protected Class<? extends Throwable> getExpectedException(FrameworkMethod frameworkMethod) {
	Test test = frameworkMethod.getAnnotation(Test.class);
	return (test != null && test.expected() != Test.None.class ? test.expected() : null);
}
 
Example 10
Source File: SpringJUnit4ClassRunner.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Get the {@code exception} that the supplied {@linkplain FrameworkMethod
 * test method} is expected to throw.
 * <p>Supports JUnit's {@link Test#expected() @Test(expected=...)} annotation.
 * <p>Can be overridden by subclasses.
 * @return the expected exception, or {@code null} if none was specified
 */
protected Class<? extends Throwable> getExpectedException(FrameworkMethod frameworkMethod) {
	Test test = frameworkMethod.getAnnotation(Test.class);
	return (test != null && test.expected() != Test.None.class ? test.expected() : null);
}