I'm writing a test with Playframework, and I need to create a temporary file.
@RunWith(classOf[JUnitRunner])
class DiagnosticSpec extends Specification {
@Rule
val temporaryFolder: TemporaryFolder = new TemporaryFolder()
"my test" should {
"run with temporary file" in {
val file = temporaryFolder.newFile() // line.35
// go on with the file
}
}
}
But when I run this test, it always throw exception:
[error] IllegalStateException: the temporary folder has not yet been created (MyTest.scala:35)
Is it possible to use it in specs2? If not, how can I create a temporary file in specs2, and delete it automatically after testing?
You cannot use JUnit rules with specs2 to do setup/teardown. You need to use
AroundExample
orFixtureExample
for that:UPDATE
The
TemporaryFolder
trait is closer to the original JUnit rule: