我正在尝试测试吃掉并处理DataFrames的类的行为。
遵循以下先前的问题:How to write unit tests in Spark 2.0+?我尝试通过以下方式使用借阅模式来运行测试: 我有一个SparkSession提供程序特征:
/**
* This trait allows to use spark in Unit tests
* https://stackoverflow.com/questions/43729262/how-to-write-unit-tests-in-spark-2-0
*/
trait SparkSetup {
def withSparkSession(testMethod: SparkSession => Any) {
val conf = new SparkConf()
.setMaster("local")
.setAppName("Spark test")
val sparkSession = SparkSession
.builder()
.config(conf)
.enableHiveSupport()
.getOrCreate()
try {
testMethod(sparkSession)
}
// finally sparkSession.stop()
}
}
我在测试班级中使用过:
class InnerNormalizationStrategySpec
extends WordSpec
with Matchers
with BeforeAndAfterAll
with SparkSetup {
...
"A correct contact message" should {
"be normalized without errors" in withSparkSession{ ss => {
import ss.implicits._
val df = ss.createDataFrame(
ss.sparkContext.parallelize(Seq[Row](Row(validContact))),
StructType(List(StructField("value", StringType, nullable = false))))
val result = target.innerTransform(df)
val collectedResult: Array[NormalizedContactHistoryMessage] = result
.where(result.col("contact").isNotNull)
.as[NormalizedContactHistoryMessage]
.collect()
collectedResult.isEmpty should be(false) // There should be something
collectedResult.length should be(1) // There should be exactly 1 message...
collectedResult.head.contact.isDefined should be(true) // ... of type contact.
}}
}
...
}
尝试使用IntelliJ工具运行我的测试时,以这种方式编写的所有测试都可以工作(一次运行Spec类),但是,从终端执行sbt test
命令会使所有测试失败。
我还以为是因为并行性,所以我加了
concurrentRestrictions in Global += Tags.limit(Tags.Test, 1)
在我的sbt设置中,但是没有用。
这是我收到的堆栈跟踪:https://pastebin.com/LNTd3KGW
有帮助吗?
谢谢