熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Junt4 動態代碼測試

2013-11-23 20:19:08  來源: Java開源技術 

  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());


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28289.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.