-
Notifications
You must be signed in to change notification settings - Fork 152
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
The faker.date().birthday() method returns different dates when using seed #1087
Comments
This test is passing @Test
void testSameBirthday() {
final Faker faker = new Faker(new Random(1));
final Date date1 = faker.date().birthday();
final Faker faker2 = new Faker(new Random(1));
final Date date2 = faker2.date().birthday();
assertThat(date1).isEqualTo(date2);
final Faker faker3 = new Faker(new Random(10));
final Date date3 = faker3.date().birthday(10, 100);
final Faker faker4 = new Faker(new Random(10));
final Date date4 = faker4.date().birthday(10, 100);
assertThat(date3).isEqualTo(date4);
} Please either submit a reproducible TC or I don't see an issue here |
The issue is that if you launch the test another day that you have created it, the birthdates generated by faker.date().birthday() will not give the same date so if you hardcoded the expected birthdate, the test will fail. For example, I have this test : @Test
void testBirthDate(){
var expectedBirthDate = LocalDate.of(1993, 6, 21);
var actualBirthDate = myclass.getBirthDate();
assertEquals(expectedBirthDate, actualBirthDate);
} and in my myclass i have : public LocalDate getBirthDate() {
return faker.date().birthday().toLocalDateTime().toLocalDate();
} So if i launch my test today (14/02/24) it works but if i launch it tomorrow (15/02/24), the dates given by the faker will not be the same even when using a seed for the faker. To show you that, i can change the date of my computer to 15/02/24 and this will happen :
|
I guess we could make another method where min and max age are only accurate to year (not month or day) and have the year be sticky (instead of using now). Would that be sufficient? (In this case the failure "should" only occur if you happen to run exactly at midnight on New Year's Eve.) |
This is expected behavior Faker faker = new Faker(new Random(1));
System.out.println(faker.date().between(
new Date(LocalDate.of(1900, 1, 1).toEpochDay() * 86400 * 1000),
new Date(LocalDate.of(2000, 1, 1).toEpochDay() * 86400 * 1000))); |
Great discussion here. Maybe your actual implementation is slightly different from the example you gave, but I think I wouldn't recommend testing for the strict value of data generated by a random data generator. The idea is that if you ask for a birth date, you may assume you're getting a birth date, say something which isn't in the future and isn't 200 years ago. That's something you could test for if you wanted, like: assertThat(birthdate).isInPast(), or something like that. Testing the exact value would be slightly trickier, and I personally don't much of a value. Maybe your use case is different though. |
Describe the bug
![Capture d’écran 2024-02-13 à 15 36 56](https://private-user-images.githubusercontent.com/144101007/304455789-93ff04f8-4199-4533-8289-d3b252e14471.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjExMzYxMzksIm5iZiI6MTcyMTEzNTgzOSwicGF0aCI6Ii8xNDQxMDEwMDcvMzA0NDU1Nzg5LTkzZmYwNGY4LTQxOTktNDUzMy04Mjg5LWQzYjI1MmUxNDQ3MS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNzE2JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDcxNlQxMzE3MTlaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1mNDRkYmM2OTZkZjlkYWZiNzU4ZjYwMzU3MGJlMjcyZmI2M2VlOGNkYWRlNGUwMmQwZmY1YzA5MzkzNDFjMTIwJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.yfeLHz1uft_Z5srWQ41SzpjsxN6d_xZunwanjDvO9cg)
When using a seed, the faker.date().birthday() method returns different birthdates. Indeed, the birthday() method seems to use the current date to generate a random birthdate. So when the method is used another day, the result will be different. This behavior can break tests.
To Reproduce
You can easily reproduce this bug by using a seed and use the faker.date().birthday() in a simple test that checks the date returned by the method.
Expected behavior
If you launch the test another day that the day you created it, the test will fail since the date returned will not be the same.
Versions:
Additional context
This was already an issue in the first java-faker
The text was updated successfully, but these errors were encountered: