Last active
August 29, 2015 13:57
-
-
Save yangwansu/9501315 to your computer and use it in GitHub Desktop.
크게 두가지 개선점 이 있음…. 첫번째 기존버전 TestMain 에 있던 주절주절하던 test 메서드를 테스트 갯수만큼 쓰거나 설명문구를 파라메터로 받지 않아도 되게 변경 필요 테스트에 대한 정의는 TestClass 에서 이미 하고 있기에 TestRunner 를 생성시 넘겨주는 TestClass 와 TestRunner.test(String message, String testMethodName) 에서 중복이 발생 TestRunner.test(String message, String testMethodName) - >TestRunner.test( ) 변경 이를 통해 TestRunner 에게 TestClass 만 넘…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.springapp.mvc; | |
public class AssertException extends RuntimeException { | |
public AssertException(String message) { | |
super(message); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.springapp.mvc; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
@Retention(RetentionPolicy.RUNTIME) | |
@Target(ElementType.METHOD) | |
public @interface Description { | |
String value() default ""; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
public Test(Class<?> testClass, Method method) { | |
this.testClass = testClass; | |
this.method=method; | |
} | |
public void execute() { | |
try { | |
Description description = method.getAnnotation(Description.class); | |
if(null != description){ | |
showMessage(description.value()); | |
} | |
method.invoke(testClass.newInstance()); | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
} catch (InvocationTargetException e) { | |
e.printStackTrace(); | |
} catch (InstantiationException e) { | |
e.printStackTrace(); | |
} | |
} | |
private static void showMessage(final String message){ | |
line(); | |
err.println(message); | |
line(); | |
} | |
private static void line() { | |
err.println("=========================================="); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.springapp.mvc; | |
public class TestMain { | |
public 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.test(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.springapp.mvc; | |
import java.lang.reflect.Method; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class TestRunner { | |
private final Class<?> testClass; | |
private final List<Test> tests; | |
public TestRunner(Class<?> testClass) { | |
this.testClass = testClass ; | |
this.tests = extractTestMethod(); | |
} | |
private List<Test> extractTestMethod() { | |
List<Test> tests = new ArrayList<Test>(); | |
for (Method each : this.testClass.getMethods()) { | |
if (isTestMehtod(each)) { | |
tests.add(new Test(testClass,each)); | |
} | |
} | |
return tests; | |
} | |
public void test() { | |
try{ | |
for (Test test : tests) { | |
test.execute(); | |
} | |
}catch(Exception e){ | |
e.printStackTrace(); | |
} | |
} | |
private boolean isTestMehtod(Method method) { | |
return method.getName().startsWith("test") | |
|| method.getReturnType().equals("void"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
크게 두가지 개선점 이 있음….
첫번째 기존버전 TestMain 에 있던 주절주절하던 test 메서드를 테스트 갯수만큼 쓰거나 설명문구를 파라메터로 받지 않아도 되게 변경 필요
테스트에 대한 정의는 TestClass 에서 이미 하고 있기에 TestRunner 를 생성시 넘겨주는 TestClass 와 TestRunner.test(String message, String testMethodName) 에서 중복이 발생
TestRunner.test(String message, String testMethodName) - >TestRunner.test( ) 변경
이를 통해 TestRunner 에게 TestClass 만 넘겨주면 됨 그리고 TestRunner.test( ) 한번만 호출하면 됨
두번째 현재 피드백이 실패시에 예외에 대해서 프린트하는것이 그치고 있음
이번에는 첫번째 만
이 전 버전 들에 비해 도약이 컸음.. 아직도 코드에 대한 테스트가 없는게 걸림..ㅜㅜ