net.serenitybdd.junit.finder.TestFinder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of serenity-junit Show documentation
Show all versions of serenity-junit Show documentation
Serenity JUnit integration
package net.serenitybdd.junit.finder;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import net.serenitybdd.junit.runners.SerenityRunner;
import net.thucydides.core.reflection.ClassFinder;
import net.thucydides.junit.annotations.UseTestDataFrom;
import net.thucydides.junit.runners.ThucydidesRunner;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
/**
* The TestFinder class lets you find the Thucydides tests or test methods underneath a given package.
* You instantiate a TestFinder by providing the top-level package where the tests live.
* You can then find the list of Thucydides test classes using getNormalTestClasses(), getDataDrivenTestClasses(),
* and getAllTestClasses() (which returns both normal and data-driven tests).
* You may also need to retrieve the list of test methods for a particular category of class. You can do this using the
* getTestMethodsFrom() method, e.g.
*
new TestFinder("my.package").getTestMethodsFrom().normalTestClasses()
*/
public abstract class TestFinder {
protected final String rootPackage;
/**
* Create a new test finder instance that will look for tests in the packages underneath the given root package.
* @param rootPackage The root package used as the starting point when looking for test classes.
*/
protected TestFinder(final String rootPackage) {
this.rootPackage = rootPackage;
}
public static TestFinderBuilderFactory thatFinds() {
return new TestFinderBuilderFactory();
}
public abstract List> getClasses();
public abstract int countTestMethods();
public TestMethodFinder findTestMethods() {
return new TestMethodFinder(this);
}
protected List> getAllTestClasses() {
return ClassFinder.loadClasses().annotatedWith(RunWith.class).fromPackage(rootPackage);
}
protected Set> getNormalTestClasses() {
Set> normalTestClasses = Sets.newHashSet();
for(Class> testClass : getAllTestClasses()) {
if (normalThucydidesTest(testClass)) {
normalTestClasses.add(testClass);
}
}
return normalTestClasses;
}
protected List> getDataDrivenTestClasses() {
return ClassFinder.loadClasses().annotatedWith(UseTestDataFrom.class).fromPackage(rootPackage);
}
protected List> sorted(List> classes) {
Collections.sort(classes, byClassName());
return classes;
}
@SuppressWarnings("deprecation")
private boolean normalThucydidesTest(Class> testClass) {
RunWith runWith = testClass.getAnnotation(RunWith.class);
return ((runWith != null)
&& ((runWith.value() == ThucydidesRunner.class) || (runWith.value() == SerenityRunner.class)));
}
public List getAllTestMethods() {
return findMethodsFrom(getClasses());
}
private List findMethodsFrom(List> testClasses) {
List methods = Lists.newArrayList();
for (Class> testClass : testClasses) {
addEachMatchingTestMethodFrom(testClass).to(methods);
}
Collections.sort(methods, byName());
return methods;
}
private Comparator byName() {
return new Comparator() {
public int compare(Method method, Method method1) {
return method.getName().compareTo(method1.getName());
}
};
}
private static class TestMethodSearchBuilder {
private final Class> testClass;
private TestMethodSearchBuilder(Class> testClass) {
this.testClass = testClass;
}
public void to(List methods) {
for(Method method : testClass.getMethods()) {
if (method.isAnnotationPresent(Test.class)) {
methods.add(method);
}
}
}
}
private TestMethodSearchBuilder addEachMatchingTestMethodFrom(Class> testClass) {
return new TestMethodSearchBuilder(testClass);
}
private Comparator> byClassName() {
return new Comparator>() {
public int compare(Class> class1, Class> class2) {
return class1.getName().compareTo(class2.getName());
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy