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

int range not working #48

Open
MateuszMysliwiec opened this issue Nov 2, 2018 · 3 comments
Open

int range not working #48

MateuszMysliwiec opened this issue Nov 2, 2018 · 3 comments

Comments

@MateuszMysliwiec
Copy link

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
Copy link
Collaborator

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:

  • Are you running @Rule public FixtureRule fr = FixtureRule.initFixtures() or FixtureAnnotations.initFixtures(this); which will initialise the @Fixture fields?
  • Are you definitely importing the correct annotations? They should be,
import com.flextrade.jfixture.annotations.Fixture;
import com.flextrade.jfixture.annotations.Range;
  • Which version of JFixture are you using? I can checkout that tag and try to reproduce it again
  • Can you reproduce this issue using long instead of int?

Thanks!

@MateuszMysliwiec
Copy link
Author

MateuszMysliwiec commented Nov 2, 2018

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
Copy link
Contributor

TWiStErRob commented Nov 2, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants