2

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.

code_fish
  • 3,381
  • 5
  • 47
  • 90
kdg1955
  • 305
  • 2
  • 12
  • 7
    Observation - a unit test which invokes OpenOffice is *not* a unit test! That sounds like an integration test, at the least. – Duncan Jones Jan 21 '13 at 13:52
  • I use the java API of OpenOffice, and I can only test if a cell (i.e.) it correctly set by my program when I launch OO.Thus for me it are unit test. But if you have suggestions to unittest without launching OO, you can inform me. – kdg1955 Jan 21 '13 at 14:27
  • There's too much to say to squeeze it into a comment, but I think you should hide your usage of the Java API behind a façade. Provided this façade is presented as an interface, you can mock the object in your unit tests and confirm you are making the right calls to the API without firing up OO. – Duncan Jones Jan 21 '13 at 14:34
  • Cannot you use the same OpenOffice java API to check the result of the operation you performed ? – Samuel EUSTACHI Jan 21 '13 at 15:42
  • example: I have a wrapper class that set or get the color of a cell : "public static void setCellColor(XCell cell, int color)" and "public static int getCellColor(XCell cell)". Can I do unit tests without launching OO? – kdg1955 Jan 21 '13 at 16:06

3 Answers3

3

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.

Sébastien Le Callonnec
  • 26,254
  • 8
  • 67
  • 80
3

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

Stephen Connolly
  • 13,872
  • 6
  • 41
  • 63
0

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

Dan D.
  • 32,246
  • 5
  • 63
  • 79