FlexTradeUKLtd / jfixture

JFixture is an open source library based on the popular .NET library, AutoFixture
MIT License
105 stars 22 forks source link

int range not working #48

Open MateuszMysliwiec opened 5 years ago

MateuszMysliwiec commented 5 years ago

Hello, I am trying to specify range to int:

    @Fixture
    @Range(min = 1, max = 10)
    private int minNoOfFirstAidersRequiredInTeam;

But still, returned int value is not in range 1-10, any help would be appreciated, thank you!

richkeenan commented 5 years ago

Hi @MateuszMysliwiec thanks for the report.

I've tried to reproduce this but it works fine for me, so here's a few questions that would help diagnosing it:

Thanks!

MateuszMysliwiec commented 5 years ago

Hi @richkeenan, thank you for response.

I am using latest version of JFixture, and looks like I have misunderstood the "Cheat Sheet" guide because I am not using any of those mentioned here:

Are you running @Rule public FixtureRule fr = FixtureRule.initFixtures() or FixtureAnnotations.initFixtures(this); which will initialise the @Fixture fields?

Here is my workflow:

  1. I am using JFixture to generate random/mock data for Database tables generated by Hibernate java framework.

I am doing it this way:

JFixture fixutre = new JFixture();
SomeEntity siteRequirement = fixture.create(SomeEntity.class);

And that is all, now inside SomeEntity class, I wanted to specify ranges for some variables, using add-notations specified in first post, answering your question, I checked and I am using correct imports, so it must be probably lack of @Rule you mentioned, but I still do not understand where shall I put it in my case, thank you again and sorry if I missed something.

TWiStErRob commented 5 years ago

Assuming SomeEntity is a production class, it shouldn't have @Fixture annotations in it. Remove jfixture dependency from production, and keep it only for test source set. The @Fixture annotation is only processed by the rule or initFixtures. The way you're creating your fixture object works without the annotation, but creates a full-range value as you observed. Sometimes if you want a specific value you need to set it manually:

SomeEntity siteRequirement = fixture.create(SomeEntity.class);
siteRequirement.minNoOfFirstAidersRequiredInTeam = fixture.create().inRange(int.class, 1, 10)

If you want to create multiple of these objects but don't want to manually configure all the time, you can use fixture.customize().intercept(SomeEntity.class, ...) (or transform) to change the auto-generated object.