posts - 32,comments - 8,trackbacks - 0
1. 在src目录下面添加文件:HelloWorldTest.java
public class HelloWorldTest extends junit.framework.TestCase {

    
public void testNothing() {
    }
    
    
public void testWillAlwaysFail() {
        fail(
"An error message");
    }
    
}

2.在lib目录下面添加junit.jar类

3.修改build.xml文件如下:
<project name="HelloWorld" basedir="." default="main">

    
<property name="src.dir" value="src" />

    
<property name="build.dir" value="build" />
    
<property name="classes.dir" value="${build.dir}/classes" />
    
<property name="jar.dir" value="${build.dir}/jar" />
    
<property name="lib.dir" value="lib" />
    
<path id="classpath">
        
<fileset dir="${lib.dir}" includes="**/*.jar" />
    
</path>

    
<property name="main-class" value="oata.HelloWorld" />



    
<target name="clean">
        
<delete dir="${build.dir}" />
    
</target>

    
<target name="compile">
        
<mkdir dir="${classes.dir}" />
        
<javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" />
        
<copy todir="${classes.dir}">
            
<fileset dir="${src.dir}" excludes="**/*.java" />
        
</copy>

    
</target>

    
<target name="jar" depends="compile">
        
<mkdir dir="${jar.dir}" />
        
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
            
<manifest>
                
<attribute name="Main-Class" value="${main-class}" />
            
</manifest>
        
</jar>
    
</target>

    
<target name="run" depends="jar">
        
<java fork="true" classname="${main-class}">
            
<classpath>
                
<path refid="classpath" />
                
<path id="application" location="${jar.dir}/${ant.project.name}.jar" />
            
</classpath>
        
</java>

    
</target>
    
<target name="junit" depends="jar">
        
<junit printsummary="yes">
            
<classpath>
                
<path refid="classpath" />
                
<path refid="application" />
            
</classpath>

            
<batchtest fork="yes">
                
<fileset dir="${src.dir}" includes="*Test.java" />
            
</batchtest>
        
</junit>
    
</target>

    
<target name="clean-build" depends="clean,jar" />

    
<target name="main" depends="clean,run" />

</project>


注:修改地方如下:
    

    
<target name="run" depends="jar">
        
<java fork="true" classname="${main-class}">
            
<classpath>
                
<path refid="classpath"/>
                
<path id="application" location="${jar.dir}/${ant.project.name}.jar"/>
            
</classpath>
        
</java>
    
</target>
    
    
<target name="junit" depends="jar">
        
<junit printsummary="yes">
            
<classpath>
                
<path refid="classpath"/>
                
<path refid="application"/>
            
</classpath>
            
            
<batchtest fork="yes">
                
<fileset dir="${src.dir}" includes="*Test.java"/>
            
</batchtest>
        
</junit>
    
</target>

    



6运行,得到结果:
...
junit:
    [junit] Running HelloWorldTest
    [junit] Tests run: 2, Failures: 1, Errors: 0, Time elapsed: 0,01 sec
    [junit] Test HelloWorldTest FAILED

BUILD SUCCESSFUL
...
posted on 2008-12-04 15:03 张辰 阅读(253) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: