下面列出了怎么用org.junit.platform.commons.JUnitException的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Returns the internally saved revision number from the given
* {@link SvnLinkedArtifact}.
*
* @param artifact the artifact
* @return the internally saved revision number
*/
private static long getRevision(final SvnLinkedArtifact artifact) {
try {
final Field serializationObjectField = SvnLinkedArtifact.class.getDeclaredField("serializationObject");
serializationObjectField.setAccessible(true);
final Object serializationObject = serializationObjectField.get(artifact);
final Class<?>[] classes = SvnLinkedArtifact.class.getDeclaredClasses();
final Optional<Class<?>> internalClass = Arrays.stream(classes)
.filter(clazz -> clazz.getSimpleName().equals("SvnLinkedArtifactSerializationObject")).findAny();
if (internalClass.isPresent()) {
final Field revisionField = internalClass.get().getDeclaredField("revision");
revisionField.setAccessible(true);
return (long) revisionField.get(serializationObject);
} else {
throw new JUnitException("Internal class 'SvnLinkedArtifactSerializationObject' not found.");
}
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
throw new JUnitException("Problem getting revision from SvnLinkedArtifact.", e);
}
}
private void ensureNoInjectAnnotationIsUsed(Class<?> testClass) {
Class<?> current = testClass;
while (current.getSuperclass() != null) {
for (Field field : current.getDeclaredFields()) {
Inject injectAnnotation = field.getAnnotation(Inject.class);
if (injectAnnotation != null) {
throw new JUnitException(
"@Inject is not supported in NativeImageTest tests. Offending field is "
+ field.getDeclaringClass().getTypeName() + "."
+ field.getName());
}
}
current = current.getSuperclass();
}
}
private ExtensionContext getParentMethodExtensionContent(ExtensionContext extensionContext) {
Optional<ExtensionContext> parent = extensionContext.getParent();
// the class MethodExtensionContext is part of junit-jupiter-engine and has only a
// package-private visibility thus this workaround is needed.
while (!parent
.map(Object::getClass)
.map(Class::getName)
.filter(s -> s.endsWith("MethodExtensionContext"))
.isPresent()) {
parent = parent.flatMap(ExtensionContext::getParent);
}
return parent.orElseThrow(
() ->
new JUnitException(
String.format(
"Test '%s' does not have a parent method", extensionContext.getUniqueId())));
}
@Override
public void interceptTestMethod(
Invocation<Void> invocation,
ReflectiveInvocationContext<Method> invocationContext,
ExtensionContext extensionContext) {
if (isAnnotated(invocationContext.getExecutable(), WithAccessIds.class)) {
throw new JUnitException("Please use @TestTemplate instead of @Test for multiple accessIds");
}
extractAccessIdAndPerformInvocation(invocation, invocationContext.getExecutable());
}
private static void checkResult(boolean result) {
if (!result) {
throw new JUnitException("Could not initialize");
}
}