Skip to content

Instantly share code, notes, and snippets.

@yangwansu
Created March 12, 2014 03:31
Show Gist options
  • Save yangwansu/9500299 to your computer and use it in GitHub Desktop.
Save yangwansu/9500299 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, String message) {
if(false == expr){
throw new AssertException(message);
}
}
}
package com.springapp.mvc;
public class AssertException extends RuntimeException {
public AssertException(String message) {
super(message);
}
}
package com.springapp.mvc;
public class GuavaTest1 {
public void testForNonNullArgument(){
Assert.isNotNull(null,"null값은 허용되지 않습니다.");
}
public void testDivisorNotZero(){
Assert.isTrue(0!= 0,"0으로 나눌 수 없습니다.");
}
public void testArrayElement(){
Assert.isTrue( 0 <= 3 && new String[]{"Dustin", "java"}.length > 3 ,"지정된 Array 요소 위치가 벗어났습니다.");
}
public void testArrayPosition(){
Assert.isTrue( 0 <= 3 && new String[]{"Dustin", "java"}.length > 3 ,"지정된 Array 요소 위치가 벗어났습니다.");
}
public void testState(){
Assert.isTrue(false, "반드시 true ");
}
}
package com.springapp.mvc;
public class TestMain {
public static void main(String[] args) {
TestRunner testRunner =new TestRunner(GuavaTest1.class);
testRunner.test("preconditions.checkNotNull", "testArrayElement");
testRunner.test("precondition.checkArgument", "testDivisorNotZero");
testRunner.test("Preconditions.checkElementIndex", "testForNonNullArgument");
testRunner.test("Preconditions.checkPositionIndex", "testArrayPosition");
testRunner.test("Preconditions.checkState", "testState");
}
}
package com.springapp.mvc;
import java.lang.reflect.InvocationTargetException;
public interface TestMethodInvoker {
void invoker(Object testInstance, String testName) 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) {
printHeader(message);
try{
TestMethodInvoker testcase = new TestMethodInvoker() {
@Override
public void invoker(Object testInstance, String testName) throws InvocationTargetException, IllegalAccessException {
for (Method each : testClass.getMethods()) {
if (
isTestMehtod(each)) {
//each.
if (each.getName().equals(testName)) {
each.invoke(testInstance);
}
}
}
}
};
testcase.invoker(testClass.newInstance(), testName);
}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("==========================================");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment