title: How to write unit tests in Spark 2.0+?
body: <p>I've been trying to find a reasonable way to test <code>SparkSession</code> with the JUnit testing framework. While there seem to be good examples for <code>SparkContext</code>, I couldn't figure out how to get a corresponding example working for <code>SparkSession</code>, even though it is used in several places internally in <a href="https://github.com/holdenk/spark-testing-base/" rel="noreferrer">spark-testing-base</a>. I'd be happy to try a solution that doesn't use spark-testing-base as well if it isn't really the right way to go here.</p>
  
  <p>Simple test case (<a href="https://github.com/bbarker/ProjectGists/tree/master/Scala/SparkSessionTester" rel="noreferrer">complete MWE project</a> with <code>build.sbt</code>):</p>
  
  <pre><code>import com.holdenkarau.spark.testing.DataFrameSuiteBase
  import org.junit.Test
  import org.scalatest.FunSuite
  
  import org.apache.spark.sql.SparkSession
  
  
  class SessionTest extends FunSuite with DataFrameSuiteBase {
  
    implicit val sparkImpl: SparkSession = spark
  
    @Test
    def simpleLookupTest {
  
      val homeDir = System.getProperty("user.home")
      val training = spark.read.format("libsvm")
        .load(s"$homeDir\\Documents\\GitHub\\sample_linear_regression_data.txt")
      println("completed simple lookup test")
    }
  
  }
  </code></pre>
  
  <p>The result of running this with JUnit is an NPE at the  load line:</p>
  
  <pre><code>java.lang.NullPointerException
      at SessionTest.simpleLookupTest(SessionTest.scala:16)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
      at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
      at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
      at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
      at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
      at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
  </code></pre>
  
  <p>Note it shouldn't matter that the file being loaded exists or not; in a properly configured SparkSession, a <a href="https://issues.apache.org/jira/browse/SPARK-20497" rel="noreferrer">more sensible error will be thrown</a>.</p>