views:

285

answers:

2

My unit tests are in a separate directory tree from my integration tests, but with the same package structure. My integration tests need external resources (e.g. a server) to be available, but my unit tests are properly independent of each other and the environment.

In IntelliJ-IDEA (v7) I have defined a JUnit Run/Debug Configuration to run all the tests in the top-level package, and this of course picks up my integration tests which fail.

I want to define a run-junit configuration that runs all my unit tests. Any ideas?

+2  A: 

Unfortunately there's no way to separate the output from the IntelliJ compile other than by classes and test classes within a single module (it's the classes that test runner is looking at).

So when I have integration tests I simply use a second module specific to these tests to get round this problem, specifying output directories as necessary for each module.

Nick Holt
Yes, that would be a correct approach to use different modules for different types of tests. In the Run/Debug configuration you specify which module classpath will be used.
CrazyCoder
I can't do this, there are already multiple modules in the project and we're working on one module per deliverable artefact principle
Paul McKenzie
+3  A: 

The answer is to create a test suite that contains only those tests underneath the unit test folder and run that instead. There is a junit-addon which does just this called DirectorySuiteBuilder but I only found this after I had pretty much re-invented the wheel.

And it's already been asked here!

import junit.framework.JUnit4TestAdapter;
import junit.framework.TestSuite;

import java.io.File;
import java.io.IOException;

public class DirectoryTestSuite {
    static final String rootPath = "proj\\src\\test\\java\\";
    static final ClassLoader classLoader = DirectoryTestSuite.class.getClassLoader();

    public static TestSuite suite() throws IOException, ClassNotFoundException {
 final TestSuite testSuite = new TestSuite();
 findTests(testSuite, new File(rootPath));
 return testSuite;
    }

    private static void findTests(final TestSuite testSuite, final File folder) throws IOException, ClassNotFoundException {
 for (final String fileName : folder.list()) {
     final File file = new File( folder.getPath() + "/" +fileName);
     if (file.isDirectory()) {
  findTests(testSuite, file);
     } else if (isTest(file)) {
  addTest(testSuite, file);
     }
 }
    }

    private static boolean isTest(final File f) {
 return f.isFile() && f.getName().endsWith("Test.java");
    }

    private static void addTest(final TestSuite testSuite, final File f) throws ClassNotFoundException {
 final String className = makeClassName(f);
 final Class testClass = makeClass(className);
 testSuite.addTest(new JUnit4TestAdapter(testClass));
    }

    private static Class makeClass(final String className) throws ClassNotFoundException {
 return (classLoader.loadClass(className));
    }

    private static String makeClassName(final File f) {
 return f.getPath().replace(rootPath, "").replace("\\", ".").replace(".java", "");
    }
}
Paul McKenzie