我正在尝试使用withFixture
方法初始化我的var ip2GeoTestJson
并在整个测试过程中使用它。我能够用var year
实现所需的逻辑。我相信我得到的错误(解析JNothing
)是因为withFixture
没有使用JSON初始化我的ip2GeoTestJson
。
我目前收到此错误:
*** RUN ABORTED ***
An exception or error caused a run to abort: java.lang.ClassCastException was thrown scenario("event.client_ip_address and event_header.client_ip_address both have values") -, construction cannot continue: "org.json4s.JsonAST$JNothing$ cannot be cast to org.json4s.JsonAST$JObject" (IP2GeoTestSuite.scala:51)
代码:
class IP2GeoTestSuite extends FeatureSpec with SparkContextFixture {
var ip2GeoTestJson: JValue = null
var year: String = null
feature("feature") {
scenario("scenario") {
println(ip2GeoTestJson)
assert(year != null)
assert(ip2GeoTestJson != null)
}
}
def withFixture(test: NoArgTest): org.scalatest.Outcome = {
year = test.configMap("year").asInstanceOf[String]
val ip2GeoConfigFile = test.configMap("config").asInstanceOf[String]
val ip2GeoUrl = getClass.getResourceAsStream(s"/$ip2GeoConfigFile")
val ip2GeoJsonString = Source.fromInputStream(ip2GeoUrl).getLines.mkString("")
System.out.println(ip2GeoJsonString)
ip2GeoTestJson = parse(ip2GeoJsonString)
try {
test()
}
}
}
当关于ip2GeoData
的行被移动到类的顶部时,代码工作正常,但是我需要对文件名进行硬编码:
class IP2GeoTestSuite extends FeatureSpec with SparkContextFixture {
val ip2GeoConfigFile = "ip2geofile.json"
val ip2GeoUrl = getClass.getResourceAsStream(s"/$ip2GeoConfigFile")
val ip2GeoJsonString = Source.fromInputStream(ip2GeoUrl).getLines.mkString("")
System.out.println(ip2GeoJsonString)
val ip2GeoTestJson = parse(ip2GeoJsonString)
var year: String = null
feature("feature") {
scenario("scenario") {
println(ip2GeoTestJson)
assert(year != null)
assert(ip2GeoTestJson != null)
}
}
def withFixture(test: NoArgTest): org.scalatest.Outcome = {
year = test.configMap("year").asInstanceOf[String]
try {
test()
}
}
}
答案 0 :(得分:1)
在每次测试前设置参数(参见http://www.scalatest.org/user_guide/sharing_fixtures#withFixtureOneArgTest):
case class FixtureParams(year: String, ip2GeoTestJson: JValue)
class IP2GeoTestSuite extends FeatureSpec with SparkContextFixture {
feature("feature") {
scenario("scenario") {
println(ip2GeoTestJson)
assert(year != null)
assert(ip2GeoTestJson != null)
}
}
override def withFixture(test: OneArgTest): org.scalatest.Outcome = {
val year = test.configMap("year").asInstanceOf[String]
val ip2GeoConfigFile = test.configMap("config").asInstanceOf[String]
val ip2GeoUrl = getClass.getResourceAsStream(s"/$ip2GeoConfigFile")
val ip2GeoJsonString = Source.fromInputStream(ip2GeoUrl).getLines.mkString("")
val fixtureParam = FixtureParam(year, parseJson(ip2GeoJsonString))
try {
withFixture(test.toNoArgTest(fixtureParam))
} finally {
// Close resourses to avoid memory leak and unpredictable behaviour
ip2GeoUrl.close()
}
}
}
在任何测试运行之前只设置一次参数(http://www.scalatest.org/user_guide/sharing_fixtures#beforeAndAfter):
class IP2GeoTestSuite extends FeatureSpec with BeforeAndAfter {
var ip2GeoTestJson: JValue = null
var year: String = null
before {
// Load config manually because configMap isn't available here.
val config = ConfigFactory.load()
year = config.getString("year")
val ip2GeoConfigFile = "ip2geofile.json"
val ip2GeoUrl = getClass.getResourceAsStream(s"/$ip2GeoConfigFile")
val ip2GeoJsonString = Source.fromInputStream(ip2GeoUrl).getLines.mkString("")
ip2GeoUrl.close()
System.out.println(ip2GeoJsonString)
ip2GeoTestJson = parseJson(ip2GeoJsonString)
}
feature("feature") {
scenario("scenario") {
println(ip2GeoTestJson)
assert(year != null)
assert(ip2GeoTestJson != null)
}
}
}