Skip to content

Make nested groups work for JDK >= 18 #528

Description

@duponter

Testing Problem

We are currently looking to upgrade our application from Java 17 to Java 21.
While our application starts up without issues on Java 21, our unit tests do not complete successfully.
More specifically, most (not all) of our jqwik unit tests fail.

Reproduction

I could, however, reproduce the issue with a simple test case:

class JqwikJdk21Test {
    @Group
    class InGroup {

        @Property
        void runsWithAnnotations(@ForAll @NumericChars @StringLength(9) String input) {
            assertThat(input).hasSize(9).containsOnlyDigits();
        }


        @Property
        void runsWithProvideFromOuterClass(@ForAll("numericStrings") String input) {
            assertThat(input).hasSize(9).containsOnlyDigits();
        }
    }

    @Property
    void runsWithProvideFromSameClass(@ForAll("numericStrings") String input) {
        assertThat(input).hasSize(9).containsOnlyDigits();
    }

    @Provide
    static Arbitrary<String> numericStrings() {
        return Arbitraries.strings().numeric().ofLength(9);
    }
}

I am using following versions:

  • openjdk 21.0.1 2023-10-17 LTS
  • org.junit.jupiter:junit-jupiter-api:5.10.0
  • org.junit.jupiter:junit-jupiter-engine:5.10.0
  • net.jqwik:jqwik:1.8.1
  • net.jqwik:jqwik-engine:1.8.1
  • org.assertj:assertj-core:3.24.2

When running this test, runsWithProvideFromOuterClass fails. However, the same test runsWithProvideFromSameClass succeeds.
Additionally, when reverting to Java 17, all tests succeed.

Following stacktrace is printed for runsWithProvideFromOuterClass ran with Java 21.

java.lang.IllegalArgumentException: Method [static net.jqwik.api.Arbitrary org.jqwik.jdk21.JqwikJdk21Test.numericStrings()] cannot be invoked on target [org.jqwik.jdk21.JqwikJdk21Test$InGroup@57a3e26a].

	at net.jqwik.engine.support.JqwikReflectionSupport.invokeMethodPotentiallyOuter(JqwikReflectionSupport.java:236)
	at net.jqwik.engine.properties.ProviderMethod.invokeProviderMethod(ProviderMethod.java:58)
	at net.jqwik.engine.properties.ProviderMethod.lambda$arbitrarySuppliers$1(ProviderMethod.java:67)
	at net.jqwik.engine.properties.ProviderMethod.lambda$invoke$0(ProviderMethod.java:54)
	at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
	at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133)
	at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1939)
	at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
	at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:921)
	at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682)
	at net.jqwik.engine.properties.ProviderMethod.mapSet(ProviderMethod.java:112)
	at net.jqwik.engine.properties.ProviderMethod.invoke(ProviderMethod.java:54)
	at net.jqwik.engine.properties.InstanceBasedSubtypeProvider.lambda$resolveFromGeneratorName$7(InstanceBasedSubtypeProvider.java:108)
	at java.base/java.util.Optional.map(Optional.java:260)
	at net.jqwik.engine.properties.InstanceBasedSubtypeProvider.resolveFromGeneratorName(InstanceBasedSubtypeProvider.java:108)
	at net.jqwik.engine.properties.InstanceBasedSubtypeProvider.apply(InstanceBasedSubtypeProvider.java:66)
	at net.jqwik.engine.properties.PropertyMethodArbitraryResolver.apply(PropertyMethodArbitraryResolver.java:11)
	at net.jqwik.engine.properties.PropertyMethodArbitraryResolver.forParameter(PropertyMethodArbitraryResolver.java:37)
	at net.jqwik.engine.execution.CachingArbitraryResolver.lambda$forParameter$0(CachingArbitraryResolver.java:25)
	at java.base/java.util.HashMap.computeIfAbsent(HashMap.java:1228)
	at net.jqwik.engine.execution.CachingArbitraryResolver.forParameter(CachingArbitraryResolver.java:23)
	at net.jqwik.engine.properties.ExhaustiveShrinkablesGenerator.resolveParameter(ExhaustiveShrinkablesGenerator.java:31)
	at net.jqwik.engine.properties.ExhaustiveShrinkablesGenerator.lambda$forParameters$0(ExhaustiveShrinkablesGenerator.java:19)
	at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
	at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1708)
	at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
	at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:921)
	at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682)
	at net.jqwik.engine.properties.ExhaustiveShrinkablesGenerator.forParameters(ExhaustiveShrinkablesGenerator.java:20)
	at net.jqwik.engine.execution.CheckedProperty.createOptionalExhaustiveShrinkablesGenerator(CheckedProperty.java:218)
	at net.jqwik.engine.execution.CheckedProperty.getOptionalExhaustive(CheckedProperty.java:251)
	at net.jqwik.engine.execution.CheckedProperty.chooseGenerationMode(CheckedProperty.java:204)
	at net.jqwik.engine.execution.CheckedProperty.createGenericProperty(CheckedProperty.java:141)
	at net.jqwik.engine.execution.CheckedProperty.check(CheckedProperty.java:67)
	at net.jqwik.engine.execution.PropertyMethodExecutor.executeProperty(PropertyMethodExecutor.java:90)
	at net.jqwik.engine.execution.PropertyMethodExecutor.executeMethod(PropertyMethodExecutor.java:69)
	at net.jqwik.engine.execution.PropertyMethodExecutor.lambda$execute$0(PropertyMethodExecutor.java:49)
	at net.jqwik.api.lifecycle.AroundPropertyHook.lambda$static$0(AroundPropertyHook.java:46)
	at net.jqwik.engine.execution.lifecycle.HookSupport.lambda$wrap$0(HookSupport.java:26)
	at net.jqwik.api.lifecycle.PropertyExecutor.executeAndFinally(PropertyExecutor.java:39)
	at net.jqwik.engine.hooks.lifecycle.PropertyLifecycleMethodsHook.aroundProperty(PropertyLifecycleMethodsHook.java:56)
	at net.jqwik.engine.execution.lifecycle.HookSupport.lambda$wrap$1(HookSupport.java:31)
	at net.jqwik.engine.execution.lifecycle.HookSupport.lambda$wrap$0(HookSupport.java:26)
	at net.jqwik.engine.hooks.statistics.StatisticsHook.aroundProperty(StatisticsHook.java:37)
	at net.jqwik.engine.execution.lifecycle.HookSupport.lambda$wrap$1(HookSupport.java:31)
	at net.jqwik.engine.execution.lifecycle.HookSupport.lambda$wrap$0(HookSupport.java:26)
	at net.jqwik.engine.hooks.lifecycle.AutoCloseableHook.aroundProperty(AutoCloseableHook.java:13)
	at net.jqwik.engine.execution.lifecycle.HookSupport.lambda$wrap$1(HookSupport.java:31)
	at net.jqwik.engine.execution.PropertyMethodExecutor.execute(PropertyMethodExecutor.java:47)
	at net.jqwik.engine.execution.PropertyTaskCreator.executeTestMethod(PropertyTaskCreator.java:166)
	at net.jqwik.engine.execution.PropertyTaskCreator.lambda$createTask$1(PropertyTaskCreator.java:51)
	at net.jqwik.engine.execution.lifecycle.CurrentDomainContext.runWithContext(CurrentDomainContext.java:28)
	at net.jqwik.engine.execution.PropertyTaskCreator.lambda$createTask$2(PropertyTaskCreator.java:50)
	at net.jqwik.engine.execution.pipeline.ExecutionTask$1.lambda$execute$0(ExecutionTask.java:31)
	at net.jqwik.engine.execution.lifecycle.CurrentTestDescriptor.runWithDescriptor(CurrentTestDescriptor.java:17)
	at net.jqwik.engine.execution.pipeline.ExecutionTask$1.execute(ExecutionTask.java:31)
	at net.jqwik.engine.execution.pipeline.ExecutionPipeline.runToTermination(ExecutionPipeline.java:82)
	at net.jqwik.engine.execution.JqwikExecutor.execute(JqwikExecutor.java:46)
	at net.jqwik.engine.JqwikTestEngine.executeTests(JqwikTestEngine.java:70)
	at net.jqwik.engine.JqwikTestEngine.execute(JqwikTestEngine.java:53)

Possible solution

When a property test requires a Provide method from outside its own class, at some point JqwikReflectionSupport#getOuterInstance is called. This method calls the deprecated private method makeAccessible, but swallows any exceptions, returning an empty Optional instead.
Probably the stacktrace of the swallowed exception will give a clue to fix this issue.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions