Skip to content

Commit

Permalink
#1306 Remove unneeded logging level configuration in BaseFakerTest.be…
Browse files Browse the repository at this point in the history
…fore()

It was WTF moment when I realized that method BaseFakerTest.before() resets all logger levels to INFO.
It took few hours to find out why my logging configuration doesn't work. :(
  • Loading branch information
asolntsev committed Aug 23, 2024
1 parent 8e6ffc3 commit 9df62a1
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 22 deletions.
2 changes: 0 additions & 2 deletions src/main/java/net/datafaker/providers/base/Twitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
Expand Down Expand Up @@ -58,7 +57,6 @@ public Date createdTime(boolean forward, Date base, Date constraints) {
*/
public String twitterId(int expectedLength) {
if (expectedLength <= 6 || expectedLength >= 25) {
LOGGER.setLevel(Level.WARNING);
LOGGER.warning("expectedLength <= 6 may easily cause twitter id collision. And expectedLength >= 25" +
" can be easily out of bound.");
}
Expand Down
16 changes: 4 additions & 12 deletions src/test/java/net/datafaker/providers/base/BaseFakerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import java.util.regex.Pattern;

Expand All @@ -39,17 +37,11 @@ public class BaseFakerTest<T extends BaseFaker> {
protected final T faker = getFaker();

@BeforeEach
protected void before() {
@SuppressWarnings("EmptyTryBlock")
final void resetMocks() throws Exception {
try (AutoCloseable ignored = MockitoAnnotations.openMocks(this)) {

Logger rootLogger = LogManager.getLogManager().getLogger("");
Handler[] handlers = rootLogger.getHandlers();
rootLogger.setLevel(Level.INFO);
for (Handler h : handlers) {
h.setLevel(Level.INFO);
}
} catch (Exception e) {
throw new RuntimeException(e);
// Need to reset all @Spy and @Mock fields
// because all test methods share the same test class instance due to @TestInstance(PER_CLASS)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ class RandomFakerTest extends BaseFakerTest<BaseFaker> {
private Random random;

@BeforeEach
protected void before() {
super.before();
final void before() {
random = new Random();
faker = new BaseFaker(random);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class HebrewFoodTest extends FoodFakerTest {
private Food food = getFaker().food();

@BeforeEach
protected void before() {
final void before() {
food = getFaker().food();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,16 @@ class FakeValuesServiceTest extends AbstractFakerTest {
@Mock
private RandomService randomService;

@Spy
private FakeValuesService fakeValuesService;
private FakerContext context;

@BeforeEach
protected void before() {
super.before();

final void before() {
// always return the first element
when(randomService.nextInt(anyInt())).thenReturn(0);
context = new FakerContext(new Locale("test"), randomService);
when(mockedFaker.getContext()).thenReturn(context);

fakeValuesService = Mockito.spy(new FakeValuesService());
fakeValuesService.updateFakeValuesInterfaceMap(context.getLocaleChain());
}

Expand Down

0 comments on commit 9df62a1

Please sign in to comment.