2017-05-27 6 views
0

withFixtureメソッドを使用してvar ip2GeoTestJsonを初期化し、テスト中に使用しようとしています。 var yearで希望のロジックを達成することができました。私はwithFixtureが私のip2GeoTestJsonをJSONで初期化していないので、私が得ているエラー(JNothingの解析)が原因であると信じています。スカートを使用してjsonデータを読み込み、初期化する

私は現在、このエラーを取得しています:

*** 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)初期化または解析に問題があります。スケートでは何もありません。 3)サンプルからは、varsを定義することは役に立たないようです。特にメソッドスコープの外です。 4)再びサンプルからあなたは 'OneArgTest'を使用しません。 – Zernike

+0

@Zernike私の間違いは、NoArgTestであることを意味していました。はい、初期化中に問題が発生しています。 2番目の例では、メソッドの外にあるJSONファイル解析ロジックをすべて移動しても機能しますが、ファイルをハードコードする必要があります。私は 'configMap'からファイル名を取得し、それを初期化したいと思いますが、どうすればそれを動作させるのかわかりません – Liondancer

+0

あなたの例では、変数の初期化を分割する必要はありません。特性本体(1回のみ初期化する必要がある場合)またはメソッド本体(他の方法)で 'val'を設定してください。 – Zernike

答えて

1

すべてのテストの前にパラメータを設定しますE http://www.scalatest.org/user_guide/sharing_fixtures#withFixtureOneArgTest):すべてのテストは(http://www.scalatest.org/user_guide/sharing_fixtures#beforeAndAfterを実行する前に一度だけ

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):それはあなたを思わ

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) 
    } 
    } 
} 
関連する問題