Refactor tests extending BaseFakerTest#1606
Conversation
b8b0fc0 to
b3b732d
Compare
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1606 +/- ##
============================================
+ Coverage 92.40% 92.41% +0.01%
- Complexity 3356 3357 +1
============================================
Files 331 331
Lines 6619 6618 -1
Branches 655 655
============================================
Hits 6116 6116
- Misses 345 346 +1
+ Partials 158 156 -2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
b3b732d to
4c07d4b
Compare
…testProviderList` and `testNoDuplications`. Also, force subclasses to implement `providerListTest()` by marking it as `abstract`. This simplifies understanding of what these tests verify.
instead, just implement method `getFaker()`.
* don't use annotations `@Spy` and `@Mock` * don't need to reset mocks between tests Especially `FakeValuesServiceTest` got much simpler. It's not needed to mock so much. It's always better to use real objects instead of mocks.
4c07d4b to
e42bc15
Compare
There was a problem hiding this comment.
Pull Request Overview
This PR refactors test classes by removing unnecessary inheritance from BaseFakerTest and AbstractFakerTest for tests that don't need the common test methods (testProviderList and testNoDuplications). The refactoring simplifies test classes by having them directly instantiate Faker instances instead of extending base test classes.
- Removes inheritance from
BaseFakerTest/AbstractFakerTestfor tests that don't use provider list testing - Replaces
setFaker()calls with directgetFaker()overrides in nested test classes - Adds direct
Fakerinstance creation in simplified test classes
Reviewed Changes
Copilot reviewed 73 out of 73 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| AbstractFakerTest.java | Completely removed as it's no longer needed |
| BaseFakerTest.java | Made abstract and removed setFaker() method |
| Various test classes | Removed inheritance and added direct Faker instantiation |
| Provider test classes | Made abstract and added final modifiers to getFaker() |
| Nested test classes | Replaced setup/teardown with getFaker() overrides |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| final class HebrewFoodTest extends FoodFakerTest { | ||
| private final Food food = getFaker().food(); |
There was a problem hiding this comment.
The field food calls getFaker().food() but this class no longer extends a base class with getFaker() method. This will cause a compilation error.
| private final Food food = getFaker().food(); | |
| private final Food food = new Faker().food(); |
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| final class HebrewFoodTest extends FoodFakerTest { |
There was a problem hiding this comment.
This class extends FoodFakerTest but the field initialization on line 12 suggests it was meant to be refactored to not extend the base class. Either remove the inheritance or add a getFaker() override for Hebrew locale.
| private FakerContext context; | ||
| private final Faker faker = spy(new Faker(new Locale("test"))); | ||
| private final FakerContext context = faker.getContext(); | ||
| private final FakeValuesService fakeValuesService = new FakeValuesService(); |
There was a problem hiding this comment.
The FakeValuesService constructor requires a FakerContext parameter but none is provided here. This will cause a compilation error.
| private final FakeValuesService fakeValuesService = new FakeValuesService(); | |
| private final FakeValuesService fakeValuesService = new FakeValuesService(context); |
| @Test | ||
| void bothify2Args() { | ||
| final DummyService dummy = mock(DummyService.class); | ||
| DummyService dummy = new DummyService(); |
There was a problem hiding this comment.
The DummyService constructor now requires parameters (based on line 359) but this instantiation provides none. This will cause a compilation error.
| DummyService dummy = new DummyService(); | |
| DummyService dummy = new DummyService(null); // TODO: replace null with appropriate constructor arguments |
| @Test | ||
| void regexifyDirective() { | ||
| final DummyService dummy = mock(DummyService.class); | ||
| DummyService dummy = new DummyService(); |
There was a problem hiding this comment.
The DummyService constructor now requires parameters but this instantiation provides none. This will cause a compilation error.
| DummyService dummy = new DummyService(); | |
| DummyService dummy = new DummyService(context); |
| @Test | ||
| void regexifySlashFormatDirective() { | ||
| final DummyService dummy = mock(DummyService.class); | ||
| DummyService dummy = new DummyService(); |
There was a problem hiding this comment.
The DummyService constructor now requires parameters but this instantiation provides none. This will cause a compilation error.
| DummyService dummy = new DummyService(); | |
| DummyService dummy = new DummyService(context); |
| @Test | ||
| void regexifyDirective2() { | ||
| final DummyService dummy = mock(DummyService.class); | ||
| DummyService dummy = new DummyService(); |
There was a problem hiding this comment.
The DummyService constructor now requires parameters but this instantiation provides none. This will cause a compilation error.
| DummyService dummy = new DummyService(); | |
| DummyService dummy = new DummyService(context); |
|
Thanks!! |
| assertThat(mockedFaker.internet().username()) | ||
| .doesNotContain(" ", "'") | ||
| .matches("^(\\w+)\\.(\\w+)$") | ||
| .matches("^\\p{javaLowerCase}+\\.\\p{javaLowerCase}+$"); |
There was a problem hiding this comment.
ideally this test should be rewritten
currently it doesn't check much
If I replace implementation of username with return a.a it will pass
There was a problem hiding this comment.
even more: if you compare output of
firstName() + "." + lastName() vs javaLowerCase for internet().username() it will not match because tests are running in TR locale
first will give
jin.quan
second
jın.quan (with turkish dotless i)
There was a problem hiding this comment.
Emmmm...
currently it doesn't check much
Yes, this is true for most DF tests. It's because they call random generator and cannot fully verify the random output.
Anyway, now this test checks more than before.
it will not match because tests are running in TR locale
Sorry, I don't understand how to make this test fail. For me, it works with both English and Turkish locale.
There was a problem hiding this comment.
It isn't lower casing the value(s), it's comparing against a character set of all Java lower case characters. (From my understanding)
There was a problem hiding this comment.
@kingthorin Yes. username returns lower case, and .matches("...\p{javaLowerCase}...") verifies that this is lower case.
There was a problem hiding this comment.
|
|
||
| public String hello() { | ||
| return "Hello"; | ||
| return greetings.get(counter.getAndIncrement()); |
There was a problem hiding this comment.
I would vote for either circular approach or hard fail with a nicer exception
otherwise the one who will use something like that
sameResolution: "#{hello} #{hello} #{hello} #{hello}"will spend lots of time for debugging
There was a problem hiding this comment.
Maybe I don't understand, but current implementation do exactly that: fails with a clear exception:
java.lang.RuntimeException: Failed to invoke MethodAndCoercedArgs[method=DummyService.hello()...]
...
Caused by: java.lang.IndexOutOfBoundsException: Index: 1 Size: 1
extends BaseFakerTestfrom all test that don't need to runtestProviderListandtestNoDuplicationssetFaker()