2017-01-04 4 views
1

私は目的に合わなくてはならないユニットテストを持っていますが、キャプチャできないので奇妙です。ユニットテストでNumberFormatExceptionを取得できません

これは、csvファイルどのように見えるかです:

curva;clase;divisa;rw 
AED_FXDEP;OIS;AED;240,1000 
ARS :Std;6m;ARS;240 
AUD_CALMNY_DISC;OIS;AUD;169.7056275 
AUD_DEPO_BBSW;6m;AUD;169.7056275 
AUD_DEPO_BBSW;6m;AUD; 

をそして、これはJSONスキーマファイルの内容です:

{"type" : "struct","fields" : [ {"name" : "curve","type" : "string","nullable" : false}, {"name":"class", "type":"string", "nullable":false}, {"name":"currency", "type":"string", "nullable":false}, {"name":"rw", "type":"string","nullable":false} ] 

私はそれが自己説明できる、の最後の行だと思いますcsvには空のフィールドがあり、許可されていません。空の値で数値を作成できるため、例外は明らかです(NumberFormatException)。単体テストで例外をキャッチしたいのですが、どうしてそれに手が届かないのですか?

これは、例外を引き起こしたコードは次のとおりです。

try{ 
    val validateGenericFile : Boolean = CSVtoParquet.validateGenericCSV(pathCSVWithHeaderWithErrors, 
                     pathCurvasJsonSchemaWithDecimal, 
                     _nullValue, 
                     _delimiter, 
                     sc, 
                     sqlContext) 
    //never reach! 
    Assert.assertTrue(validateGenericFile) 
} catch { 
    case e:NumberFormatException => Assert.assertTrue("ERROR! " + e.getLocalizedMessage,false) 
    case ex:Exception => Assert.assertTrue("ERROR! " + ex.getLocalizedMessage,false) 
} finally { 
    println("Done testValidateInputFilesFRTBSTDES436_WithErrors!") 
} 

validateGenericCSVがどのように見える方法:NumberFormatExceptionががvalidateGenericCSV方法で内に到達されることはありませんなぜ

val myDfWithCustomSchema = _sqlContext.read.format("com.databricks.spark.csv"). 
              option("header", "true"). 
              option("delimiter", _delimiter). 
              option("nullValue", _nullValue). 
              option("mode","FAILFAST"). 
              schema(mySchemaStructType). 
              load(fileToReview) 

var finallyCorrect : Boolean = true 

var contLinesProcessed = 1 

try{ 
    //this line provokes the exception! 
    val myArray = myDfWithCustomSchema.collect 


    var contElementosJson = 0 

    var isTheLineCorrect: Boolean = true 


    myArray.foreach { elem => 
    println("Processing line with content: " + elem) 
    for (myElem <- myList) { 
     val actualType = myElem.`type` 
     val actualName = myElem.name 
     val actualNullable = myElem.nullable 

     if (contElementosJson == myList.size) { 
     contElementosJson = 0 
     } 

     if (actualType == "string") { 
     val theField = elem.getString(contElementosJson) 
     val validatingField: Boolean = theField.isInstanceOf[String] 

     isTheLineCorrect = validatingField && !((theField == "" || theField == null) && !actualNullable) 
     contElementosJson += 1 
     if (!isTheLineCorrect){ 
      finallyCorrect=false 
      println("ATTENTION! an empty string chain. " + "Check this field " + actualName + " in the csv file, which should be a " + actualType + " according with the json schema file, can be nullable? " + actualNullable + " isTheLineCorrect? " + isTheLineCorrect) 
     } 
     } else if (actualType == "integer") { 
     val theField = elem.get(contElementosJson) 
     val validatingField: Boolean = theField.isInstanceOf[Integer] 
     isTheLineCorrect = validatingField && !((theField == "" || theField == null) && !actualNullable) 
     contElementosJson += 1 
     if (!isTheLineCorrect){ 
      finallyCorrect=false 
      println("ATTENTION! an empty string chain. " + "Check this field " + actualName + " in the csv file, which should be a " + actualType + " according with the json schema file, can be nullable? " + actualNullable + " isTheLineCorrect? " + isTheLineCorrect) 
     } 
     } else if (actualType.startsWith("decimal")) { 
     val theField = elem.get(contElementosJson) 
     val validatingField: Boolean = theField.isInstanceOf[java.math.BigDecimal] 
     isTheLineCorrect = validatingField && !((theField == "" || theField == null) && !actualNullable) 
     contElementosJson += 1 
     if (!isTheLineCorrect){ 
      finallyCorrect=false 
      println("ATTENTION! an empty string chain. " + "Check this field " + actualName + " in the csv file, which should be a " + actualType + " according with the json schema file, can be nullable? " + actualNullable + " isTheLineCorrect? " + isTheLineCorrect) 
     } 
     } else { 
     println("Attention! se está intentando procesar una columna del tipo " + actualType + " que no está prevista procesar. Comprobar.") 
     } 
    } //for 
    contLinesProcessed += 1 
    } //foreach)) 

} catch { 
    //NEVER REACHED! why???? 
    case e:NumberFormatException => throw e 
    case ex:Exception => throw ex 
} 

私はこれらの行変更したUPDATE

:これらの行のための

case e:NumberFormatException => Assert.assertTrue("ERROR! " + e.getLocalizedMessage,true) 
case ex:Exception => Assert.assertTrue("ERROR! " + ex.getLocalizedMessage,true) 

case e:NumberFormatException => Assert.assertTrue("ERROR! " + e.getLocalizedMessage,false) 
case ex:Exception => Assert.assertTrue("ERROR! " + ex.getLocalizedMessage,false) 

同じエラーが、私の問題は、ときに私がキャッチ文に到達できないということです例外が発生します!

Assert.assertTrue(...,true)が失敗しないため、テストは失敗しません

+1

例外のスタックトレースを表示できますか? – maasg

+0

こんにちは@maasg、http:// pastebin。com/X2KFZiJ5 – aironman

+0

btw、 'val myArray = myDfWithCustomSchema.collect'を実行すると、すべてのデータがドライバに渡されます。これは、Sparkドライバのメモリに収まるデータファイルに対してのみ機能します。単体テストの文脈では問題ありませんが、一般的なアプローチとしては使用しないでください。 – maasg

答えて

1

スタックトレースを検査するとき、我々は以下を参照してくださいすることができます

ERRORは!ジョブによるステージの失敗に中止:(ローカルホスト、TID 2)段階1.0でロストタスク1.0:タスク1段階1.0においては1回、最新の失敗を失敗java.lang.NumberFormatException

スパーク分散コンピューティングフレームワークであります。 NumberFormatExceptionは、タスクを処理している間にエグゼキュータの1人でリモートのをリモートで実行しています。 SparkはTaskFailureをそのエグゼキュータから取得し、org.apache.spark.SparkExceptionにラップされた例外を、この具体的なケースでは.collect()メソッドの計算をトリガするアクションに伝播します。

エラーの原因を知りたい場合は、ex.getCauseを使用してください。 実際には、次のようなスニペットが表示されます。

catch { 
    case ex:Exception if ex.getCause.getClass == classOf[NumberFormatException] => Assert.fail("Number parsing failed" + e.getLocalizedMessage) 
    case ex:Exception => Assert.fail(...) 
} 
+0

ハム、@maasgの答えをありがとう、そう、私は例外を発生することはできません? – aironman

+0

@aironman私は失敗の理由を認識することを意図していると思った。これは、示されているように「原因」を調べることによって行うことができます。 – maasg

+0

申し訳ありませんが、私の貧しい私の英語は、例外を検出し、この呼び出し元が処理するために呼び出し元に立ち上げることです例外はある。私はそれを捨ててキャッチすることを望みます。 ケースex:ex.getCause.getClass == classOf [NumberFormatException] => throw ex ex case ex:Exception => Assert.fail(...) } – aironman

0

ありがとうございます。第2パラメータがfalseで、それがtrueでない場合はassertTrueが失敗します。

+0

こんにちは@サイモン、問題は、私が思うに、例外が投げられることはないので、私は決して断言できません。 – aironman

関連する問題