Junt 动态代码测试 方便与java在线教程编写 Listenerjava import javautilHashMap; import orgjunitrunnerDescription; import orgjunitrunnerResult; import orgjunitrunnernotificationFailure; import orgjunitrunnernotificationRunListener; public class Listener extends RunListener { public static long begin_mill; public static long end_mill; public static long test_begin_mill; public static long test_end_mill; public static HashMap<String Boolean> test_map; @Override public void testAssumptionFailure(Failure failure) { // TODO Autogenerated method stub supertestAssumptionFailure(failure); } @Override public void testFailure(Failure failure) throws Exception { // TODO Autogenerated method stub supertestFailure(failure); Description description = failuregetDescription(); TestDescription text_doc = descriptiongetAnnotation(TestDescriptionclass); String name = descriptiongetMethodName(); Systemoutprintln(this); test_mapput(name false); // Systemoutprintln(test + name + ( + text_docvalue() + ) +is failure); } @Override public void testFinished(Description description) throws Exception { // TODO Autogenerated method stub supertestFinished(description); String name = descriptiongetMethodName(); TestDescription text_doc = descriptiongetAnnotation(TestDescriptionclass); Boolean value = test_mapget(name); if(value == null){ test_mapput(name true); } Systemoutprintln(this); // Systemoutprintln(test + name + ( + text_docvalue() + ) +is finished); } @Override public void testIgnored(Description description) throws Exception { // TODO Autogenerated method stub supertestIgnored(description); } @Override public void testRunFinished(Result result) throws Exception { // TODO Autogenerated method stub supertestRunFinished(result); // Systemoutprintln(all time + (end_mill begin_mill)); } @Override public void testRunStarted(Description description) throws Exception { // TODO Autogenerated method stub supertestRunStarted(description); begin_mill = SystemcurrentTimeMillis(); test_map = new HashMap<String Boolean>(); } @Override public void testStarted(Description description) throws Exception { // TODO Autogenerated method stub supertestStarted(description); String name = descriptiongetMethodName(); // Systemoutprintln(test + name + is started); } } TestDescriptionjava import static javalangannotationElementTypeMETHOD; import static javalangannotationRetentionPolicyRUNTIME; import javalangannotationRetention; import javalangannotationTarget; @Target({METHOD}) @Retention(RUNTIME) public @interface TestDescription { String value(); } rule: Practicejava public class Practice { public int sum(int a int b){ return a + b; } } PracticeTestjava import orgjunitAssert; import orgjunitTest; import orgjunitrunnerRunWith; import orgjunitrunnersJUnit; @RunWith(JUnitclass) public class PracticeTest { @Test @TestDescription(sum( ) > ) public void test() { Practice a = new Practice(); AssertassertEquals(asum( )); } @Test @TestDescription(sum( ) > ) public void test() { Practice a = new Practice(); AssertassertEquals(asum( )); } @Test @TestDescription(sum( ) > ) public void test() { Practice a = new Practice(); AssertassertEquals(asum( )); } @Test @TestDescription(sum( ) > ) public void test() { Practice a = new Practice(); AssertassertEquals(asum( )); } } Mainjava import javautilIterator; import javautilSet; import orgjunitrunnerJUnitCore; import orgjunitrunnerResult; public class Main { /** * @param args */ public static void main(String[] args) { // TODO Autogenerated method stub JUnitCore core = new orgjunitrunnerJUnitCore(); coreaddListener(new Listener()); Result result = corerun(PracticeTestclass); Set<String> set = Listenertest_mapkeySet(); Iterator<String> it = erator(); while(ithasNext()){ String name = itnext(); boolean is_success = Listenertest_mapget(name); String is_success_str = is_success ? success : failure; Systemoutprintln(test + name + : + is_success_str ); } Systemoutprintln(resultwasSuccessful()); |