2017-05-27 30 views
0

閱讀和初始化JSON數據,我試圖用withFixture方法來初始化我var ip2GeoTestJson,並用它在我的測試。我能夠通過var year實現所需的邏輯。我相信我得到(解析JNothing)錯誤是因爲withFixture未初始化我ip2GeoTestJson與JSON引起的。與scalatest withFixture

我目前正在此錯誤:

*** 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)看來你對樣品改變代碼。 2)你有初始化或解析問題。沒有什麼與scalatest。 3)從樣本來看,定義變量似乎毫無用處。特別是超出方法範圍。 4)再從樣品你不使用'OneArgTest' – Zernike

+0

@Zernike我的錯誤,它的意思是'NoArgTest'我做出了修正。是的,我在初始化過程中遇到了麻煩。在第二個示例中,我移動了該方法之外的所有JSON文件解析邏輯,並且它工作正常,但我必須對該文件進行硬編碼。我想抓住從'configMap'文件名和初始化,但我不知道如何得到它的工作 – Liondancer

+0

到您的例子有沒有必要「分流」變量初始化。在特質體中設置'val'(如果它應該初始化一次)或者在方法體中(其他方式)。 – Zernike

回答

1

在每次測試之前設置參數(se Ë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() 
    } 
    } 
} 

設置PARAMS只有一次前的任何測試將運行(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) 
    } 
    } 
}