Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NPE exception when forType used with Jupiter #79

Closed
luvarqpp opened this issue Dec 15, 2019 · 3 comments
Closed

NPE exception when forType used with Jupiter #79

luvarqpp opened this issue Dec 15, 2019 · 3 comments
Labels
bug

Comments

@luvarqpp
Copy link
Contributor

@luvarqpp luvarqpp commented Dec 15, 2019

Testing Problem

When using Arbitraries.forType() method, there is NPE exception, when it is used outside of jqwik test runner (i.e. in junit 5 default, jupiter, runner).

Stacktrace:

java.lang.NullPointerException
	at net.jqwik.engine.facades.ArbitrariesFacadeImpl.allDefaultsFor(ArbitrariesFacadeImpl.java:196)
	at net.jqwik.engine.facades.ArbitrariesFacadeImpl.lambda$defaultFor$3(ArbitrariesFacadeImpl.java:164)
	at net.jqwik.engine.properties.arbitraries.LazyArbitrary.generator(LazyArbitrary.java:18)
	at net.jqwik.api.Combinators$ListCombinator$1.lambda$generator$0(Combinators.java:697)
	at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
	at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1654)
	at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
	at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
	at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
	at net.jqwik.api.Combinators$ListCombinator$1.generator(Combinators.java:698)
	at net.jqwik.engine.properties.arbitraries.IgnoreGenerationErrorArbitrary.generator(IgnoreGenerationErrorArbitrary.java:18)
	at net.jqwik.engine.properties.shrinking.FlatMappedShrinkable.lambda$new$0(FlatMappedShrinkable.java:17)
	at net.jqwik.engine.properties.shrinking.FlatMappedShrinkable.generateShrinkable(FlatMappedShrinkable.java:29)
	at net.jqwik.engine.properties.shrinking.FlatMappedShrinkable.<init>(FlatMappedShrinkable.java:24)
	at net.jqwik.engine.properties.shrinking.FlatMappedShrinkable.<init>(FlatMappedShrinkable.java:17)
	at net.jqwik.engine.facades.RandomGeneratorFacadeImpl.flatMap(RandomGeneratorFacadeImpl.java:21)
	at net.jqwik.api.RandomGenerator.lambda$flatMap$2(RandomGenerator.java:58)

Test code throwing exception is something like this:

    public static UserForTests aUserFromForType() {
        return Arbitraries.forType(UserForTests.class).usePublicConstructors()
                .generator(0).next(rnd).value();
    }

Problem is test engine. I have also one property based test, which is passing ok. Here it is:

    @Property
    @Report(Reporting.GENERATED)
    void justTestOfForTypeThing(@ForAll @UseType UserForTests aPerson) {
        System.out.println(aPerson);
    }
    @Provide
    Arbitrary<UserForTests> people() {
        return Arbitraries.forType(UserForTests.class).usePublicConstructors();
    }

Suggested Solution

I would check, if forType is called in right context (i.e. correct test engine executor) and throw some more informative exception.

Perhaps in DefaultTypeArbitrary constructor would be a nice place. Other imho valid place could be in DomainContextFacadeImpl. Change its static property called currentContext into "static getter" and check for valid value in thread local there.

Discussion

I am not aware if jqwik internal code hierarchy, so not much here from me. I would just suggest to document for jqwik engine in forType javadoc. (I can try to make pull request for this if you like).

@jlink jlink added the bug label Dec 17, 2019
@jlink

This comment has been minimized.

Copy link
Owner

@jlink jlink commented Dec 17, 2019

There could be a (somewhat) useful behaviour outside a property: Just using the global domain context, i.e. use all globally registered arbitrary providers. I might implement that...

@jlink

This comment has been minimized.

Copy link
Owner

@jlink jlink commented Jan 2, 2020

Fixed in 1.2.2-SNAPSHOT

@jlink jlink closed this Jan 2, 2020
@jlink

This comment has been minimized.

Copy link
Owner

@jlink jlink commented Jan 2, 2020

It now works outside jqwik properties using default global domain, i.e. all globally registered arbtirary providers are considered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.