How can I recursively find and run all Junit 4 tests in Eclipse?

I would like to run all junit 4 tests in my Eclipse project. The project is configured using / source and / test. There are various packages under / test, for example:

com.yaddayadda.test.core.entity com.yaddayadda.test.core.framework 

If I right-click on the /test level in the package explorer and select Run As; Junit test I get an error message:

 No tests found with test runner 'JUnit 4'. 

If I right-click on com.yaddayadda.test.core.entity , it finds and runs all the tests inside this package. Thus, @Test annotations are correct (they were also correctly picked up by Ant on the build server). However, if I try to run all the tests in com.yaddayadda.test.core , then it will not detect.

In principle, it seems that it looks only in a package, and not in reduction for all children. Is there any way to fix this?

+6
eclipse junit4
source share
4 answers

If someone else is looking for a solution, I found the answer on the Burt Beckwith website:

http://burtbeckwith.com/blog/?p=52

To use this, simply right-click on it in the class tree in Eclipse and click on β€œRun as JUnit Test”.

 import java.io.File; import java.io.UnsupportedEncodingException; import java.lang.reflect.Modifier; import java.net.URLDecoder; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import org.apache.log4j.Logger; import org.junit.internal.runners.InitializationError; import org.junit.runner.Description; import org.junit.runner.RunWith; import org.junit.runner.notification.RunListener; import org.junit.runner.notification.RunNotifier; import org.junit.runners.Suite; /** * Discovers all JUnit tests and runs them in a suite. */ @RunWith(AllTests.AllTestsRunner.class) public final class AllTests { private static final File CLASSES_DIR = findClassesDir(); private AllTests() { // static only } /** * Finds and runs tests. */ public static class AllTestsRunner extends Suite { private final Logger _log = Logger.getLogger(getClass()); /** * Constructor. * * @param clazz the suite class - <code>AllTests</code> * @throws InitializationError if there a problem */ public AllTestsRunner(final Class<?> clazz) throws InitializationError { super(clazz, findClasses()); } /** * {@inheritDoc} * @see org.junit.runners.Suite#run(org.junit.runner.notification.RunNotifier) */ @Override public void run(final RunNotifier notifier) { initializeBeforeTests(); notifier.addListener(new RunListener() { @Override public void testStarted(final Description description) { if (_log.isTraceEnabled()) { _log.trace("Before test " + description.getDisplayName()); } } @Override public void testFinished(final Description description) { if (_log.isTraceEnabled()) { _log.trace("After test " + description.getDisplayName()); } } }); super.run(notifier); } private static Class<?>[] findClasses() { List<File> classFiles = new ArrayList<File>(); findClasses(classFiles, CLASSES_DIR); List<Class<?>> classes = convertToClasses(classFiles, CLASSES_DIR); return classes.toArray(new Class[classes.size()]); } private static void initializeBeforeTests() { // do one-time initialization here } private static List<Class<?>> convertToClasses( final List<File> classFiles, final File classesDir) { List<Class<?>> classes = new ArrayList<Class<?>>(); for (File file : classFiles) { if (!file.getName().endsWith("Test.class")) { continue; } String name = file.getPath().substring(classesDir.getPath().length() + 1) .replace('/', '.') .replace('\\', '.'); name = name.substring(0, name.length() - 6); Class<?> c; try { c = Class.forName(name); } catch (ClassNotFoundException e) { throw new AssertionError(e); } if (!Modifier.isAbstract(c.getModifiers())) { classes.add(c); } } // sort so we have the same order as Ant Collections.sort(classes, new Comparator<Class<?>>() { public int compare(final Class<?> c1, final Class<?> c2) { return c1.getName().compareTo(c2.getName()); } }); return classes; } private static void findClasses(final List<File> classFiles, final File dir) { for (File file : dir.listFiles()) { if (file.isDirectory()) { findClasses(classFiles, file); } else if (file.getName().toLowerCase().endsWith(".class")) { classFiles.add(file); } } } } private static File findClassesDir() { try { String path = AllTests.class.getProtectionDomain() .getCodeSource().getLocation().getFile(); return new File(URLDecoder.decode(path, "UTF-8")); } catch (UnsupportedEncodingException impossible) { // using default encoding, has to exist throw new AssertionError(impossible); } } } 
+4
source share

First: Select the project in Project Explorer and press Alt + Shift + X T. It will run all JUint tests within the project. The same thing can be done by right-clicking the project and selecting "Run as" β†’ JUnit test.

If this does not work (which is likely), go to "Run / Run configurations", create a new JUnit configuration and tell it to run all the tests in your project. If this does not work, I will need to take a look at your project before I can help.

+8
source share

Have you added /test to create a path -> source folders?

0
source share

I found that the Burt Beckwith code is wonderful, but it will run every test in the project no matter where you place AllTests. This modification of one function will allow you to place AllTests in any subdirectory of your project, and it will run tests only in this place.

 private static Class<?>[] findClasses() {List<File> classFiles = new ArrayList<File>(); String packagepath = AllTests.class.getPackage().getName().replace(".", "/"); File RELATIVE_DIR = new File( CLASSES_DIR.getAbsolutePath() + "\\" + packagepath ); findClasses(classFiles, RELATIVE_DIR); List<Class<?>> classes = convertToClasses(classFiles, CLASSES_DIR); return classes.toArray(new Class[classes.size()]); } 
0
source share

All Articles