Comments (5)
This is expected behavior
the reason is that birthday
returns a timestamp limited by min age and max age (if no args are passed then default values are 18 and 65) . Ages are defined by amount of days in the past from the current day. Each time there is a new date the ages will define new boundaries.
If you want to have something independent on current date then you can use between
and pass there strict boundaries like that
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)));
from datafaker.
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
from datafaker.
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 :
org.opentest4j.AssertionFailedError:
Expected :1993-06-21
Actual :1993-06-22
from datafaker.
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.)
from datafaker.
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.
from datafaker.
Related Issues (20)
- birthday throwing exception if run at 2/29/2024 HOT 7
- Faker.date().birthday() returns in invalid Date on Leap Day HOT 2
- Possible JSON generation error HOT 15
- Duplicate faker groups (Money and Currency) HOT 9
- Release 2.2.0 HOT 10
- Add Java 22 to the build matrix. HOT 6
- Adding a Sensor data provider for testing IoT applications HOT 5
- Support JPMS HOT 7
- Extend medical providers to include more medicine-related values HOT 11
- We should support Ollama HOT 2
- Fix address for 'en-NZ' locale
- Using metacharacters on a character set with regexify does not produce the expected text
- v2.2.0: Exceptions when calling certain Faker methods HOT 8
- NullPointerException when using Faker#locality HOT 3
- Unit test failed
- Provide Stream output for transformers HOT 4
- java.lang.RuntimeException: Unable to resolve #{male_first_name} directive for FakerContext HOT 14
- Allow an easy way to create random PII/PHI data HOT 3
- Breaking DateAndTime HOT 4
- Intermitted expressions failures during testing HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from datafaker.