users@glassfish.java.net

Re: Embedded testing woes

From: <glassfish_at_javadesktop.org>
Date: Thu, 08 Apr 2010 12:23:48 PDT

You can detect whether Surefire is in play and running with a forkMode of once by using this (horrible) little bit of logic:

[code]
  @BeforeClass
  public static void handleSurefire() {
    final String surefireRealClassPath = System.getProperty("surefire.real.class.path");
    final String surefireTestClassPath = System.getProperty("surefire.test.class.path");
    final String javaClassPath = System.getProperty("java.class.path");

    if (surefireRealClassPath == null) {
      if (surefireTestClassPath != null) {
        // surefire, and forkMode == never; puke
        fail("It looks like you're running under the Maven Surefire plugin, but your forkMode is set to never. forkMode must be set to once.");
      } else {
        final int psepIndex = javaClassPath.indexOf(File.pathSeparator);
        if (psepIndex < 0) {
          // No path separator, so only one classpath entry
          final int surefireBooterIndex = javaClassPath.indexOf("surefirebooter");
          if (surefireBooterIndex >= 0) {
            // surefire, and forkMode == always; puke
            fail("It looks like you're running under the Maven Surefire plugin, but your forkMode is set to always. forkMode must be set to once.");
          }
        }
      }
    }
  }
[/code]
[Message sent by forum member 'ljnelson']

http://forums.java.net/jive/thread.jspa?messageID=395994