specs 2, Scala and maven with IntelliJ 12.1 and getting things right

Recently, to learn a test framework for Scala ( even for Java) I chose specs2 .  Its still difficult to move to SBT, largely due to the fact that I am yet to be comfortable using it. So, for the time being I will stick to maven.
To configure the scala/java project edit the pom.xml to include
    
     <dependencies>
        <!-- specs2 version I used : 1.12.3-->
        <dependency>
            <groupid>org.specs2</groupid>
            <artifactid>specs2_${scala.version}</artifactid>
            <version>${specs2.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>${scala.version}</version>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>src/main/scala</sourceDirectory>
        <testSourceDirectory>src/test/scala</testSourceDirectory>
            <plugin>
                <groupId>org.scala-tools</groupId>
                <artifactId>maven-scala-plugin</artifactId>
                <version>2.15.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                     <jvmArgs>
                        <jvmArg>-Xms128m</jvmArg>
                        <jvmArg>-Xmx1024m</jvmArg>
                    </jvmArgs>
                    <scalaVersion>${scala.version}</scalaVersion>
                </configuration>
            </plugin>
     </build>

Include tests under  src/scala/test . An ubiquitous example
  
import org.specs2.mutable._
import org.junit.runner.RunWith
import org.specs2.runner.JUnitRunner


@RunWith(classOf[JUnitRunner])
class MyTest extends Specification{



  "The 'Hello world' string" should {
    "contain 11 characters" in {
      "Hello world" must have size (11)
    }
    "start with 'Hello'" in {
      "Hello world" must startWith("Hello")
    }
    "end with 'world'" in {
      "Hello world" must endWith("world")
    }
  }
  // Update : by default tests are run in parallel, to execute them sequentially
  // add `sequential` before the spec e.g.

  sequential
  "My sequential spec" should {
     var a = 5
    "add 1 to a" in {
      a = a+1
      a mustEqual(6)
    }

    "multiply current a by 2" in {
      a = a* 2
      a mustEqual(12)
    }
  }

 // try removing `sequential` from the spec, it would fail randomly.
}

Thats it!. For running tests from IntelliJ simply right-click and run, and from cmdline execute mvn test


3 comments:

  1. Hi,

    FYI maven-scala-plugin is deprecated. Use scala-maven-plugin instead.

    /davidB (author of both plugin)

    ReplyDelete
    Replies
    1. Hi,

      Thanks for the heads up. I will update it.

      Cheers.

      Delete
  2. I have been searching for a useful post like this on salesforce course details, it is highly helpful for me and I have a great experience with this Salesforce Training who are providing certification and job assistance.
    Salesforce training Hyderabad 

    ReplyDelete