I wrote a simple Scala code to practice Scala Test:
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)
}
}
I know it is easy to write a test code for reduceWithSum() but it seems very hard for me to write a test code for main(). Any hint?
Here is a sample testing code I wrote:
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)
}
The main
method returns Unit
(its signature is def main(args: Array[String]): Unit
), so it's kind of weird to test what's "returned" by this method.
Here, you're just doing a println
, so if you really want to check that the result is printed, you should follow the link that @JoelBerkeley gave in his comment : Scalatest - how to test println. However, just testing directly printed results is not a good practice : you should test what's returned by your method, but not just the fact that the result is printed rightly.
In most of cases, you do not need to test the main
method because it's just the entrypoint to your program. IMHO, the main
method should just create an instance of a class (SparkProgram
for example) making your processing, and that's it (see that answer I totally agree with). The methods in that class must be tested independently of your main
. Your main
method should be as short as possible : no business logic, just delegating work to classes with methods returning non-Unit
results that could be tested.