0

According to ScalaTest documentation it's possible to use JUnitRunner to run tests. My assumption was, if it runs with JUnitRunner, its callback methods (i.e. methods marked with @Before or @After annotation should work as well. But apparently my assumption was wrong. I've created simple example to demonstrate it:

import org.junit.Before
import org.junit.runner.RunWith
import org.scalatest.{FunSuite, _}
import org.scalatest.junit.JUnitRunner

@RunWith(classOf[JUnitRunner])
class Test extends FunSuite {

  @Before
  def before() = {
    println("before test")
  }

  test("nothing") {
    println("test is started")
  }
}

If you run this example, you'll see test is started line, but not before test.

I'm aware of ScalaTest lifecycle callbacks, but the thing is I need to make JUnit callbacks work somehow. I want to write a test for Play 2.4 application and the thing it's play.test.WithBrowser class relies on JUnit callbacks. I found this workaround for the issue:

 var testBrowser : TestBrowser = _

 before {
   new WithBrowser {
     override def createBrowser(): Unit = {
       super.createBrowser()
       testBrowser = browser
     }
 }.createBrowser()

}

but I believe it's quite ugly and I suspect that there is a better way.

So, my question is if it's possible to make these JUnit lifecycle callbacks work with ScalaTest? And if it is possible, how to do that?

wax
  • 2,400
  • 3
  • 19
  • 28

2 Answers2

1

You should be able to do this using JUnitSuite instead of FunSuite. The documentation is here:

http://www.scalatest.org/getting_started_with_junit_4_in_scala http://doc.scalatest.org/2.2.4/index.html#org.scalatest.junit.JUnitSuite

Noah
  • 13,821
  • 4
  • 36
  • 45
0

I did just like Noah suggested and it worked. Just in case anyone stumbles upon the same issue with Play 2.x and WithBrowser, I ended up with a test like this:

class IntegrationSpec extends WithBrowser with JUnitSuiteLike with Matchers {

  @Test def workWithBrowser() {
     browser.goTo("http://localhost:9000")
     browser.pageSource should include("Add Person")
  }
}
wax
  • 2,400
  • 3
  • 19
  • 28