1

Junit5 has introduced @BeforeAll to signal that the annotated method should be executed before all tests in the current test class. However it has requirement of method being static.

I have following structure in my Scala test suite:

class BaseTest extends MockitoSugar {

  @BeforeAll
  def codeNeedeForAllTests() = {   <== this does not work as as it is not static
    println("calling codeNeedeForAllTests")
    // other code
  }
}

and other test classes:

class IndexerTest extends BaseTest {
  @Test
  def add() = {
     //test code
  }
}

So I want that codeNeedeForAllTests get called before all tests, however the catch is @BeforeAll's requirement of method being static, for which I need to make codeNeedeForAllTests as object to have static method in Scala.

Now in Scala, a class can not extend an object and and object also can not extend object.

I also tried creating companion object of BaseTest, but that also did not work, any clean approach to do this?

Sam Brannen
  • 29,611
  • 5
  • 104
  • 136
Saurabh
  • 71,488
  • 40
  • 181
  • 244

2 Answers2

0

@BeforeAll methods in JUnit Jupiter (a.k.a., JUnit 5) do not have to be static.

The link you provided is to JUnit 4.

This link is to JUnit Jupiter.

Excerpt:

Denotes that the annotated method should be executed before all @Test, @RepeatedTest, @ParameterizedTest, and @TestFactory methods in the current class; analogous to JUnit 4’s @BeforeClass. Such methods are inherited (unless they are hidden or overridden) and must be static (unless the "per-class" test instance lifecycle is used).

And that links to the documentation on Test Instance Lifecycle.

Thus, you should be able to annotate your test class with @TestInstance(Lifecycle.PER_CLASS) and declare your @BeforeAll method as non-static.

Sam Brannen
  • 29,611
  • 5
  • 104
  • 136
  • I am not able to get this done even after putting `@TestInstance(Lifecycle.PER_CLASS)` at top of class. Not sure why not working. – Saurabh Aug 16 '18 at 18:37
  • Sounds odd. Perhaps you should post a simplified version of your project online and link to it for others to try it out. – Sam Brannen Aug 20 '18 at 09:37
0

You can do this with FunCpec.

package net

import org.scalatest._

class Test extends FunSpec with BeforeAndAfterAll {

    override def beforeAll: Unit = {
        println("beforeAll")
    }

    describe("running a test") {
        println("in describe")

        it("should do something") {
            println("in my test")
            assert(true)
        }
    }
}

If u'll test it in REPL:

scala> test
[info] Compiling 1 Scala source to /Users/kmmere/Workspace/Work/scalatest_before/target/scala-2.11/test-classes...
in describe
beforeAll
in my test
[info] Test:
[info] running a test
[info] - should do something
[info] Run completed in 99 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[success] Total time: 1 s, completed Oct 19, 2015 4:32:56 PM

Don't forget to include dependency of scalatest in yor build.sbt file:

"org.scalatest" %% "scalatest" % "3.2.0-SNAP10" % Test

class FunSpec - facilitates a “behavior-driven” style of development (BDD), in which tests are combined with text that specifies the behavior the tests verify.

Recommended Usage: For teams coming from Ruby's RSpec tool, FunSpec will feel familiar and comfortable; More generally, for any team that prefers BDD, FunSpec's nesting and gentle guide to structuring text (with describe and it) provide an excellent general-purpose choice for writing specification-style tests.

Dmitry Kaltovich
  • 2,046
  • 19
  • 21