आप JUnit आप के नवीनतम संस्करण उपयोग कर रहे हैं
ExtendedTestRunner.java (आज़माएं/कैच ब्लॉक में अपने तरीकों में से प्रत्येक रैप करने के लिए बिना) डिफ़ॉल्ट परीक्षण धावक आप के लिए इस संभाल करने के लिए विस्तार कर सकते हैं - नए परीक्षण धावक: -
public class ExtendedTestRunner extends BlockJUnit4ClassRunner
{
public ExtendedTestRunner(Class<?> clazz)
throws InitializationError
{
super(clazz);
}
@Override
protected Statement possiblyExpectingExceptions(FrameworkMethod method,
Object test,
Statement next)
{
ExtendedTest annotation = method.getAnnotation(ExtendedTest.class);
return expectsCauseException(annotation) ?
new ExpectCauseException(next, getExpectedCauseException(annotation)) :
super.possiblyExpectingExceptions(method, test, next);
}
@Override
protected List<FrameworkMethod> computeTestMethods()
{
Set<FrameworkMethod> testMethods = new HashSet<FrameworkMethod>(super.computeTestMethods());
testMethods.addAll(getTestClass().getAnnotatedMethods(ExtendedTest.class));
return testMethods;
}
@Override
protected void validateTestMethods(List<Throwable> errors)
{
super.validateTestMethods(errors);
validatePublicVoidNoArgMethods(ExtendedTest.class, false, errors);
}
private Class<? extends Throwable> getExpectedCauseException(ExtendedTest annotation)
{
if (annotation == null || annotation.expectedCause() == ExtendedTest.None.class)
return null;
else
return annotation.expectedCause();
}
private boolean expectsCauseException(ExtendedTest annotation) {
return getExpectedCauseException(annotation) != null;
}
}
ExtendedTest.java टिप्पणी के साथ परीक्षण तरीकों चिह्नित करने के लिए:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface ExtendedTest
{
/**
* Default empty exception
*/
static class None extends Throwable {
private static final long serialVersionUID= 1L;
private None() {
}
}
Class<? extends Throwable> expectedCause() default None.class;
}
ExpectCauseException.java - नई JUnit कथन:
public class ExpectCauseException extends Statement
{
private Statement fNext;
private final Class<? extends Throwable> fExpected;
public ExpectCauseException(Statement next, Class<? extends Throwable> expected)
{
fNext= next;
fExpected= expected;
}
@Override
public void evaluate() throws Exception
{
boolean complete = false;
try {
fNext.evaluate();
complete = true;
} catch (Throwable e) {
if (e.getCause() == null || !fExpected.isAssignableFrom(e.getCause().getClass()))
{
String message = "Unexpected exception cause, expected<"
+ fExpected.getName() + "> but was<"
+ (e.getCause() == null ? "none" : e.getCause().getClass().getName()) + ">";
throw new Exception(message, e);
}
}
if (complete)
throw new AssertionError("Expected exception cause: "
+ fExpected.getName());
}
}
उपयोग:
@RunWith(ExtendedTestRunner.class)
public class MyTests
{
@ExtendedTest(expectedCause = MyException.class)
public void someMethod()
{
throw new RuntimeException(new MyException());
}
}
महत्वहीन अतिरिक्त नोट: है यह "उम्मीद = ...", नहीं "उम्मीद = ..." – hoijui
शामिल करने के लिए मैं विश्वास नहीं कर सकता JUnit 5 जाहिरा तौर पर अपनी टिप्पणी के वाक्य रचना संवर्धित नहीं किया गया है इस। –