How do you use a jqwik @Provider specified in another class as part of a @ForAll paramter?

239 Views Asked by At

We have a bunch of generators specified in a class.

class MyUsefulGenerators {

    @Provide 
    public Arbitrary<String> someDomainSpecificThing() {
        ...
    }
}

They're broadly useful, so I'd like to be able to use them in different test classes other than where they're specified. For example:


class MyTestClass {

    @Property
    void testThing(@ForAll("someDomainSpecificThing") String thing) {
        ...
    }
}

However, jqwik is unable to discover this Provider since it lives outside of the current class. I can, of course, manually import the provider from the other class and setup a new provider in this one, but that all feels a bit crufty.

Is there a way to directly use a Provider specified in another file?

1

There are 1 best solutions below

0
On

jqwik's mechanism for provider sharing is called "Domains". There's a whole chapter on it in jqwik's user guide.

In your example you could introduce a domain context class like this:

class MyDomainContext extends AbstractContextBase {
    @Provide 
    public Arbitrary<String> someDomainSpecificThing() {
        /// return whatever
    }
}

and then use the context in your property:

class MyTestClass {

    @Property
    @Domain(MyDomainContext.class)
    void testThing(@ForAll String thing) {
        ...
    }
}