Using Scala and TestNG with Gradle

December 30, 2010

During the christmas holidays i tried out Scala and TestNG with Gradle.

I brushed up my knowledge of the programming language Scala during the christmas holidays. I read the book Programming in Scala by Odersky et al. again and i started to develop some code using the Scala plugin of IntelliJ IDEA 10. If you are programming in Groovy or Scala you really should give IDEA a try. In my opinion it is the best IDE.

Since i am a fan of TestNG i wanted to use it, too. Actually it’s easily usable with Scala using ScalaTest.The following snippet is an example of the TestNGSuite and the equality === which gives an error message it it’s operants are not equal.

package net.dinkla.scalaville.utilities

import org.scalatest.testng.TestNGSuite
import org.testng.annotations.Test

class ValueTest extends TestNGSuite {

  object TestMeasure extends Measure("TestMeasure")

  @Test
  def value() = {
    val v1 = new Value(20, TestMeasure)
    val v2 = new Value(10, TestMeasure)
    val v3 = v1 + v2
    assert(v3.amount === 30)
  }
}

As a build tool i used gradle for the first time. And it was not that straightforward, I had to search the web and read manuals for more than half an hour to get this working. The following is the complete build script. My project deviates from the standard directory structure, so i have to declare, that my source code is in src and tests are in test.

apply plugin: 'scala'

repositories {
    mavenRepo urls: 'http://scala-tools.org/repo-releases'
    mavenCentral()
}

dependencies {
    scalaTools 'org.scala-lang:scala-compiler:2.8.1'
    scalaTools 'org.scala-lang:scala-library:2.8.1'
    compile 'org.scala-lang:scala-library:2.8.1'
    testCompile 'org.testng:testng:5.14.4'
    testCompile 'org.scalatest:scalatest:1.2'
}

sourceSets {
    main {
        scala {
            srcDir 'src'
        }
    }
    test {
        scala {
            srcDir 'test'
        }
    }
}

test {
    useTestNG()
}

Lessons learned: At the time of writing the central maven repository has older versions of ScalaTest in the directory org.scala-tools.testing. Do not use these with the newest TestNG and Scala 2.8. Use the newer version 1.2 from the directory org.scalatest in the scala-tools.org repository.