Skip to content

Commit

Permalink
Fix seed setting as annotated (#1052)
Browse files Browse the repository at this point in the history
  • Loading branch information
seongahjo authored Sep 11, 2024
1 parent 897f1a9 commit c9a1583
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
import com.navercorp.fixturemonkey.junit.jupiter.annotation.Seed;

public final class FixtureMonkeySeedExtension implements BeforeTestExecutionCallback, AfterTestExecutionCallback {
private static final ThreadLocal<Long> SEED_HOLDER = new ThreadLocal<>();

private static final Logger LOGGER = LoggerFactory.getLogger(FixtureMonkeySeedExtension.class);

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

/**
Expand All @@ -66,6 +64,7 @@ private void setSeed(long seed) {
**/
private void logSeedIfTestFailed(ExtensionContext context) {
Method testMethod = context.getRequiredTestMethod();
LOGGER.error(String.format("Test Method [%s] failed with seed: %d", testMethod.getName(), SEED_HOLDER.get()));
LOGGER.error(
String.format("Test Method [%s] failed with seed: %d", testMethod.getName(), Randoms.currentSeed()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,14 @@
package com.navercorp.fixturemonkey.junit.jupiter.extension;

import static org.assertj.core.api.BDDAssertions.then;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Collections;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
import lombok.Data;

import com.navercorp.fixturemonkey.FixtureMonkey;
import com.navercorp.fixturemonkey.api.type.TypeReference;
Expand All @@ -52,54 +34,11 @@
@ExtendWith(FixtureMonkeySeedExtension.class)
class FixtureMonkeySeedExtensionTest {
private static final FixtureMonkey SUT = FixtureMonkey.create();
private static ListAppender<ILoggingEvent> EVENT_APPENDER;

@BeforeAll
static void setup() {
LoggerContext loggerContext = (LoggerContext)LoggerFactory.getILoggerFactory();

EVENT_APPENDER = new ListAppender<>();
EVENT_APPENDER.setContext(loggerContext);
EVENT_APPENDER.start();

loggerContext.getLogger(Logger.ROOT_LOGGER_NAME).addAppender(EVENT_APPENDER);
loggerContext.getLogger(Logger.ROOT_LOGGER_NAME).setLevel(Level.ERROR);

}

@AfterEach
void tearDown() {
EVENT_APPENDER.list.clear();
}

@Seed(1234L)
@Test
void testWithSeed() {
boolean logFound = false;
try {
Product product = SUT.giveMeBuilder(Product.class)
.set("id", 1000L)
.set("productName", "Book")
.sample();

assertAll(
() -> assertNotNull(product),
() -> assertEquals(2000L, (long)product.getId()),
() -> assertEquals("Computer", product.getProductName())
);
} catch (AssertionError e) {
List<ILoggingEvent> logs = EVENT_APPENDER.list;
logFound = logs.stream()
.anyMatch(event -> event.getFormattedMessage()
.contains("Test Method [testWithSeed] failed with seed: "));
}
assertTrue(logFound, "Expected log message found.");
}

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

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

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

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

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

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

then(firstSet).isNotEqualTo(secondList);
}

@Data
private static class Product {
@NotNull
private Long id;
@NotBlank
private String productName;
}
}

0 comments on commit c9a1583

Please sign in to comment.