You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Maven构建中,如何配置Nullaway以排除测试类?

Alright, let's solve this Nullaway issue you're facing. Those test classes in src/test/java (same package as your main code) that are designed to verify NullPointerExceptions get thrown correctly are getting flagged by Nullaway—and that's not what you want. Here's how to exclude them properly:

Option 1: Build Tool Configuration (Batch Exclusion)

This is the most efficient approach if you have multiple test classes to exclude. Nullaway runs on top of Error Prone, so we can use Error Prone's exclusion flags targeted at your test compilation tasks.

For Gradle

Add this configuration to your build.gradle (or build.gradle.kts for Kotlin DSL) to exclude test classes during their compilation:

tasks.withType(JavaCompile).configureEach {
    options.compilerArgs.add("-Xplugin:ErrorProne")
    // Target only test compilation tasks
    if (name.contains("Test")) {
        // Exclude all test classes in your target package (use wildcard for bulk exclusion)
        options.compilerArgs.add("-XepExcludedClasses:com.yourpackage.*")
        // Or target specific test classes if needed:
        // options.compilerArgs.add("-XepExcludedClasses:com.yourpackage.NPEValidationTest,com.yourpackage.AnotherNpeTest")
    }
}

dependencies {
    // Make sure you have the required dependencies
    annotationProcessor 'com.google.errorprone:error_prone_core:2.22.0'
    annotationProcessor 'com.uber.nullaway:nullaway:0.10.20'
}

The -XepExcludedClasses flag accepts full class names or wildcards (*) to match multiple classes at once.

For Maven

In your pom.xml, configure the maven-compiler-plugin to add exclusion flags specifically for the test-compile phase:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.11.0</version>
            <configuration>
                <source>17</source> <!-- Replace with your Java version -->
                <target>17</target>
                <compilerArgs>
                    <arg>-Xplugin:ErrorProne</arg>
                </compilerArgs>
                <annotationProcessorPaths>
                    <path>
                        <groupId>com.google.errorprone</groupId>
                        <artifactId>error_prone_core</artifactId>
                        <version>2.22.0</version>
                    </path>
                    <path>
                        <groupId>com.uber.nullaway</groupId>
                        <artifactId>nullaway</artifactId>
                        <version>0.10.20</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
            <executions>
                <execution>
                    <id>test-compile</id>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                    <configuration>
                        <compilerArgs>
                            <!-- Exclude test classes in your package -->
                            <arg>-XepExcludedClasses:com.yourpackage.*</arg>
                            <!-- Or list specific classes: -->
                            <!-- <arg>-XepExcludedClasses:com.yourpackage.NPEValidationTest</arg> -->
                        </compilerArgs>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

This ensures only your test classes are excluded, while Nullaway continues enforcing null safety on your main code.

Option 2: Annotation-Based Exclusion (For Individual Classes/Methods)

If you only have a handful of test classes or methods that need exclusion, use the @SuppressWarnings annotation directly:

@SuppressWarnings("NullAway")
public class NPEValidationTest {
    @Test
    public void testNullArgumentThrowsNPE() {
        assertThrows(NullPointerException.class, () -> {
            yourService.methodThatThrowsNpe(null);
        });
    }
}

You can also apply this annotation to individual test methods if only specific cases need to be skipped.

Key Notes

  • Adjust the package names and dependency versions to match your project setup.
  • Wildcards work for bulk exclusion, but be precise to avoid accidentally excluding non-test classes.
  • Build tool configuration is better for scaling, while annotations are ideal for one-off cases.

内容的提问来源于stack exchange,提问作者Elliotte Rusty Harold

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:15:38