title: Scalatest and Spark giving "java.io.NotSerializableException: org.scalatest.Assertions$AssertionsHelper"
body: <p>I’m testing a Spark Streaming application with the help of "<strong>com.holdenkarau.spark-testing-base</strong>" and <strong>scalatest</strong>.</p>
  
  <pre><code>import com.holdenkarau.spark.testing.StreamingSuiteBase
  import org.apache.spark.rdd.RDD
  import org.scalatest.{ BeforeAndAfter, FunSuite }
  
  class Test extends FunSuite with BeforeAndAfter with StreamingSuiteBase {
  
    var delim: String = ","
  
    before {
      System.clearProperty("spark.driver.port")
     }
  
    test(“This Fails“) {
  
      val source = scala.io.Source.fromURL(getClass.getResource(“/some_logs.csv"))
      val input = source.getLines.toList
  
      val rowRDDOut = Calculator.do(sc.parallelize(input))   //Returns DataFrame
  
      val report: RDD[String] = rowRDDOut.map(row =&gt; new String(row.getAs[String](0) + delim + row.getAs[String](1))
  
      source.close
    }
  }
  </code></pre>
  
  <p>I get Serialization exception for field '<strong><em>delim</em></strong>':</p>
  
  <pre><code>org.apache.spark.SparkException: Task not serializable
  [info]   at org.apache.spark.util.ClosureCleaner$.ensureSerializable(ClosureCleaner.scala:304)
  [info]   at org.apache.spark.util.ClosureCleaner$.org$apache$spark$util$ClosureCleaner$$clean(ClosureCleaner.scala:294)
  [info]   at org.apache.spark.util.ClosureCleaner$.clean(ClosureCleaner.scala:122)
  [info]   at org.apache.spark.SparkContext.clean(SparkContext.scala:2055)
  [info]   at org.apache.spark.rdd.RDD$$anonfun$map$1.apply(RDD.scala:324)
  [info]   at org.apache.spark.rdd.RDD$$anonfun$map$1.apply(RDD.scala:323)
  [info]   at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:150)
  [info]   at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:111)
  [info]   at org.apache.spark.rdd.RDD.withScope(RDD.scala:316)
  [info]   at org.apache.spark.rdd.RDD.map(RDD.scala:323)
  [info]   ...
  [info]   Cause: java.io.NotSerializableException: org.scalatest.Assertions$AssertionsHelper
  [info] Serialization stack:
  [info]  - object not serializable (class: org.scalatest.Assertions$AssertionsHelper, value: org.scalatest.Assertions$AssertionsHelper@78b339fa)
  [info]  - field (class: org.scalatest.FunSuite, name: assertionsHelper, type: class org.scalatest.Assertions$AssertionsHelper)
  </code></pre>
  
  <p>If I replace 'delim' by String value in place, it works fine.</p>
  
  <pre><code>val report: RDD[String] = rowRDDOut.map(row =&gt; new String(row.getAs[String](0) + “,” + row.getAs[String](1))
  </code></pre>
  
  <p>What’s the difference between first and second version?</p>
  
  <p>Thanks in advance!</p>