MathJax

SyntaxHighlighter

Highlight

Custom CSS

Sunday, February 17, 2008

Maven + Scala + TestNG + Netbeans

There are lots of articles on the web on how to set up Maven with Scala, or TestNG and Scala, or Netbeans with Scala, but there aren't any resources that tell you how to tie them all together. The cursed Maven pom is a mystery, but I've managed to unearth the ancient hieroglyphs in the exact sequence that will make Maven, TestNG, and Scala work well together.

If you want to use all three along with an IDE, I hear jEdit is somewhat popular for Scala users, but I'm somewhat invested in Netbeans myself.

Update

Blast all these IDEs to hell! I've had my last straw and I'm sticking with vim! The syntax highlighting is better with jEdit and its a little more stable, but Netbeans has a cleaner, more familiar interface (in my opinion). Stability is only a problem because you have to use the dev version of Netbeans to get the Scala plugins working. Anywho, once you get your editor/IDE set up, you'll want to know exactly what type in your pom to make everything work correctly.

If you follow the links for setting up Maven and Scala, your pom will look somewhat correct, you'll need to add the TestNG dependency:

<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>5.7</version>
  <classifier>jdk15</classifier>
  <scope>test</scope>
</dependency>

Now, if you try to compile a TestNG test, you'll get compiler warnings about annotations. Make sure the target VM is set for 1.5 with the maven-scala-plugin:

<plugin>
  <groupId>org.scala-tools</groupId>
  <artifactId>maven-scala-plugin</artifactId>
  <version>2.3</version>
  <executions>
    <execution>
      <goals>
        <goal>compile</goal>
        <goal>testCompile</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <scalaVersion>2.6.1</scalaVersion>
    <args>
      <arg>-target:jvm-1.5</arg>
    </args>
  </configuration>
</plugin>

By default, the 2.3 version of the maven-surefire-plugin is pulled in, but you'll want 2.4. Additionally, you'll want to add some information so surefire will find the tests you want to run:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.4</version>
  <executions>
    <execution>
      <goals>
        <goal>test</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <classesDirectory>target/classes</classesDirectory>
    <testClassesDirectory>target/test-classes</testClassesDirectory>
    <testSourceDirectory>src/test/scala</testSourceDirectory>
  </configuration>
</plugin>

Now if you have a test that looks like this:

import org.testng.annotations._

@Test
class MyCoolTest {
  def testConsole = {
    Console println "Hi!"
  }
}

...you'll get a nice, satisfying result:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
[Parser] Running:
  Command line suite

Hi!
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.411 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0