跳转至

Unit tests(单元测试(Unit tests))

Java Transforms supports running unit tests as part of CI checks.

:::callout{theme="neutral"} Java Transforms does not currently support interactive unit tests or debugging. If your use case requires this functionality, consider using Python Transforms instead. :::

:::callout{theme="neutral"} The Java unit tests described on this page are only applicable to batch pipelines, and are not supported for streaming pipelines. :::

The unit testing environment is set up by default when you apply com.palantir.transforms.lang.java-defaults plugin to your Java transforms subproject. It adds popular Java unit testing libraries JUnit ↗, Mockito ↗ and AssertJ ↗ 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 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:

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:

import com.palantir.transforms.lang.java.testing.api.SparkSessionResource;
import org.junit.ClassRule;

@ClassRule
public static SparkSessionResource sparkSession = new SparkSessionResource();
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:

@Test
public void myUnitTest() {
    SparkSession sparkSession = sparkSession.get();
}

Note that 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="warning" title="Warning"} 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. :::


中文翻译

单元测试(Unit tests)

Java Transforms 支持在持续集成(CI)检查中运行单元测试。

:::callout{theme="neutral"} Java Transforms 目前不支持交互式单元测试或调试。如果您的用例需要此功能,请考虑使用 Python Transforms。 :::

:::callout{theme="neutral"} 本页面描述的 Java 单元测试仅适用于批处理管道(batch pipelines),不支持流式管道(streaming pipelines)。 :::

当您将 com.palantir.transforms.lang.java-defaults 插件应用于 Java transforms 子项目时,单元测试环境会默认设置。该插件会将流行的 Java 单元测试库 JUnit ↗Mockito ↗AssertJ ↗ 添加到子项目的 testCompile 配置中。 您应将测试文件放在 src/test/java 目录下,以便 Gradle 自动识别。有关如何向环境添加插件的更多详细信息,请参阅 Python Tests 中的步骤。

使用 Spark 进行测试(Testing with Spark)

我们提供 JUnit5 扩展(extension)和 JUnit4 规则(rule),帮助您轻松访问受管理的 SparkSession

如果您使用 JUnit5,请像使用任何常规扩展一样声明该扩展:

import com.palantir.transforms.lang.java.testing.api.SparkSessionExtension;
import org.junit.jupiter.api.extension.RegisterExtension;

@RegisterExtension
public static SparkSessionExtension sparkSession = new SparkSessionExtension();

如果您使用 JUnit4(现已弃用),可以在测试文件中将 SparkSession 声明为 ClassRuleRule。例如:

import com.palantir.transforms.lang.java.testing.api.SparkSessionResource;
import org.junit.ClassRule;

@ClassRule
public static SparkSessionResource sparkSession = new SparkSessionResource();
import com.palantir.transforms.lang.java.testing.api.SparkSessionResource;
import org.junit.Rule;

@Rule
public static SparkSessionResource sparkSession = new SparkSessionResource();

在这两种情况下,您都可以像以下代码片段所示,在测试中正常使用该扩展或规则:

@Test
public void myUnitTest() {
    SparkSession sparkSession = sparkSession.get();
}

请注意,在 JUnit4 中,ClassRuleRule 是不同的:如果将 SparkSession 声明为 ClassRule,则同一个 SparkSession 将在测试类中的所有测试之间共享。这将节省重新创建 SparkSession 的时间,但类路径(classpath)将继承自之前运行的测试。 相比之下,如果将 SparkSession 声明为 Rule,则每个单独的测试都会获得一个新的隔离 SparkSession。如果在一个类中有许多使用 SparkSession 的测试,这可能会导致显著的性能下降。

:::callout{theme="warning" title="警告"} 如果将 SparkSession 声明为 Rule,且一个类中有大量使用 SparkSession 的测试,可能会导致显著的性能下降。因此,我们强烈建议尽可能使用 ClassRule,或使用 JUnit5 的 Extension 而非 JUnit4 的规则。 :::