我写了一个简单的Scala代码来练习Scala测试:
object Job {
def main(args: Array[String]) = {
val sc = new SparkContext()
println(reduceWithSum(sc))
}
def reduceWithSum(sc: SparkContext): MyClass = {
val data = Array(1, 2, 3, 4, 5)
val distData = sc.parallelize(data)
val distDataValue = distData.map(MyClass(_))
distDataValue.reduce(MyClass.sum)
}
}
Run Code Online (Sandbox Code Playgroud)
我知道为reduceWithSum()编写测试代码很容易,但为main()编写测试代码对我来说似乎很难。有什么提示吗?
这是我编写的示例测试代码:
class JobTest extends FlatSpec with Matchers {
val conf = new SparkConf().setMaster("local[*]").setAppName("Test")
val testSc = new SparkContext(conf)
it should "reduce correctly" in {
Job.reduceWithSum(testSc) shouldBe MyClass(15)
}
Run Code Online (Sandbox Code Playgroud)