Skip to content

Instantly share code, notes, and snippets.

@yangwansu
Created March 11, 2014 05:44
Show Gist options
  • Save yangwansu/9480092 to your computer and use it in GitHub Desktop.
Save yangwansu/9480092 to your computer and use it in GitHub Desktop.
TestRunner#test 의 두번째 파라메터에 TestClass 의 인스턴스를 넘기는 부분은 초기 객체 생성자에 넘기는 class 로 해결 가능 데이터의 중복
package com.springapp.mvc;
import static com.google.common.base.Preconditions.*;
public class GuavaTest1 {
private final boolean initialzed = false;
public void testForNonNullArgument(final String parameter)
{
final String localPrameter = checkNotNull(parameter, "null값은"
+ "허용되지 않습니다.");
}
public void testDivisorNotZero(final int divisor){
checkArgument(divisor != 0, "0으로 나눌 수 없습니다.");
}
public void testArrayElement(final String[] strArray,
final int indexNumber){
final int index = checkElementIndex(indexNumber, strArray.length,
"지정된 Array 요소 위치가 벗어났습니다.");
}
public void testarrayPosition(final String[] strarray, final int indexNumber){
final int index = checkPositionIndex(indexNumber, strarray.length,
"지정된 Array 요소 위치가 벗어났습니다.");
}
public void testState(){
checkState(this.initialzed, "초기화 되지 않았습니다.");
}
}
package com.springapp.mvc;
public class TestMain {
public static void main(String[] args) {
TestRunner testRunner =new TestRunner(GuavaTest1.class);
testRunner.test("preconditions.checkNotNull", "testArrayElement", new String[]{"Dustin", "java"}, 3);
testRunner.test("precondition.checkArgument", "testDivisorNotZero", 0);
testRunner.test("Preconditions.checkElementIndex", "testForNonNullArgument", null);
testRunner.test("Preconditions.checkPositionIndex", "testarrayPosition", new String[]{"Dustin", "java"}, 3);
testRunner.test("Preconditions.checkState", "testState");
}
}
package com.springapp.mvc;
import java.lang.reflect.InvocationTargetException;
public interface TestMethodInvoker {
void invoker(Object testInstance, String testName, Object[] parameters) throws InvocationTargetException, IllegalAccessException;
}
package com.springapp.mvc;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import static java.lang.System.err;
public class TestRunner {
private final Class<?> testClass;
public TestRunner(Class<?> testClass) {
this.testClass = testClass ;
}
public void test(String message, String testName,Object... parameters) {
printHeader(message);
try{
TestMethodInvoker testcase = new TestMethodInvoker() {
@Override
public void invoker(Object testInstance, String testName, Object[] parmeters) throws InvocationTargetException, IllegalAccessException {
for (Method each : testClass.getMethods()) {
if (
isTestMehtod(each)) {
//each.
if (each.getName().equals(testName)) {
if (null == parmeters) {
each.invoke(testInstance);
} else {
each.invoke(testInstance, parmeters);
}
}
}
}
}
};
testcase.invoker(testClass.newInstance(), testName, parameters);
}catch(Exception e){
e.printStackTrace();
}
}
private boolean isTestMehtod(Method each) {
return each.getName().startsWith("test")
|| each.getReturnType().equals("void");
}
private void printHeader(final String newHeaderText){
line();
err.println(newHeaderText);
line();
}
private static void line() {
err.println("\n==========================================");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment