by

Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Jun 22 16:37
    dadiyang commented #420
  • Jun 22 08:12
    onacit commented #420
  • Jun 22 07:06
    onacit commented #420
  • Jun 10 12:56
    gudshugo opened #422
  • Jun 07 02:18
    dadiyang synchronize #421
  • Jun 07 02:09
    dadiyang opened #421
  • Jun 07 01:56
    dadiyang edited #420
  • Jun 07 01:50
    dadiyang opened #420
  • May 30 17:57
    calinafox edited #419
  • May 30 17:54
    calinafox edited #419
  • May 30 17:54
    calinafox edited #419
  • May 30 17:53
    calinafox edited #419
  • May 30 17:53
    calinafox edited #419
  • May 30 17:52
    calinafox opened #419
  • May 22 13:31
    premanandc edited #418
  • May 22 13:23
    premanandc opened #418
  • May 19 00:07
    dadams89 opened #417
  • May 12 09:49
    mjureczko commented #412
  • May 10 18:18
    araknafobia opened #416
  • May 10 10:50
    benas commented #413
Mahmoud Ben Hassine
@benas
@elexx I'm not sure this is possible out of the box. You need to write a custom predicate for that.
Alexander Falb
@elexx
I'm not sure I can follow you. .excludeField(field -> ...) does not work, because aside from the field I would need to know which class easyrandom is currently trying to fill. This way I could compare if the given field is declared in the given class, like this eg .excludeField((classToBeFilled, fieldToBeFilled) -> classToBeFilled != fieldToBeFilled.getDeclaringClass()).
Mahmoud Ben Hassine
@benas
ok I see, in that case, a custom exclusion policy is more appropriate. It gives you access to the current field and the randomization context, from which you can get the target type and do your comparison. Is this what you are looking for?
Alexander Falb
@elexx
Which in turn means, I need to check the fieldExclusionPredicates und typeExclusionPredicates myself, because ExclusionChecker is not running anymore?
Mahmoud Ben Hassine
@benas
BTW, even with custom predicate you should be able to do it, by passing the target type (which you know upfront) as a parameter to the predicate (at construction time for example). Do you agree?
Alexander Falb
@elexx
That's right, I just don't really like hardcoding all parent types, because they might change or we might add others in the future.
Mahmoud Ben Hassine
@benas

Which in turn means, I need to check the fieldExclusionPredicates und typeExclusionPredicates myself, because ExclusionChecker is not running anymore?

No, you can extend it and override its shouldBeExcluded(final Field field, final RandomizerContext context) method.

then register your custom ExclusionChecker as an exclusion policy.
Alexander Falb
@elexx
ExclusionChecker is package private
Mahmoud Ben Hassine
@benas
oops yes you are right! sorry I missed that.
please open an issue with an example and we can see the best way to address your use case.
Alexander Falb
@elexx
I mean I could .getExclusionPolicy() and delegate from my custom policy and then .setExclusionPolicy(..) my custom one, but this really feels a bit hacky ;-)
Mahmoud Ben Hassine
@benas
Is that ok for you?
Alexander Falb
@elexx
Sure. Thanks!
Mahmoud Ben Hassine
@benas
Indeed, that's hacky.. But no hacks here, either a clean way or nothing!
If it is not possible today, Easy Random should be improved
Mahmoud Ben Hassine
@benas
I was randomly browsing the web and came across this: https://www.baeldung.com/java-easy-random. Happy to see people using and writing about Easy Random :smile:
Mahmoud Ben Hassine
@benas
Some other nice articles and blog posts: https://github.com/j-easy/easy-random#articles-and-blog-posts
I'm also proud to see some serious companies like Netflix, Jetbrains (IntelliJ!) and Mulesoft using it: https://github.com/j-easy/easy-random#who-is-using-easy-random-
Mahmoud Ben Hassine
@benas
This could not be possible without the help of all amazing contributors! So many thanks to anyone who contributed to Easy Random :thumbsup:
Mahmoud Ben Hassine
@benas

Hi @/all ,

Easy Random v4.2 has been released with a couple of enhancements and bug fixes: https://github.com/j-easy/easy-random/releases/tag/easy-random-4.2.0.

Many thanks to all contributors to this release!

Happy randomizing :wink:

Cem Nura
@cemnura

Hi jeasy community,

Firstly, thanks for your great work I found easy-random greatly useful.
I was recently trying to implement easy-random for my unit tests.

I had a couple of difficulties relying on dependencies.

I wished to used EmailRandomizer to generate a fake email. However, I got a snakeyaml transient dependency error

I researched the error and found that this was an java-faker error.
DiUS/java-faker#327

So I configured my gradle.build as the following that I saw on the java-faker issue such as;

testImplementation 'org.jeasy:easy-random-core:4.2.0'
testImplementation ('org.jeasy:easy-random-randomizers:4.2.0') {exclude module: 'snakeyaml'}
testImplementation group: 'org.yaml', name: 'snakeyaml', version: '1.17'

Then I was able to get EmailRandomizer in my classpath. I though this information would be beneficial.

I also created a issue I ran into while I was implement easy-random. j-easy/easy-random#399

Mahmoud Ben Hassine
@benas

Hi @cemnura , thank you! I'm glad you like Easy Random.

I though this information would be beneficial.

Thank you for sharing this useful tip. I hope java-faker will get this resolved soon.

I also created a issue I ran into while I was implement easy-random. j-easy/easy-random#399

Looking into this right now, I will try to share some feedback on github asap. Thank you for reporting the issue.

Ender Tunc
@endertunc_gitlab
Hey all
I am having a strange issue with easy random
I have the following class:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ExampleClass {
    private Optional<String> a;
    private Optional<String> b;
}
Then I have the following definitions;
ExampleClass e1 = easyRandom.nextObject(ExampleClass.class);
ExampleClass e2 = new ExampleClass(Optional.of("asd"), Optional.of("asd"));
When I try to sout I see the following:
System.out.println(e1);
System.out.println(e2);


ExampleClass(a=Optional[java.lang.Object@7675f6], b=Optional[java.lang.Object@3117145])
ExampleClass(a=Optional[asd], b=Optional[asd])
As you can see the e1 which is generated by easy random has object inside optional.
Ender Tunc
@endertunc_gitlab
It seems to be problematic. I was trying to serialize the ExampleClass generated by easy random and it keeps failing with
class java.lang.Object cannot be cast to class java.lang.String (java.lang.Object and java.lang.String are in module java.base of loader 'bootstrap')
I can serialize the ExampleClass (e2) I created manually without a problem.
Then I said it might be serialization but following simple java code also fails.
e1.getA().ifPresent(System.out::println);

class java.lang.Object cannot be cast to class java.lang.String (java.lang.Object and java.lang.String are in module java.base of loader 'bootstrap')
Does anyone have any idea?
I am using java 8 btw.
Mahmoud Ben Hassine
@benas
@endertunc_gitlab Thank you for raising this. Please open an issue and I will take a look asap (unless someone in this room has already an answer).
Ender Tunc
@endertunc_gitlab
@benas j-easy/easy-random#416 Thanks in advance!
Ender Tunc
@endertunc_gitlab
@benas if I understand this correctly at some point we generate instance of field by calling T result = objectFactory.createInstance(type, context);
When we have the type Option we get 2 fields from getDeclaredFields(result)
private static final java.util.Optional java.util.Optional.EMPTY and private final java.lang.Object java.util.Optional.value. Since the first one is static it is skipped and the second one will produce an object and put inside an Option. I assume we have private final java.lang.Object java.util.Optional.value due to the type erasure.
You can take a look at this ss as well. You will see that we have java.lang.Object as type parameter.
https://i.imgur.com/DT87Qvi.png
Anderson Mesquita
@andersonvom
Hi there, I'm trying my hand at writing a custom randomizer that - making assumptions about the protobuf structure - will generate random protobuf objects. I'm currently iterating over all the fields in a protobuf object and calling easyRandom.nextObject(type), but I'd like to take the field randomizers into account (e.g. .randomize(field -> FieldPredicates.named("foo"), new SomeRandomizer()). Is there a way to do it in the public API, either directly from EasyRandom or from RadomizerContext?
Mahmoud Ben Hassine
@benas
@andersonvom The API you are looking for is EasyRandomParameters. Have you registered your custom randomizer? something like:
EasyRandomParameters parameters = new EasyRandomParameters()
   .randomize(field -> FieldPredicates.named("foo"), new SomeRandomizer());

EasyRandom easyRandom = new EasyRandom(parameters);
easyRandom.nextObject(type);
Anderson Mesquita
@andersonvom
Hi @benas, thanks for your reply! My understanding is that this works if an object of type type has a field named foo, right? In my case, I'm creating a protobuf object (which can't be automatically created by easy random). For a concrete example:
// if in easy random config I have
.randomize(field -> FieldPredicates.named("city"), new CityRandomizer())

// in ProtoRandomizer I need to do something like
String someCity = <generate a city directly from easy random>;
// so that I can call the appropriate setter in the proto object builder and return it
return protoObject.setCity(someCity).build();
Mahmoud Ben Hassine
@benas
@andersonvom Do you have a minimal example I can play with? I would love to help but I still fail to see what's missing in the current API to make this possible.
Anderson Mesquita
@andersonvom
@benas absolutely. I send over an example by the end of the day (in the next 8h, that is). Thanks for helping out :D
calinafox
@calinafox

hello, I do have a question and I am not sure if it is a bug or I am just using it wrong
If I use the following code:

.randomize(LocalDateTime.class, () -> new LocalDateTimeRangeRandomizer(LocalDateTime.now(), LocalDateTime.now().plusDays(1)).getRandomValue());

I always get the same date which is basically Now() - date and time. But if I use:

.randomize(Date.class, () -> new DateRangeRandomizer(dateManager.getStartDate(), dateManager.getEndDate()).getRandomValue());

the date will be randomized.
I am wrong to assume that since I am able to get the random value for date and for LocatDateTime not, that there might be an issue?

calinafox
@calinafox
@all - the issue was on 4.0.0 it is now fixed in 4.2.0
nitishborade
@nitishborade
@benas Thank you for this awesome library. We were thinking of writing our solution to generate large test data until we came across this amazing library by you and your team.
Mahmoud Ben Hassine
@benas
@nitishborade Thank you very much! We're glad you like it!
nitishborade
@nitishborade

@benas Thank you!
This library has helped randomize our data to a huge extent. However, we encounter a couple of scenarios, and we are not sure how to use easy-random to overcome them.

  1. A field in our table only accepts unique Strings. Generating a large amount of data, the StringRandomizer or the WordRandomizer returns duplicate strings. How make the randomizer return a unique String always in case of large data?
  2. If a field in a Child object is dependent on the value of a field in the Parent object, how do I pass this value from Parent->Child while generating random objects e.g. the value of Child.endDate cannot be a date < Parent.startDate?

Your response will be of great value to us!

Mahmoud Ben Hassine
@benas

@nitishborade

  1. That's a limitation of java.util.Random. Try creating a custom randomizer based on java.security.SecureRandom or use the built-in UUIDRandomizer
  2. You should be able to do that with a ContextAwareRandomizer. The randomization context gives you access to current object, current field, target type, etc that should allow you to generate a random value based on the parent-> child relation. You can find some examples here: https://github.com/j-easy/easy-random/blob/master/easy-random-core/src/test/java/org/jeasy/random/context/ContextAwareRandomizationTests.java

Hope this helps.