Skip to content

Commit

Permalink
fix: 각 테스트가 독립적으로 실행되지 않는 오류 해결 (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
PENEKhun committed Feb 17, 2024
1 parent f7aa2c1 commit eb071ce
Showing 1 changed file with 49 additions and 3 deletions.
52 changes: 49 additions & 3 deletions src/main/resources/code_sample/TestHelper.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
* 이 테스트코드는 <a href="https://github.com/PENEKhun/Baekjoon-java-starter">Baekjoon-java-starter </a>를
* 사용하여 생성되었습니다.
* 이 테스트코드는 <a href="https://github.com/PENEKhun/Baekjoon-java-starter">Baekjoon-java-starter </a>를 사용하여
* 생성되었습니다.
*
* @Author : PENEKhun
*/
public class TestHelper {

private static final HashMap<Field, Object> initialStates = new HashMap<>();

public static void main(String[] args) {
TestCase[] testCases = new TestCase[]{
captureInitialState();

TestCase[] testCases = new TestCase[] {
// {{test_case}}
};

int passedCases = 0;

for (int i = 0; i < testCases.length; i++) {
resetToInitialState();
TestCase testCase = testCases[i];

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Expand Down Expand Up @@ -54,6 +64,42 @@ public static void main(String[] args) {
}
}

private static void captureInitialState() {
try {
Class<?> clazz = Main.class;
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (Modifier.isStatic(field.getModifiers())) {
field.setAccessible(true);
initialStates.put(field, field.get(null));
}
}
} catch (Exception e) {
System.out.println(red("Main 클래스에 접근할 수 없습니다."));
}
}

private static void resetToInitialState() {
try {
for (Map.Entry<Field, Object> entry : initialStates.entrySet()) {
Field field = entry.getKey();
field.setAccessible(true);
Object value = entry.getValue();
if (value instanceof Collection) {
((Collection<?>) value).clear();
} else if (value instanceof Map) {
((Map<?, ?>) value).clear();
} else if (value instanceof StringBuilder) {
((StringBuilder) value).setLength(0);
} else {
field.set(null, value);
}
}
} catch (IllegalAccessException e) {
System.out.println(red("Main 클래스에 접근할 수 없습니다."));
}
}

public static void printFail(int caseNumber, TestCase testCase, String message) {
System.out.printf("""
====== %s ======
Expand Down

0 comments on commit eb071ce

Please sign in to comment.