Skip to content

Instantly share code, notes, and snippets.

@yangwansu
Last active August 29, 2015 13:57
Show Gist options
  • Save yangwansu/9501944 to your computer and use it in GitHub Desktop.
Save yangwansu/9501944 to your computer and use it in GitHub Desktop.
열라 심풀 테스트프레임웍.
package com.springapp.mvc;
public class Assert {
public static void isNotNull(Object expected,String message) {
if(null == expected){
throw new AssertException(message);
}
}
public static void isTrue(boolean expr) {
isTrue(expr,"is not true");
}
public static void isTrue(boolean expr, String message) {
if(false == expr){
throw new AssertException(message);
}
}
public static void assertEquals(int expected, int got) {
isTrue(expected== got,String.format("%s != %s",expected, got));
}
}
package com.springapp.mvc;
public class AssertException extends RuntimeException {
public AssertException(String message) {
super(message);
}
}
package com.springapp.mvc;
public class Reporter {
public void write(Test test) {
System.out.println(String.format("[%5s] %s",test.getStatus(),test.getDescription()));
}
}
package com.springapp.mvc;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import static java.lang.System.err;
public class Test {
private final Method method;
private final Class<?> testClass;
private TestStatus status=TestStatus.READY;
public Test(Class<?> testClass, Method method) {
this.testClass = testClass;
this.method=method;
}
public void execute() {
try {
status = TestStatus.ING;
method.invoke(testClass.newInstance());
status = TestStatus.GREEN;
} catch (IllegalAccessException e) {
//e.printStackTrace();
status = TestStatus.RED;
} catch (InvocationTargetException e) {
//e.printStackTrace();
status = TestStatus.RED;
} catch (InstantiationException e) {
//e.printStackTrace();
status = TestStatus.RED;
}
}
public String getDescription(){
Description description = method.getAnnotation(Description.class);
return description.value();
}
public TestStatus getStatus(){
return status;
}
}
package com.springapp.mvc;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class TestExtractor {
public List<Test> extract(Class<?> testClass) {
List<Test> tests = new ArrayList<Test>();
for (Method each : testClass.getMethods()) {
if (isTestMehtod(each)) {
tests.add(new Test(testClass,each));
}
}
return tests;
}
private boolean isTestMehtod(Method method) {
final String TEST_PREFIX = "test";
final String TEST_RETURN_TYPE = "void";
return method.getName().startsWith(TEST_PREFIX)
|| method.getReturnType().equals(TEST_RETURN_TYPE);
}
}
package com.springapp.mvc;
public class TestMain {
public static class FooTest {
@Description("설명이거들랑 0 ")
public void testForNonNullArgument(){
Assert.isNotNull(null,"null값은 허용되지 않습니다.");
}
@Description("설명이거들랑 1 ")
public void testDivisorNotZero(){
Assert.isTrue(0!= 0,"0으로 나눌 수 없습니다.");
}
@Description("설명이거들랑 2 ")
public void testArrayElement(){
Assert.isTrue( 0 <= 3 && new String[]{"Dustin", "java"}.length > 3 ,"지정된 Array 요소 위치가 벗어났습니다.");
}
@Description("설명이거들랑 3 ")
public void testArrayPosition(){
Assert.isTrue( 0 <= 3 && new String[]{"Dustin", "java"}.length > 3 ,"지정된 Array 요소 위치가 벗어났습니다.");
}
@Description("설명이거들랑 4 ")
public void testState(){
Assert.isTrue(false, "반드시 true ");
}
}
public static void main(String[] args) {
TestRunner testRunner =new TestRunner(FooTest.class);
testRunner.run();
}
}
package com.springapp.mvc;
import java.util.List;
public class TestRunner {
private final List<Test> tests;
private Reporter reporter =new Reporter();
public TestRunner(Class<?> testClass) {
this.tests = new TestExtractor().extract(testClass);
}
public void run() {
try{
for (Test test : tests) {
test.execute();
reporter.write(test);
}
}catch(Exception e){
e.printStackTrace();
}
}
}
package com.springapp.mvc;
public enum TestStatus {
READY,ING,GREEN,RED
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment