Skip to content

Commit c9a1583

Browse files
authored
Fix seed setting as annotated (#1052)
1 parent 897f1a9 commit c9a1583

File tree

2 files changed

+13
-78
lines changed

2 files changed

+13
-78
lines changed

fixture-monkey-junit-jupiter/src/main/java/com/navercorp/fixturemonkey/junit/jupiter/extension/FixtureMonkeySeedExtension.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
import com.navercorp.fixturemonkey.junit.jupiter.annotation.Seed;
3030

3131
public final class FixtureMonkeySeedExtension implements BeforeTestExecutionCallback, AfterTestExecutionCallback {
32-
private static final ThreadLocal<Long> SEED_HOLDER = new ThreadLocal<>();
33-
3432
private static final Logger LOGGER = LoggerFactory.getLogger(FixtureMonkeySeedExtension.class);
3533

3634
@Override
@@ -57,7 +55,7 @@ public void afterTestExecution(ExtensionContext context) throws Exception {
5755
* Sets the seed for generating random numbers.
5856
**/
5957
private void setSeed(long seed) {
60-
SEED_HOLDER.set(Randoms.create(String.valueOf(seed)).nextLong());
58+
Randoms.create(String.valueOf(seed));
6159
}
6260

6361
/**
@@ -66,6 +64,7 @@ private void setSeed(long seed) {
6664
**/
6765
private void logSeedIfTestFailed(ExtensionContext context) {
6866
Method testMethod = context.getRequiredTestMethod();
69-
LOGGER.error(String.format("Test Method [%s] failed with seed: %d", testMethod.getName(), SEED_HOLDER.get()));
67+
LOGGER.error(
68+
String.format("Test Method [%s] failed with seed: %d", testMethod.getName(), Randoms.currentSeed()));
7069
}
7170
}

fixture-monkey-junit-jupiter/src/test/java/com/navercorp/fixturemonkey/junit/jupiter/extension/FixtureMonkeySeedExtensionTest.java

Lines changed: 10 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,14 @@
1818
package com.navercorp.fixturemonkey.junit.jupiter.extension;
1919

2020
import static org.assertj.core.api.BDDAssertions.then;
21-
import static org.junit.jupiter.api.Assertions.assertAll;
22-
import static org.junit.jupiter.api.Assertions.assertEquals;
23-
import static org.junit.jupiter.api.Assertions.assertNotNull;
24-
import static org.junit.jupiter.api.Assertions.assertTrue;
2521

26-
import java.util.Collections;
22+
import java.util.Arrays;
2723
import java.util.HashSet;
2824
import java.util.List;
2925
import java.util.Set;
3026

31-
import javax.validation.constraints.NotBlank;
32-
import javax.validation.constraints.NotNull;
33-
34-
import org.junit.jupiter.api.AfterEach;
35-
import org.junit.jupiter.api.BeforeAll;
3627
import org.junit.jupiter.api.RepeatedTest;
37-
import org.junit.jupiter.api.Test;
3828
import org.junit.jupiter.api.extension.ExtendWith;
39-
import org.slf4j.Logger;
40-
import org.slf4j.LoggerFactory;
41-
42-
import ch.qos.logback.classic.Level;
43-
import ch.qos.logback.classic.LoggerContext;
44-
import ch.qos.logback.classic.spi.ILoggingEvent;
45-
import ch.qos.logback.core.read.ListAppender;
46-
import lombok.Data;
4729

4830
import com.navercorp.fixturemonkey.FixtureMonkey;
4931
import com.navercorp.fixturemonkey.api.type.TypeReference;
@@ -52,54 +34,11 @@
5234
@ExtendWith(FixtureMonkeySeedExtension.class)
5335
class FixtureMonkeySeedExtensionTest {
5436
private static final FixtureMonkey SUT = FixtureMonkey.create();
55-
private static ListAppender<ILoggingEvent> EVENT_APPENDER;
56-
57-
@BeforeAll
58-
static void setup() {
59-
LoggerContext loggerContext = (LoggerContext)LoggerFactory.getILoggerFactory();
60-
61-
EVENT_APPENDER = new ListAppender<>();
62-
EVENT_APPENDER.setContext(loggerContext);
63-
EVENT_APPENDER.start();
64-
65-
loggerContext.getLogger(Logger.ROOT_LOGGER_NAME).addAppender(EVENT_APPENDER);
66-
loggerContext.getLogger(Logger.ROOT_LOGGER_NAME).setLevel(Level.ERROR);
67-
68-
}
69-
70-
@AfterEach
71-
void tearDown() {
72-
EVENT_APPENDER.list.clear();
73-
}
74-
75-
@Seed(1234L)
76-
@Test
77-
void testWithSeed() {
78-
boolean logFound = false;
79-
try {
80-
Product product = SUT.giveMeBuilder(Product.class)
81-
.set("id", 1000L)
82-
.set("productName", "Book")
83-
.sample();
84-
85-
assertAll(
86-
() -> assertNotNull(product),
87-
() -> assertEquals(2000L, (long)product.getId()),
88-
() -> assertEquals("Computer", product.getProductName())
89-
);
90-
} catch (AssertionError e) {
91-
List<ILoggingEvent> logs = EVENT_APPENDER.list;
92-
logFound = logs.stream()
93-
.anyMatch(event -> event.getFormattedMessage()
94-
.contains("Test Method [testWithSeed] failed with seed: "));
95-
}
96-
assertTrue(logFound, "Expected log message found.");
97-
}
9837

9938
@Seed(1)
10039
@RepeatedTest(100)
10140
void seedReturnsSame() {
102-
String expected = "섨ꝓ仛禦催ᘓ蓊類౺阹瞻塢飖獾ࠒ⒐፨婵얎⽒竻·俌欕悳잸횑ٻ킐結";
41+
String expected = "섨ꝓ仛禦催ᘓ蓊類౺阹瞻塢飖獾ࠒ⒐፨";
10342

10443
String actual = SUT.giveMeOne(String.class);
10544

@@ -109,7 +48,7 @@ void seedReturnsSame() {
10948
@Seed(1)
11049
@RepeatedTest(100)
11150
void latterValue() {
112-
String expected = "聩ዡ㘇뵥刲禮ᣮ鎊熇捺셾壍Ꜻꌩ垅凗❉償粐믩࠱哠횛";
51+
String expected = "欕悳잸";
11352
SUT.giveMeOne(String.class);
11453

11554
String actual = SUT.giveMeOne(String.class);
@@ -120,7 +59,10 @@ void latterValue() {
12059
@Seed(1)
12160
@RepeatedTest(100)
12261
void containerReturnsSame() {
123-
List<String> expected = Collections.singletonList("仛禦催ᘓ蓊類౺阹瞻塢飖獾ࠒ⒐፨婵얎⽒竻·俌欕悳잸횑ٻ킐結");
62+
List<String> expected = Arrays.asList(
63+
"仛禦催ᘓ蓊類౺阹瞻塢飖獾ࠒ⒐፨婵얎⽒竻·俌欕悳잸횑ٻ킐結",
64+
"塸聩ዡ㘇뵥刲禮ᣮ鎊熇捺셾壍Ꜻꌩ垅凗❉償粐믩࠱哠"
65+
);
12466

12567
List<String> actual = SUT.giveMeOne(new TypeReference<List<String>>() {
12668
});
@@ -131,7 +73,9 @@ void containerReturnsSame() {
13173
@Seed(1)
13274
@RepeatedTest(100)
13375
void containerMattersOrder() {
134-
Set<String> expected = new HashSet<>(Collections.singletonList("仛禦催ᘓ蓊類౺阹瞻塢飖獾ࠒ⒐፨婵얎⽒竻·俌欕悳잸횑ٻ킐結"));
76+
Set<String> expected = new HashSet<>(
77+
Arrays.asList("仛禦催ᘓ蓊類౺阹瞻塢飖獾ࠒ⒐፨婵얎⽒竻·俌欕悳잸횑ٻ킐結", "塸聩ዡ㘇뵥刲禮ᣮ鎊熇捺셾壍Ꜻꌩ垅凗❉償粐믩࠱哠")
78+
);
13579

13680
Set<String> actual = SUT.giveMeOne(new TypeReference<Set<String>>() {
13781
});
@@ -150,12 +94,4 @@ void multipleContainerReturnsDiff() {
15094

15195
then(firstSet).isNotEqualTo(secondList);
15296
}
153-
154-
@Data
155-
private static class Product {
156-
@NotNull
157-
private Long id;
158-
@NotBlank
159-
private String productName;
160-
}
16197
}

0 commit comments

Comments
 (0)