Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Incase anyone else is looking for a solution to this, I found an answer here on Burt Beckwith's website:</p> <p><a href="http://burtbeckwith.com/blog/?p=52" rel="nofollow">http://burtbeckwith.com/blog/?p=52</a></p> <blockquote> <p>To use this, just right-click it in the class tree in Eclipse and click “Run As JUnit Test”.</p> </blockquote> <pre><code>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 - &lt;code&gt;AllTests&lt;/code&gt; * @throws InitializationError if there's a problem */ public AllTestsRunner(final Class&lt;?&gt; 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&lt;?&gt;[] findClasses() { List&lt;File&gt; classFiles = new ArrayList&lt;File&gt;(); findClasses(classFiles, CLASSES_DIR); List&lt;Class&lt;?&gt;&gt; classes = convertToClasses(classFiles, CLASSES_DIR); return classes.toArray(new Class[classes.size()]); } private static void initializeBeforeTests() { // do one-time initialization here } private static List&lt;Class&lt;?&gt;&gt; convertToClasses( final List&lt;File&gt; classFiles, final File classesDir) { List&lt;Class&lt;?&gt;&gt; classes = new ArrayList&lt;Class&lt;?&gt;&gt;(); 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&lt;?&gt; 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&lt;Class&lt;?&gt;&gt;() { public int compare(final Class&lt;?&gt; c1, final Class&lt;?&gt; c2) { return c1.getName().compareTo(c2.getName()); } }); return classes; } private static void findClasses(final List&lt;File&gt; 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); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload