Question

I have a Scala Specs 2 test that looks as follows:

import org.specs2.mutable.Specification
import org.junit.runner.RunWith
import org.junit.experimental.categories.Category
import util.categories.NightlyTestCategory
import org.specs2.runner.JUnitRunner
import org.junit.Test
import org.junit.runners.JUnit4

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

  "My Test" should {
    "succeed" in {
      done
    }
  }

}

Note that the above test uses a custom runner JUnitRunner (from Specs2) to execute the test. I use Maven 3 to maintain the project and have configured surefire to only include tests marked with group NightlyTestCategory:

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.15</version>
    <configuration>
    ...
       <groups>util.categories.NightlyTestCategory</groups>

    </configuration>
  </plugin>

When I execute the tests, I expect Maven to not run the above test as it is not annotated with Category NightlyTestCategory. However, Maven does execute the test. The strange thing is that if I turn the test into a regular JUnit test that is executed with JUnit4 runner, Maven will execute this test only if I add Category NightlyTestCategory:

@RunWith(classOf[JUnit4])
@Category(Array(classOf[NightlyTestCategory])) 
class MyTest extends Specification {

  @Test
  def test = println("Test")

}    

For me it seems like using the custom Specs2 runner JUnitRunner somehow influences surefire to ignore the groups setting and simply run all the tests it finds. I am looking for a solution that allows me to run the specs2 test with JUnitRunner but at the same time the category should be respected.

Was it helpful?

Solution

specs2 indeed doesn't know about Junit categories yet. In a meantime a workaround is to use tags:

import org.specs2.specification.Tags

@RunWith(classOf[JUnitRunner])
class MyTest extends Specification with Tags { section("NightlyTest")

  "My Test" should {
    "succeed" in {
      done
    }
  }
}

Then if you add, as system argument, -Dspecs2.include=NightlyTest only this specification will be executed. However this means that you cannot use the "groups" configuration from Surefire and you will have to use Maven profiles.

Another option is to use all of JUnit/Maven infrastructure and reuse specs2 matchers only:

import org.specs2.matcher._

@RunWith(classOf[JUnit4])
@Category(Array(classOf[NightlyTestCategory]))
class MyTest extends JUnitMatchers {

  @Test
  def test = { 1 must_== 1 }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top