【发布时间】:2019-04-19 17:44:22
【问题描述】:
我正在尝试在 Scala 中使用 PowerMockito 来绕过对 scala.io.Source.fromURL 的调用,这是 Source 类中的静态方法。我已经非常接近让它工作了,但我不断收到NotAMockException。
import com.sun.xml.internal.messaging.saaj.util.ByteInputStream
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito
import org.powermock.api.mockito.PowerMockito
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
import scala.io.BufferedSource
import scala.io.Source
@RunWith(classOf[PowerMockRunner])
@PrepareForTest(Array(classOf[Source]))
class UtilsTest {
@Test def someTest() {
PowerMockito.mockStatic(classOf[Source])
val a = "hello"
Mockito.doReturn(
new BufferedSource(
new ByteInputStream(a.getBytes, 6), 6))
.when(Source).fromURL(anyString)
}
}
我在when 行收到以下错误:
org.mockito.exceptions.misusing.NotAMockException:
Argument passed to when() is not a mock!
Example of correct stubbing:
doThrow(new RuntimeException()).when(mock).someMethod();
知道我做错了什么吗?我正确地模拟了班级,不是吗?
[编辑]
我的 build.sbt(显然是瘦身):
import _root_.sbtassembly.AssemblyPlugin.autoImport._
name := "yahoo-cml"
organization := "com.yahoo"
version := "0.1.0"
scalaVersion := "2.11.8"
lazy val yahooNexusRepo = "Nexus" at "http://pp-nexus.office.yahoo.com/content/groups/public"
lazy val cpArchiva = "cpArchiva" at "http://cp-archiva.office.yahoo.com"
lazy val yahooRepo = "NexusXP" at
"http://pp-nexus.office.yahoo.com/content/repositories/yahoo-releases"
fullResolvers ++= Seq(
yahooNexusRepo,
cpArchiva,
yahooRepo
)
libraryDependencies ++= Seq(
// scalatest is exlcuded due to conflict with testing libraries.
"org.apache.spark" %% "spark-core" % "2.1.0" % "provided"
exclude("org.scalatest", "scalatest_2.11"),
"org.apache.spark" %% "spark-sql" % "2.1.0" % "provided",
"org.apache.spark" % "spark-hive_2.11" % "2.1.0" % "provided",
"org.apache.spark" % "spark-mllib_2.11" % "2.1.0" % "provided",
// Command line parser
"com.frugalmechanic" %% "scala-optparse" % "1.1.1",
// SBT Junit is supported through junit-interface
"com.novocode" % "junit-interface" % "0.11" % "test",
// scalacheck added explicitely to avoid below error due to higher version of scalacheck.
// "org.scalacheck" % "scalacheck_2.11" % "1.12.5" % "test",
// ScalaTest is test framework for scala
// "org.scalatest" % "scalatest_2.11" % "3.0.1" % "test",
// Added as this is optional dependency for generating HTML reports in scalatest.
"org.pegdown" % "pegdown" % "1.6.0" % "test",
// spark test framework.
"com.holdenkarau" % "spark-testing-base_2.11" % "2.1.0_0.6.0" % "test",
// mockito
"org.powermock" % "powermock-api-mockito" % "1.7.4" % Test,
"org.powermock" % "powermock-core" % "1.7.4" % Test,
"org.powermock" % "powermock-module-junit4" % "1.7.4" % Test,
"org.mockito" % "mockito-core" % "1.10.19" % Test,
"com.novocode" % "junit-interface" % "0.11" % Test,
// fastUtil efficient type specific collection library
"it.unimi.dsi" % "fastutil" % "8.2.1"
)
// Create a default Scala style task to run with compile
(scalastyleConfig in Compile) := baseDirectory.value / "scalastyle-config.xml"
lazy val compileScalastyle = taskKey[Unit]("compileScalastyle")
compileScalastyle := org.scalastyle.sbt.ScalastylePlugin.scalastyle.in(Compile).toTask("").value
(compile in Compile) <<= (compile in Compile) dependsOn compileScalastyle
// Create a default Scala style task to run with tests
lazy val testScalastyle = taskKey[Unit]("testScalastyle")
testScalastyle := org.scalastyle.sbt.ScalastylePlugin.scalastyle.in(Test).toTask("").value
(test in Test) <<= (test in Test) dependsOn testScalastyle
// Configure Java source code style checking plugin.
checkstyleConfigLocation := CheckstyleConfigLocation.File("checkstyle-config.xml")
checkstyleSeverityLevel := Some(CheckstyleSeverityLevel.Error)
(checkstyle in Compile) <<= (checkstyle in Compile) triggeredBy (compile in Compile)
(checkstyle in Test) <<= (checkstyle in Test) triggeredBy (compile in Test)
// Specifying scalatest style traits for uniformity.
// All scala test classes should extend this style ONLY.
testOptions in Test += Tests.Argument(TestFrameworks.ScalaTest, "-y", "org.scalatest.PropSpec")
testOptions in Test += Tests.Argument(TestFrameworks.JUnit, "-v")
testOptions in Test ++= Seq(Tests.Argument(TestFrameworks.ScalaTest, "-o"),
Tests.Argument(TestFrameworks.ScalaTest, "-h", "target/scala-2.11/scalatest-reports"))
testOptions in Test += Tests.Argument("-oD") // test execution time recording.
parallelExecution in Test := false
fork in Test := true
javaOptions ++= Seq("-Xms512M", "-Xmx2048M",
"-XX:MaxPermSize=2048M", "-XX:+CMSClassUnloadingEnabled")
// Coverage settings
// Coverage is disabled by default to avoid Scoverage's runtime dependency in application jar.
// Enable coverage explicitly when needed. like,
// sbt coverage clean test coverageReport
// Do not use coverage when publishing jar
// coverageEnabled := true
coverageMinimum := 70
coverageFailOnMinimum := false
coverageHighlighting := true
// Do not include scala librabries in assembly jar.
assemblyOption in assembly :=
(assemblyOption in assembly).value.copy(includeScala = false)
// assembly name here should match to same generated by publishLocal.
assemblyJarName := "cml_" + scalaBinaryVersion.value + "-assembly.jar"
// addArtifact(artifact in (Compile, assembly), assembly)
// https://books.sonatype.com/nexus-book/reference/sbt.html
// SBT publish settings.
credentials += Credentials("Sonatype Nexus Repository Manager",
"pp-nexus.office.yahoo.com",
"yahooUser", "password")
publishTo <<= version { v: String =>
val nexus = "http://pp-nexus.office.yahoo.com/"
if (v.trim.endsWith("SNAPSHOT")) {
Some("snapshots" at nexus + "content/repositories/yahoo-snapshots/")
} else {
Some("releases" at nexus + "content/repositories/yahoo-releases/")
}
}
【问题讨论】:
-
如果我是你,我会开始寻找不涉及模拟伴随对象的解决方案,因为它似乎不受支持。我快速搜索了你,我找不到确凿的证据,但请看:stackoverflow.com/questions/43678415/… + stackoverflow.com/questions/20364454/… 此外,它经常在其他 scala 专用模拟库中被列为限制,这一定是有原因的。
-
我正在尝试重现你的问题,但我遇到了困难,你能分享你的依赖关系吗? build.sbt 或 pom.xml ...
-
@fonkap 当然,我会用 sbt 配置更新问题。非常感谢您对此进行调查!我完全被难住了:/
标签: scala unit-testing mockito static-methods powermockito