Skip to content

Instantly share code, notes, and snippets.

@horitaku1124
Last active March 9, 2019 03:39
Show Gist options
  • Save horitaku1124/54ae05a213ca25d51265ca6e39dad0dc to your computer and use it in GitHub Desktop.
Save horitaku1124/54ae05a213ca25d51265ca6e39dad0dc to your computer and use it in GitHub Desktop.
import org.junit.internal.runners.InitializationError;
import org.junit.internal.runners.JUnit4ClassRunner;
public class CustomRunner extends JUnit4ClassRunner {
public CustomRunner(Class<?> klass) throws InitializationError {
super(klass);
}
protected void validate() throws InitializationError {
// ignore
}
}
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.AfterClass;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class TestRunner {
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_BLACK = "\u001B[30m";
public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_YELLOW = "\u001B[33m";
public static final String ANSI_BLUE = "\u001B[34m";
public static final String ANSI_PURPLE = "\u001B[35m";
public static final String ANSI_CYAN = "\u001B[36m";
public static final String ANSI_WHITE = "\u001B[37m";
private static final String OK = " --- " + ANSI_GREEN + "ok" + ANSI_RESET;
private static final String NG = " --- " + ANSI_RED + "ng" + ANSI_RESET;
public static void main(String[] args) {
final Class[] tests = {
TestTest.class, TestTest.class
};
StringBuilder result = new StringBuilder();
int successCount = 0;
for (Class c:tests) {
String className = c.getCanonicalName();
try {
check(c);
result.append(className + OK);
successCount++;
} catch (Exception e) {
e.printStackTrace();
result.append(className + NG);
}
result.append("\n");
}
System.out.println(result.toString());
System.out.println(successCount + " / " + tests.length + " tests succeed.");
}
private static void check(Class target) throws InvocationTargetException, IllegalAccessException, InstantiationException {
Method beforeClass = null;
Method afterClass = null;
Method before = null;
Method after = null;
List<Method> tests = new ArrayList<>();
System.out.println(target.getCanonicalName());
// Field f[] = c.getFields();
// for (int i = 0; i < f.length; i++) {
// System.out.println(f[i]);
// }
Method[] methods = target.getMethods();
for (int i = 0; i < methods.length; i++) {
Method m = methods[i];
Test testAnnotation = m.getAnnotation(Test.class);
if (testAnnotation != null) {
tests.add(m);
}
Before beforeAnnotation = m.getAnnotation(Before.class);
if (beforeAnnotation != null) {
before = m;
}
After afterAnnotation = m.getAnnotation(After.class);
if (afterAnnotation != null) {
after = m;
}
BeforeClass beforeClassAnnotation = m.getAnnotation(BeforeClass.class);
if (beforeClassAnnotation != null) {
beforeClass = m;
}
AfterClass afterClassAnnotation = m.getAnnotation(AfterClass.class);
if (afterClassAnnotation != null) {
afterClass = m;
}
// System.out.println(methods[i]);
}
// Run
if (beforeClass != null) {
beforeClass.invoke(null);
}
for (Method method: tests) {
Object testTarget = target.newInstance();
if (before != null) {
before.invoke(testTarget);
}
method.invoke(testTarget);
if (after != null) {
after.invoke(testTarget);
}
}
if (afterClass != null) {
afterClass.invoke(null);
}
}
}
import org.junit.*;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public class TestTest {
private Integer target;
@BeforeClass
public static void bc() {
System.out.println(" -- BeforeClass --");
}
@Before
public void b() {
target = Integer.valueOf(100);
System.out.println(" -- Before --");
}
@After
public void a() {
System.out.println(" -- After --");
}
@Test
public void test1() {
target = target.intValue() + 10;
System.out.println(" -- test1 --");
assertThat(target.intValue(), is(110));
}
@Test
public void test2() {
target = target.intValue() + 20;
System.out.println(" -- test2 --");
assertThat(target.intValue(), is(120));
}
@Ignore
@Test
public void test3() {
target = target.intValue() + 30;
System.out.println(" -- test3 --");
System.out.println(target.intValue());
assertThat(target.intValue(), is(120));
}
@AfterClass
public static void ac() {
System.out.println(" -- AfterClass --");
}
}
import org.junit.Test;
import org.junit.internal.runners.InitializationError;
import org.junit.internal.runners.JUnit4ClassRunner;
import org.junit.runner.RunWith;
@RunWith(CustomRunner.class)
public class TestTest2 {
@Test
public TestResult test1() {
System.out.println("ok");
return new TestResult();
}
class TestResult {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment