Ant 和 Junit 的复用

来源:互联网 发布:淘宝卖假货 编辑:程序博客网 时间:2024/04/30 18:04

 

Ant Junit 的复用:

//基本类,实现在一个字符数组中查找一个字符,并返回这个字符在字符数组中的位置。
package cn.itcast;
public class FindChar
{
     int search(char [] chars, char ch) throws IllegalArgumentException
     {
              //throw new UnsupportedOperationException("search");
              if(chars == null)
              {
                       throw new IllegalArgumentException("");
              }
             
              for(int i=0; i<chars.length; i++)
              {
                       if(chars[i] == ch)
                       {
                                 return i;
                       }
              }
              return -1;
     }
}
 
      //测试类
package cn.itcast;
import junit.framework.TestCase;
 
public class TestFindCharTwo extends TestCase
{
     private FindChar fc = null;
     private char [] chars = {'a','b','c','d'};
    
     public void setUp()
     {
              fc = new FindChar();
     }
    
     public void testSearchFound()
     {
              int index = fc.search(chars,'c');
              assertEquals(index,2);
     }
     public void testSearchNotFound()
     {
              int index = fc.search(chars,'e');
              assertTrue((index==-1));
     }
     public void testSearchIllegalArguments()
     {
              char [] chs = null;
              fc.search(chs,'a');
              fail();
     }
}
 
      //build.xml文件编码如下
<project name = "testFindChar" default = "test2">
    
     <property name = "src.java.dir" value = "src/java" />
     <property name = "src.test.dir" value = "src/test" />
     <property name = "class.java.dir" value = "classes/java" />
     <property name = "class.test.dir" value = "classes/test" />
     <property file = "myproperty.properties" />
    
     <target name = "init">
              <mkdir dir = "classes/java" />
              <mkdir dir = "classes/test" />
              <mkdir dir = "reports" />
     </target>
    
     <target name = "compileJava" depends = "init">
              <javac srcdir="${src.java.dir}/cn/itcast" destdir="${class.java.dir}" />
     </target>
    
     <target name = "compileTest" depends = "init">
              <javac srcdir = "${src.test.dir}/cn/itcast" destdir = "${class.test.dir}" >
                       <classpath>
                                 <pathelement location="${class.java.dir}" />
                       </classpath>
              </javac>
     </target>
    
     <target name = "compile" depends = "compileJava,compileTest" />
    
     <target name = "clear">
              <delete dir="${class.test.dir}" />
              <delete dir="${class.java.dir}" />
     </target>
    
     <target name="test" depends="clear,compile">
              <junit fork="true" printsummary="on" >
                      
                       <classpath>
                                 <pathelement location="${class.java.dir}/cn/itcast" />
                                 <pathelement location="${class.test.dir}/cn/itcast" />
                                 <!--pathelement location="C:/junit3.8.1/junit.jar" /-->
                       </classpath>
                       <batchtest fork="yes" todir="reports">
                                 <fileset dir="${class.test.dir}">
                                          <include name="**/*Test*.class" />
                                          <exclude name="**/AllTests.class" />
                                 </fileset>
                       </batchtest>
                       <formatter type="brief" usefile="false" />
                       <formatter type="xml" />
                       <formatter type="plain" />
              </junit>
     </target>
    
     <target name="test2" depends="test">
              <junitreport todir=".">                  
                       <fileset dir="${reports}">
                                 <include name="TEST-*.xml"/>
           </fileset>
           <report format="frames" todir="${reports}"/>                          
              </junitreport>
     </target>   
</project>
 
//myproperty.properties文件输入参数如下:
reports=reports
 
//输出结果:
 
             //index.html文件中的输出结果如下:
 
原创粉丝点击