Joshua MN
Joshua MN

Reputation: 1516

Can't run Scalatest with Gradle

task scalaTest(dependsOn: testClasses) << {
    description = 'Runs Scalatest suite'
    ant.taskdef(name: 'scalatest',
            classname: 'org.scalatest.tools.ScalaTestAntTask',
            classpath: sourceSets.test.runtimeClasspath.asPath
    )
    ant.scalatest(runpath: sourceSets.test.output.classesDir,
            haltonfailure: 'true', fork: 'false') {
        reporter(type: 'stdout')
    }
}

I run gradle scalaTest and I get:

* What went wrong:
Execution failed for task ':scalaTest'.
> java.lang.NoClassDefFoundError: scala/reflect/ClassManifest$

I am using Scala 2.10.2 and Gradle 1.7

dependencies {
    compile 'org.scala-lang:scala-library:2.10.2'   
    testCompile 'org.scalatest:scalatest:1.3'
    testCompile 'org.scalamock:scalamock_2.10:3.0.1'
}

What's wrong??

Upvotes: 30

Views: 20251

Answers (5)

Machisuji
Machisuji

Reputation: 738

You can put the following in your build.gradle:

task spec(dependsOn: ['testClasses'], type: JavaExec) {
  main = 'org.scalatest.tools.Runner'
  args = ['-R', 'build/classes/scala/test', '-o']
  classpath = sourceSets.test.runtimeClasspath
}

Note: The path my be different depending on the gradle version as pointed out in the comments by @MikeRylander. Before gradle 4 it used to be 'build/classes/test'.

Then just run gradle spec to execute your tests. I named the task spec because there already is a test task. I don't know if you can override the default test task.

You can look up the available options here.

Upvotes: 24

Developer
Developer

Reputation: 711

This response may be a bit late. But for one using scala with gradle (5.x), the following works.

Add the following plugin to gradle.

plugins {
  id "com.github.maiflai.scalatest" version "0.25"
}

To run the code

> gradle test

As a bonus the test results from the above plugin would also be reported better than the default report.

Upvotes: 5

rarry
rarry

Reputation: 3573

I do not know how to solve this one, but I can offer you a workaround. Annotate your test classes with @RunWith(classOf[JUnitRunner]), like this:

import org.scalatest.junit.JUnitRunner
import org.junit.runner.RunWith

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

and then, gradle test should work.

Edit:

My dependencies:

compile "org.scala-lang:scala-library:2.10.1"
testCompile "org.scalatest:scalatest_2.10:1.9.1"

Upvotes: 34

Victor Sorokin
Victor Sorokin

Reputation: 11996

As of Gradle 3.2.1, following root build.gradle runs both JUnit and ScalaTest code. This code is for multi-project build, enabled via settings.gradle, that's why subprojects used.
Plus, it's on purpose uses explicit Gradle API, to minimize Gradle DSL magic.

description = 'root project'

def enableScalaTest(Project project) {
    Task scalaTest = project.task(
            [
                    'dependsOn': 'testClasses',
                    'description': 'Runs ScalaTest tests in the project'
            ],
            'scalaTest',
            {
                ext.inputDir = project.tasks.getByName('compileTestScala').destinationDir
                inputs.dir(ext.inputDir)
            }
    )

    scalaTest.doLast({
        ant.taskdef(name: 'scalatest',
                classname: 'org.scalatest.tools.ScalaTestAntTask',
                classpath: project.sourceSets.test.runtimeClasspath.asPath)
        ant.scalatest(runpath: ext.inputDir,
                fork: 'false',
                haltonfailure: 'true')
                { reporter(type: 'stdout') }
    })

    project.getTasks().getByName('build').dependsOn(scalaTest)
}

subprojects {
    apply plugin: 'scala'

    repositories {
        mavenCentral()
    }

    dependencies {
        compile 'org.scala-lang:scala-library:2.12.1'

        testCompile 'junit:junit:4.12'
        testCompile 'org.scalatest:scalatest_2.12:3.0.1'
    }

    enableScalaTest(delegate)
}

Upvotes: 3

voxoid
voxoid

Reputation: 1289

rarry is correct. And even better, you can annotate a base test class with @RunWith(classOf[JUnitRunner]) once to cause all of your ScalaTest tests to run with the JUnit runner (assuming they extend the base class), e.g.:

import org.junit.runner.RunWith
import org.scalatest._
import org.scalatest.junit.JUnitRunner

@RunWith(classOf[JUnitRunner])
abstract class UnitSpec extends FlatSpec with Matchers

Upvotes: 3

Related Questions