Skip to content

Instantly share code, notes, and snippets.

@yangwansu
Created March 11, 2014 04:51
Show Gist options
  • Save yangwansu/9479683 to your computer and use it in GitHub Desktop.
Save yangwansu/9479683 to your computer and use it in GitHub Desktop.
TestCase 라는 인터페이스의 의도를 드러내도록 메소드 명 변경
package com.springapp.mvc;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import static com.google.common.base.Preconditions.*;
import static java.lang.System.err;
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, "초기화 되지 않았습니다.");
}
public static void printHeader(final String newHeaderText){
err.println("\n=====================");
err.println("===" + newHeaderText);
err.println("=======================");
}
public static void main(String[] args) {
GuavaTest1 testInstance = new GuavaTest1();
test("preconditions.checkNotNull", testInstance, "testArrayElement",new String[]{"Dustin", "java"}, 3);
test("precondition.checkArgument", testInstance,"testDivisorNotZero",0);
test("Preconditions.checkElementIndex", testInstance,"testForNonNullArgument",null);
test("Preconditions.checkPositionIndex", testInstance,"testarrayPosition", new String[]{"Dustin", "java"}, 3);
test("Preconditions.checkState", testInstance,"testState");
}
private static void test(String message, Object testInstance, 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 : GuavaTest1.class.getMethods()) {
if (
each.getName().startsWith("test")
|| each.getReturnType().equals("void")) {
//each.
if (each.getName().equals(testName)) {
if (null == parmeters) {
each.invoke(testInstance);
} else {
each.invoke(testInstance, parmeters);
}
}
}
}
}
};
testcase.invoker(testInstance, testName, parameters);
}catch(Exception e){
e.printStackTrace();
}
}
}
package com.springapp.mvc;
import java.lang.reflect.InvocationTargetException;
public interface TestMethodInvoker {
void invoker(Object testInstance, String testName, Object[] parameters) throws InvocationTargetException, IllegalAccessException;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment