下面列出了怎么用org.junit.internal.MethodSorter的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* {@inheritDoc}
*/
@Override
protected void scanAnnotatedMembers(final Map<Class<? extends Annotation>,
List<FrameworkMethod>> methodsForAnnotations,
final Map<Class<? extends Annotation>, List<FrameworkField>> fieldsForAnnotations) {
for (final Class<?> eachClass : getSuperClasses(getJavaClass())) {
for (final Method eachMethod : MethodSorter.getDeclaredMethods(eachClass)) {
addToAnnotationLists(new FrameworkMethod(eachMethod), methodsForAnnotations);
}
// Fields are ignored
}
for (final Map.Entry<Class<? extends Annotation>,
List<FrameworkMethod>> methodsEntry : methodsForAnnotations.entrySet()) {
final Class<? extends Annotation> key = methodsEntry.getKey();
if (key == Test.class) {
final List<FrameworkMethod> methods = methodsEntry.getValue();
final List<FrameworkMethod> newMethods = new ArrayList<>(methods.size() * 2);
for (final FrameworkMethod m : methods) {
newMethods.add(new StandardsFrameworkMethod(m.getMethod(), false));
newMethods.add(new StandardsFrameworkMethod(m.getMethod(), true));
}
methodsForAnnotations.put(key, newMethods);
}
}
}
@Override
public void setUp() throws Exception {
baseSetUp();
if (!beforeClassDone) {
beforeClass();
beforeClassDone = true;
}
if (lastTest == null) {
// for class-level afterClass, list the test methods and do the
// afterClass in the tearDown of last method
Class<?> scanClass = getClass();
while (Test.class.isAssignableFrom(scanClass)) {
for (Method m : MethodSorter.getDeclaredMethods(scanClass)) {
String methodName = m.getName();
if (methodName.startsWith("test")
&& m.getParameterTypes().length == 0
&& m.getReturnType().equals(Void.TYPE)) {
lastTest = methodName;
}
}
scanClass = scanClass.getSuperclass();
}
if (lastTest == null) {
fail("Could not find any last test in " + getClass().getName());
} else {
getLogWriter()
.info("Last test for " + getClass().getName() + ": " + lastTest);
}
}
}
@Override
public void setUp() throws Exception {
baseSetUp();
if (!beforeClassDone) {
beforeClass();
beforeClassDone = true;
}
if (lastTest == null) {
// for class-level afterClass, list the test methods and do the
// afterClass in the tearDown of last method
Class<?> scanClass = getClass();
while (Test.class.isAssignableFrom(scanClass)) {
for (Method m : MethodSorter.getDeclaredMethods(scanClass)) {
String methodName = m.getName();
if (methodName.startsWith("test")
&& m.getParameterTypes().length == 0
&& m.getReturnType().equals(Void.TYPE)) {
lastTest = methodName;
}
}
scanClass = scanClass.getSuperclass();
}
if (lastTest == null) {
fail("Could not find any last test in " + getClass().getName());
} else {
getLogWriter()
.info("Last test for " + getClass().getName() + ": " + lastTest);
}
}
}