2017-05-27 1 views
0

Ich versuche withFixture Methode zu verwenden, um meine var ip2GeoTestJson zu initialisieren und sie während meiner Tests zu verwenden. Ich konnte die gewünschte Logik mit var year erreichen. Ich glaube, der Fehler, den ich bekomme (Parsing JNothing) wird verursacht, weil die withFixture nicht initialisiert meine ip2GeoTestJson mit dem JSON.Lesen und Initialisieren von Json-Daten mit Scalatest mit Fixture

Ich bin derzeit immer diese Fehlermeldung:

*** 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) 

Code:

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() 
    } 
    } 
} 

Der Code funktioniert gut, wenn die Linien in Bezug auf ip2GeoData an die Spitze der Klasse bewegt werden wie so aber ich muss hardcode der Dateiname:

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) Sie scheint für eine Probe geändert Code. 2) Sie haben ein Problem mit der Initialisierung oder dem Parsen. Nichts mit Skalata. 3) Aus Beispiel scheint es sinnlos zu sein, vars zu definieren. Besonders außerhalb des Methodenbereichs. 4) Erneut von der Probe benutzt du nicht 'OneArgTest' – Zernike

+0

@Zernike Mein Fehler, es sollte' NoArgTest' sein Ich machte die Korrektur. Ja, ich habe Probleme während der Initialisierung. Im zweiten Beispiel habe ich die JSON-Datei-Parsing-Logik außerhalb der Methode verschoben, und es funktioniert, aber ich muss die Datei fest codieren. Ich möchte den Dateinamen von 'configMap' abgreifen und ihn initialisieren, aber ich bin mir nicht sicher, wie ich ihn zum Laufen kriege. – Liondancer

+0

Bis zu Ihrem Beispiel müssen Sie die Variableninitialisierung nicht" aufteilen ". Setzen Sie 'val' entweder im Merkmals-Body (wenn es nur einmal initialisiert werden soll) oder im Methoden-Body (anders). – Zernike

Antwort

1

Parameter vor jedem Test einstellen (se e 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() 
    } 
    } 
} 

Set params nur einmal vor jeder Prüfung (http://www.scalatest.org/user_guide/sharing_fixtures#beforeAndAfter) ausgeführt wird:

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) 
    } 
    } 
} 
Verwandte Themen