Overview

The @DisabledInAotMode annotation introduced in conjunction with #30834 disables any test class during AOT processing, but it only disables JUnit Jupiter tests during AOT runtime (via a JUnit Jupiter ExecutionCondition).

If we want to provide similar support for JUnit 4 and TestNG based tests, we would need to take a different approach -- for example, by aborting tests via an exception.

  • JUnit 4 supports org.junit.AssumptionViolatedException for this purpose.

  • TestNG supports org.testng.SkipException for this purpose.

These types of exceptions could be thrown from dedicated TestExecutionListener implementations or via static utility methods that we could make available to users.

For example, the following works for JUnit 4.

public static void abortInAotMode(Class<?> testClass) {
    Assume.assumeFalse("Test class [%s] is not supported in AOT mode".formatted(testClass.getName()),
            AotDetector.useGeneratedArtifacts());
}

And the following works for TestNG.

public static void abortInAotMode(Class<?> testClass) {
    if (AotDetector.useGeneratedArtifacts()) {
        throw new SkipException("Test class [%s] is not supported in AOT mode"
                .formatted(testClass.getName()));
    }
}

A proof-of-concept implementation has been pushed to the following branch for TestNG based tests: https://github.com/spring-projects/spring-framework/compare/main...sbrannen:TestNG-abort-in-AOT-mode

Related Issues

  • 30834

  • https://github.com/junit-team/testng-engine/pull/69

Comment From: snicoll

AFAIK, @DisabledInAotMode is primarily used in relation to native and there's no support for JUnit 4 (and probably never will be). I am a bit confused about the title that looks like it's under consideration but the issue has been triaged for 6.x.

Unless I've missed something, my vote is to close this. We can reconsider if someone actually has any need for such support.

Comment From: sbrannen

AFAIK, @DisabledInAotMode is primarily used in relation to native and there's no support for JUnit 4 (and probably never will be).

spring-test has explicit AOT support for JUnit 4 and implicit AOT support for TestNG.

In addition, the GraalVM Native Build Tools (NBT) project has explicit support for JUnit 4, and TestNG is theoretically supported due to the TestNG Engine for the JUnit Platform since NBT supports any JUnit Platform TestEngine.

I am a bit confused about the title that looks like it's under consideration but the issue has been triaged for 6.x.

I've changed the title to avoid the confusion.

Unless I've missed something, my vote is to close this. We can reconsider if someone actually has any need for such support.

I agree. If the community expresses interest in these features we can revisit this topic.