Question

As mentioned on different locations, maven (version 3.0.1) doesn't support a CLASSPATH outside the project. Thus when a jUnitTest want to launch an external application (like OpenOffice i.e.) it fails when launching the command '$> mvn test'. In eclipse I can manualy add the CLASSPATH in 'run configuration' and then it works fine.

My question: Can I add code inside my jUnitTest source which verifies that I'm in Eclipse? As this will just skip the tests on the command line. While other tests still continue on the command line.

Was it helpful?

Solution

Write a class that verifies if the class you want to use is loaded (will need to be a separate class due to how classloaders work. it will be something like

public class OODetector {
  public static boolean isOOOnClassPath() {
    try {
      OODetector.class.getClassLoader().loadClass("org.... class name here ...");
      return true;
    } catch (Throwable t) {
      return false;
    }
  } 
}

Now at the start of any test that requires the class on the classpath, just put Assume.assumeThat(OODetector.isOOOnClassPath(), is(true)); and your test will be skipped in such cases, e.g.

public class OOTests {
  @Test
  public void smokes() throws Exception {
    Assume.assumeThat(OODetector.isOOOnClassPath(), is(true));
    // rest of test
  }
}

or better yet, put it in the @BeforeClass, e.g.

public class OOTests {
  @BeforeClass
  public static void smokes() throws Exception {
    Assume.assumeThat(OODetector.isOOOnClassPath(), is(true));
  }

  // tests
}

That will skip all the tests in the class if OO is not on the classpath, does not affect failed count, but keeps the total test count valid, and when you figure out the correct way to get the runtime dependencies onto your Maven-launched test classpath then the tests will magically start running

OTHER TIPS

You can use Maven profiles to activate different properties, run different commands, amend your classpath as needed, or even filter out some tests. You could, for instance, create a specific profile only used in Eclipse.

Just add a runtime or VM flag that you can test for its value inside the code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top