PlayFramework2.1.3(Java) テスト0件問題を回避

play2.1.3(java)で UnitTestができない問題。

とのことですた。

MLからパッチをいただく

Play 2.1.3 released - groups.google.com play-framework より

testOptions in Test ~= { args =>
  for {
    arg <- args
    val ta: Tests.Argument = arg.asInstanceOf[Tests.Argument]
    val newArg = if(ta.framework == Some(TestFrameworks.JUnit)) ta.copy(args = List.empty[String]) else ta
  } yield newArg
}

パッチあて

どこにあてんの?と思ったら project/Build.scala でした。

パッチをあてた samples/java/computer-database-jpa の project/Build.scala

import sbt._
import Keys._
import play.Project._

object ApplicationBuild extends Build {

    val appName         = "computer-database-jpa"
    val appVersion      = "1.0"

    val appDependencies = Seq(
      javaCore,
      javaJdbc,
      javaJpa,
      "org.hibernate" % "hibernate-entitymanager" % "3.6.9.Final"
    )

    val main = play.Project(appName, appVersion, appDependencies).settings(
      ebeanEnabled := false,

      // PATCH : JUnit tests in Play do not execute
      testOptions in Test ~= { args =>
        for {
          arg <- args
          val ta: Tests.Argument = arg.asInstanceOf[Tests.Argument]
          val newArg = if(ta.framework == Some(TestFrameworks.JUnit)) ta.copy(args = List.empty[String]) else ta
        } yield newArg
      }
    )
}

テスト結果

[computer-database-jpa] $ test
[info] play - datasource [jdbc:h2:mem:play-test--899905098;] bound to JNDI as DefaultDS
[info] IntegrationTest
[info] + IntegrationTest.test
[info]
[info]
[info] Total for test IntegrationTest
[info] Finished in 0.018 seconds
[info] 1 tests, 0 failures, 0 errors
[info] play - datasource [jdbc:h2:mem:play] bound to JNDI as DefaultDS
[info] play - datasource [jdbc:h2:mem:play] bound to JNDI as DefaultDS
[info] play - datasource [jdbc:h2:mem:play] bound to JNDI as DefaultDS
[info] play - datasource [jdbc:h2:mem:play] bound to JNDI as DefaultDS
[info] FunctionalTest
[info] + FunctionalTest.filterComputerByName
[info] + FunctionalTest.redirectHomePage
[info] + FunctionalTest.createANewComputer
[info] + FunctionalTest.listComputersOnTheFirstPage
[info]
[info]
[info] Total for test FunctionalTest
[info] Finished in 0.01 seconds
[info] 4 tests, 0 failures, 0 errors
[info] play - datasource [jdbc:h2:mem:play] bound to JNDI as DefaultDS
[info] play - datasource [jdbc:h2:mem:play-test--1376794684;] bound to JNDI as DefaultDS
[info] ModelTest
[info] + ModelTest.findById
[info] + ModelTest.pagination
[info]
[info]
[info] Total for test ModelTest
[info] Finished in 0.004 seconds
[info] 2 tests, 0 failures, 0 errors
[info] Passed: : Total 7, Failed 0, Errors 0, Passed 7, Skipped 0
[success] Total time: 12 s, completed 2013/08/15 21:17:30

無事テストできますた。

参考

Play 2.1.3 released - groups.google.com play-framework

SBT セッティングについて - playframework-ja.org
ここの「デフォルト値の Play プロジェクトセッティング」を読んでおくといいかもしれない。scalaを読めるようになっておくといいかもしれない。最近書いてないのでまた読めなくなってきた。