Add @FakerSource annotation and FakerSourceProvider for dynamic test data generation#1869
Add @FakerSource annotation and FakerSourceProvider for dynamic test data generation#1869spannm wants to merge 1 commit into
@FakerSource annotation and FakerSourceProvider for dynamic test data generation#1869Conversation
d1c2e28 to
e002ceb
Compare
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1869 +/- ##
=========================================
Coverage 92.48% 92.48%
- Complexity 3561 3562 +1
=========================================
Files 346 346
Lines 7048 7048
Branches 685 685
=========================================
Hits 6518 6518
- Misses 365 366 +1
+ Partials 165 164 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
I would say that the public API for end users is The fact that DF uses YML and cryptic strings like |
e002ceb to
09ec6b1
Compare
…data generation Introduce a custom JUnit Jupiter ArgumentsProvider that dynamically resolves and executes Datafaker provider methods via reflection - Implement FakerSourceProvider supporting direct field/path expressions (e.g., 'address#longitude' or 'BELGIAN_FAKER.address') - Fall back to test class instantiation if testInstance is unavailable during the early lifecycle phase of non-static fields - Add support for combinatorial parameter arrays via multiParams - Migrate several tests (AddressTest, CodeTest, FinanceTest, HttpTest) from @RepeatedTest to @ParameterizedTest leveraging new @FakerSource
09ec6b1 to
4762d0a
Compare
@asolntsev fair point — but this is test-only tooling under The real payoff is the test report: every generated value now shows up individually and by name (see before/after screenshots above), instead of being buried in a Happy to rename P.S.: Updated PR description for clarity and added screenshots. cc: @bodiam |
|
Sorry, I'm closing this because of AI messages. |
|
That's disappointing - I put in a considerable amount of work and genuinely believe it adds value to the test suite. The screenshots speak for themselves. |
|
The fact that you put a lot of work in it or not is no reason to add it to the code. Besides, you said you were looking for feedback, and now you have feedback. I don't think it's very useful at this stage, @asolntsev doesn't think it's super useful, and if you want, you can always create a small side library, datafaker-junit5 or so, which has this functionality. If it's used by other people, we can always reconsider to add it to the core, and make it part of the "public API" or so, but at this stage, I'm not convinced about this. |
RFC — looking for feedback, not asking for a merge yet
Right now, "generate N fake values and check a property" means
@RepeatedTestplus a manualfaker.x().y()call, and locale variants each get their own staticFakerfield.@FakerSourcereplaces that with one annotation:@ParameterizedTest(name = "[{index}] {0}")
@FakerSource(code = "address#longitude", repeat = 10)
void testLongitude(String longitude) { ... }
It resolves a provider method via reflection (also handles locales, whole-provider injection, and combinatorial params). Test-only, lives under
src/test— this is tooling for our own suite, not public API. I migratedAddressTest,CodeTest,FinanceTest,HttpTestas a proof of concept.Why it's nice: the standout win is much better test feedback. Instead of one aggregate
@RepeatedTestresult where generated values are buried in a loop, each value gets its own named, inspectable row in the report — you can see exactly which input triggered a failure. CompareirishAddressbefore/after:Before (

@RepeatedTest— values hidden):After (

@FakerSource— values visible by name):Same story for
creditCardWithType— before you only see the enum branch that ran, after you see the actual generated card number per case:Before:

After:

It also kills off a bunch of one-off static locale-
Fakerfields and@MethodSourcegenerator methods.Tradeoff to weigh: resolution is string-based reflection, so a rename like
Address.longitude()isn't caught by the IDE while editing. That said, it is caught immediately at build time — an incorrectly annotated@FakerSourcesimply fails the test, same as any other broken test, so nothing wrong slips through CI unnoticed. There's also a fallback that re-instantiates the test class if the instance isn't ready yet during early JUnit lifecycle, and method resolution grabs the first match by name/type — worth watching if we ever get overloaded provider methods, though it hasn't been an issue in the proof-of-concept migration.Before I migrate more tests: given the much better test feedback, is this a direction we want — with the understanding that any misconfiguration surfaces as a failing test, not a silent runtime issue?