PowerMock 没有 ScalaTest 支持,所以我尝试使用 JUnit 来运行我的 ScalaTest 类,使用@RunWith[classOf[JUnitRunner]]
. 使用 Maven,您可以将 javaagent 模块配置为急切加载以支持此功能,如PowerMock wiki 中所述。我需要在 SBT 中做什么才能获得相同的结果?
name := "my-project"
version := "1.0"
scalaVersion := "2.10.2"
libraryDependencies ++= Seq(
// ... other dependencies above ...
"org.powermock" % "powermock-api-mockito" % "1.5.1" % "test",
"org.powermock" % "powermock-module-javaagent" % "1.5.1" % "test",
"org.powermock" % "powermock-module-junit4-rule-agent" % "1.5.1" % "test",
"com.novocode" % "junit-interface" % "0.10" % "test->default"
)
如图所示,这个项目有错误:
[error] Caused by: java.lang.IllegalStateException: PowerMockRule can only be used with the system classloader but was loaded by sbt.classpath.ClasspathUtilities$$anon$1@1cee593
[error] at org.powermock.modules.junit4.rule.PowerMockRule.<clinit>(PowerMockRule.java:28)
[error] ... 55 more
看起来 SBT 有自己的类加载器来运行与 PowerMock 需要做的冲突的测试。