여기서는 사실 Mock을 쓰거나 SpringBoot를 사용하는 것이 일반적이겠지만 NS TEST라는건 우테코에서 만든 것이었다.
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package camp.nextstep.edu.missionutils.test;
import camp.nextstep.edu.missionutils.DateTimes;
import camp.nextstep.edu.missionutils.Randoms;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.function.Executable;
import org.mockito.ArgumentMatchers;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
public class Assertions {
private static final Duration SIMPLE_TEST_TIMEOUT = Duration.ofSeconds(1L);
private static final Duration RANDOM_TEST_TIMEOUT = Duration.ofSeconds(10L);
private Assertions() {
}
public static void assertSimpleTest(Executable executable) {
org.junit.jupiter.api.Assertions.assertTimeoutPreemptively(SIMPLE_TEST_TIMEOUT, executable);
}
public static void assertRandomNumberInListTest(Executable executable, Integer value, Integer... values) {
assertRandomTest(() -> {
Randoms.pickNumberInList(ArgumentMatchers.anyList());
}, executable, value, values);
}
public static void assertRandomNumberInRangeTest(Executable executable, Integer value, Integer... values) {
assertRandomTest(() -> {
Randoms.pickNumberInRange(ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt());
}, executable, value, values);
}
public static void assertRandomUniqueNumbersInRangeTest(Executable executable, List<Integer> value, List<Integer>... values) {
assertRandomTest(() -> {
Randoms.pickUniqueNumbersInRange(ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt());
}, executable, value, values);
}
public static <T> void assertShuffleTest(Executable executable, List<T> value, List<T>... values) {
assertRandomTest(() -> {
Randoms.shuffle(ArgumentMatchers.anyList());
}, executable, value, values);
}
private static <T> void assertRandomTest(MockedStatic.Verification verification, Executable executable, T value, T... values) {
org.junit.jupiter.api.Assertions.assertTimeoutPreemptively(RANDOM_TEST_TIMEOUT, () -> {
MockedStatic<Randoms> mock = Mockito.mockStatic(Randoms.class);
try {
mock.when(verification).thenReturn(value, Arrays.stream(values).toArray());
executable.execute();
} catch (Throwable var8) {
if (mock != null) {
try {
mock.close();
} catch (Throwable var7) {
var8.addSuppressed(var7);
}
}
throw var8;
}
if (mock != null) {
mock.close();
}
});
}
public static void assertNowTest(Executable executable, LocalDateTime value, LocalDateTime... values) {
org.junit.jupiter.api.Assertions.assertTimeoutPreemptively(RANDOM_TEST_TIMEOUT, () -> {
MockedStatic<DateTimes> mock = Mockito.mockStatic(DateTimes.class);
try {
mock.when(DateTimes::now).thenReturn(value, Arrays.stream(values).toArray());
executable.execute();
} catch (Throwable var7) {
if (mock != null) {
try {
mock.close();
} catch (Throwable var6) {
var7.addSuppressed(var6);
}
}
throw var7;
}
if (mock != null) {
mock.close();
}
});
}
}
<aside> 💡
기본적으로 여기서도 Mock를 사용하는 것을 알 수 있었다. 조금 신기했던 점은 Output Stream을 받아서 test 기능을 만든 것이 신기했다.
</aside>