# Unit tests Java Transforms supports running unit tests as part of CI checks. The Java unit tests described on this page are only applicable to batch pipelines, and are not supported for streaming pipelines. Java Transforms does not currently support interactive unit tests or debugging; if your use case requires this functionality, consider using [Python Transforms](../../transforms-python/overview/index.md) instead. The unit testing environment is set up by default when you apply the `com.palantir.transforms.lang.java-defaults` plugin to your Java transforms subproject. It adds popular Java unit testing libraries [JUnit ↗](https://junit.org/junit4/), [Mockito ↗](https://site.mockito.org/) and [AssertJ ↗](https://assertj.github.io/doc/) to the subproject’s `testCompile` configuration. You should put your test files under `src/test/java` for Gradle to automatically pick them up. See the steps in [Python Tests](../../transforms-python/unit-tests/index.md) for more details on how to add plugins to your environment. ## Testing with Spark We provide a JUnit5 extension and a JUnit4 rule to help you easily access a managed `SparkSession`. If you are using JUnit5, declare the extension like you would with any regular extension: ```java import com.palantir.transforms.lang.java.testing.api.SparkSessionExtension; import org.junit.jupiter.api.extension.RegisterExtension; @RegisterExtension public static SparkSessionExtension sparkSession = new SparkSessionExtension(); ``` If you are on JUnit4 (now deprecated), you can declare the `SparkSession` in your test files as either a `ClassRule` or a `Rule`. For example: ```java import com.palantir.transforms.lang.java.testing.api.SparkSessionResource; import org.junit.ClassRule; @ClassRule public static SparkSessionResource sparkSession = new SparkSessionResource(); ``` ```java import com.palantir.transforms.lang.java.testing.api.SparkSessionResource; import org.junit.Rule; @Rule public static SparkSessionResource sparkSession = new SparkSessionResource(); ``` In both cases, you can then use the extension or rule in your tests normally as shown in the following snippet: ```java @Test public void myUnitTest() { SparkSession session = sparkSession.get(); } ``` In JUnit4, `ClassRule` and `Rule` are different: If you declare the `SparkSession` as a `ClassRule`, the same `SparkSession` will be shared across all the tests inside the test class. This will save you some time on the recreation of `SparkSession,` but the classpath will be inherited from previously-run tests. By contrast, if you declare the `SparkSession` as a `Rule`, you will get a new isolated `SparkSession` for each individual test. This can cause significant performance degradation if you have many tests using `SparkSession` in one class. :::callout{theme="success" title="Prefer ClassRule or the JUnit5 Extension over Rule"} Declaring `SparkSession` as a `Rule` can cause significant performance degradation if you have a lot of tests using `SparkSession` in one class. We therefore strongly suggest using `ClassRule` wherever possible or using the JUnit5 `Extension` rather than the JUnit4 rule. :::