freedigitalphotos.net |
In case you want to include Scala code in your project, you can do this by using the maven-scala-plugin.
The plugin has reasonable defaults, configuring a scala project with maven has become quite trivial.
All you have to do is to include the following code in your build section of your pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<scalaVersion>2.9.1</scalaVersion>
<jvmArgs>
<jvmArg>-client</jvmArg>
<jvmArg>-Xms64m</jvmArg>
<jvmArg>-Xmx1024m</jvmArg>
</jvmArgs>
<args>
<arg>-deprecation</arg>
<arg>-dependencyfile</arg>
<arg>${project.build.directory}/.scala_dependencies</arg>
</args>
</configuration>
</plugin>
</plugins>
</build>
This suffices if you place your Scala classes in src/main/scala and src/test/scala.
The jvm arguments give the Scala compiler a little more space which may speed up the compilation process.
Running Unit Tests with Scala and Maven
I'm using JUnit and ScalaTest in my project. As a matter of fact, it doesn't really matter which framework you choose for your tests, as long as you write tests. If maintenance of tests is more of a concern at a later phase of the project, it will pay of that you apply the same quality standards to your test code as you do for your production code.
I've learned that tests need a certain amount of work and attention in order to give you the revenue they promise. In trivial cases it is ok to cut'n paste in order to give test cases some variation, but this gets easily out of control - so be sure you create helper methods and classes which encapsulate complicated setup strategies in order to achieve a code reuse. It's more or less always the same.
Coming back to ScalaTest, it is a very nice testing framework which allows you to pursue different styles of testing, whatever fits best for your taste or environment. Here is a taste of ScalaTest:
package nxs.editor.usecases
import org.junit.runner.RunWith
import org.scalatest.BeforeAndAfterAll
import org.scalatest.FlatSpec
import org.scalatest.Tag
import org.scalatest.junit.JUnitRunner
object ATag extends Tag("ATag")
@RunWith(classOf[JUnitRunner])
class ExampleSpec extends FlatSpec with BeforeAndAfterAll {
override def beforeAll() = {
// doReallyExpensiveSetup()
}
override def afterAll() = {
// cleanup()
}
"this" should "be really green" taggedAs (ATag) in { assert(true) }
}
ScalaTest is really very well documented on their site, have a look.
You may have noticed I've using here an annotation org.junit.runner.RunWith - this is solely for the purpose to execute the Specs in Eclipse as tests. As soon as the IDE Support for ScalaTest is fully functional one could remove this annotation and have an equally comfortable integration. There is a video online where Bill Venners shows what can be expected for ScalaTest integration in Eclipse - I'm looking forward to it!
To integrate ScalaTest in your maven build you want to have something like this in your pom:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7</version>
<configuration>
<skipTests>true</skipTests><!-- disable surefire -->
</configuration>
</plugin>
<!-- enable scalatest -->
<!-- see http://www.scalatest.org/user_guide/using_the_scalatest_maven_plugin -->
<plugin>
<groupId>org.scalatest</groupId>
<artifactId>maven-scalatest-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<configuration>
<reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
<junitxml>.</junitxml>
<filereports>WDF TestSuite.txt</filereports>
<tagsToInclude>ATag</tagsToInclude>
</configuration>
<executions>
<execution>
<id>test</id>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
you will also need the dependency for scalatest of course:
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_2.9.1</artifactId>
<version>1.7.2</version>
<scope>test</scope>
</dependency>
and a proper download site for the plugin:
<pluginRepositories>
<pluginRepository>
<id>oss sonatype</id>
<url>https://oss.sonatype.org/content/groups/public</url>
</pluginRepository>
</pluginRepositories>
But see for yourself here.
<build> <plugins> <plugin> <groupId>org.scala-tools</groupId> <artifactId>maven-scala-plugin</artifactId> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions> <configuration> <scalaVersion>2.9.1</scalaVersion> <jvmArgs> <jvmArg>-client</jvmArg> <jvmArg>-Xms64m</jvmArg> <jvmArg>-Xmx1024m</jvmArg> </jvmArgs> <args> <arg>-deprecation</arg> <arg>-dependencyfile</arg> <arg>${project.build.directory}/.scala_dependencies</arg> </args> </configuration> </plugin> </plugins> </build>
This suffices if you place your Scala classes in src/main/scala and src/test/scala.
The jvm arguments give the Scala compiler a little more space which may speed up the compilation process.
Running Unit Tests with Scala and Maven
I've learned that tests need a certain amount of work and attention in order to give you the revenue they promise. In trivial cases it is ok to cut'n paste in order to give test cases some variation, but this gets easily out of control - so be sure you create helper methods and classes which encapsulate complicated setup strategies in order to achieve a code reuse. It's more or less always the same.
Coming back to ScalaTest, it is a very nice testing framework which allows you to pursue different styles of testing, whatever fits best for your taste or environment. Here is a taste of ScalaTest:
package nxs.editor.usecases import org.junit.runner.RunWith import org.scalatest.BeforeAndAfterAll import org.scalatest.FlatSpec import org.scalatest.Tag import org.scalatest.junit.JUnitRunner object ATag extends Tag("ATag") @RunWith(classOf[JUnitRunner]) class ExampleSpec extends FlatSpec with BeforeAndAfterAll { override def beforeAll() = { // doReallyExpensiveSetup() } override def afterAll() = { // cleanup() } "this" should "be really green" taggedAs (ATag) in { assert(true) } }
ScalaTest is really very well documented on their site, have a look.
You may have noticed I've using here an annotation org.junit.runner.RunWith - this is solely for the purpose to execute the Specs in Eclipse as tests. As soon as the IDE Support for ScalaTest is fully functional one could remove this annotation and have an equally comfortable integration. There is a video online where Bill Venners shows what can be expected for ScalaTest integration in Eclipse - I'm looking forward to it!
To integrate ScalaTest in your maven build you want to have something like this in your pom:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.7</version> <configuration> <skipTests>true</skipTests><!-- disable surefire --> </configuration> </plugin> <!-- enable scalatest --> <!-- see http://www.scalatest.org/user_guide/using_the_scalatest_maven_plugin --> <plugin> <groupId>org.scalatest</groupId> <artifactId>maven-scalatest-plugin</artifactId> <version>1.0-SNAPSHOT</version> <configuration> <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory> <junitxml>.</junitxml> <filereports>WDF TestSuite.txt</filereports> <tagsToInclude>ATag</tagsToInclude> </configuration> <executions> <execution> <id>test</id> <goals> <goal>test</goal> </goals> </execution> </executions> </plugin>
you will also need the dependency for scalatest of course:
<dependency> <groupId>org.scalatest</groupId> <artifactId>scalatest_2.9.1</artifactId> <version>1.7.2</version> <scope>test</scope> </dependency>
and a proper download site for the plugin:
<pluginRepositories> <pluginRepository> <id>oss sonatype</id> <url>https://oss.sonatype.org/content/groups/public</url> </pluginRepository> </pluginRepositories>
But see for yourself here.
No comments:
Post a Comment