ant+junit

目录结构


D:\code\kongee\ant01>tree /f
卷 软件 的文件夹 PATH 列表
卷序列号为 000D-CD8E
D:.
│  .classpath
│  .project
│  build.properties
│  build.xml
│
├─.settings
│      org.eclipse.jdt.core.prefs
│
├─bin
│  └─com
│      └─laolang
│          ├─cal
│          │      Cal.class
│          │      ICal.class
│          │      TestCal.class
│          │
│          ├─dao
│          │      StudentDaoImpl.class
│          │      TestStudentDao.class
│          │
│          ├─main
│          │      Main.class
│          │
│          └─modle
│                  Student.class
│
├─lib
│      hamcrest-all-1.3.jar
│      hamcrest-core-1.3.jar
│      junit-4.11.jar
│
├─src
│  └─com
│      └─laolang
│          ├─cal
│          │      Cal.java
│          │      ICal.java
│          │
│          ├─dao
│          │      StudentDaoImpl.java
│          │
│          ├─main
│          │      Main.java
│          │
│          └─modle
│                  Student.java
│
└─test
    └─com
        └─laolang
            ├─cal
            │      TestCal.java
            │
            └─dao
                    TestStudentDao.java


D:\code\kongee\ant01>




src

com.laolang.ca.ICal


package com.laolang.cal;

/**
 * 计算接口
 */
public interface ICal {

	/**
	 * 加
	 *
	 * @param a
	 *            the a
	 * @param b
	 *            the b
	 * @return a与b之和
	 */
	public int add(int a, int b);

	/**
	 * 减
	 *
	 * @param a
	 *            the a
	 * @param b
	 *            the b
	 * @return a与b之差
	 */
	public int minus(int a, int b);

	/**
	 * 乘
	 *
	 * @param a
	 *            the a
	 * @param b
	 *            the b
	 * @return a与b之积
	 */
	public int mult(int a, int b);

	/**
	 * 除
	 *
	 * @param a
	 *            the a
	 * @param b
	 *            the b
	 * @return a与b之商
	 */
	public int divide(int a, int b);
}
com.laolang.cal.Cal



package com.laolang.cal;

/**
 * 计算实现
 */
public class Cal implements ICal {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.laolang.cal.ICal#add(int, int)
	 */
	@Override
	public int add(int a, int b) {
		return a + b;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.laolang.cal.ICal#minus(int, int)
	 */
	@Override
	public int minus(int a, int b) {
		return a - b;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.laolang.cal.ICal#mult(int, int)
	 */
	@Override
	public int mult(int a, int b) {
		return a * b;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.laolang.cal.ICal#divide(int, int)
	 */
	@Override
	public int divide(int a, int b) {
		if (0 != b) {
			return a / b;
		} else {
			return 0;
		}

	}

}
com.laolang.dao.StudentDaoImpl



package com.laolang.dao;

/**
 * The Class StudentDaoImpl.
 */
public class StudentDaoImpl {

	/**
	 * Instantiates a new student dao impl.
	 */
	public StudentDaoImpl() {
		super();
	}

	/**
	 * Instantiates a new student dao impl.
	 *
	 * @param str
	 *            the str
	 */
	public StudentDaoImpl(String str) {
		super();
		this.str = str;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "StudentDaoImpl [str=" + str + "]";
	}

	/**
	 * Gets the str.
	 *
	 * @return the str
	 */
	public String getStr() {
		return str;
	}

	/**
	 * Sets the str.
	 *
	 * @param str
	 *            the new str
	 */
	public void setStr(String str) {
		this.str = str;
	}

	/** 字符串 */
	private String str;
}
com.laolang.main.Main



package com.laolang.main;

public class Main {

	public static void main(String[] args) {
		int i = 0;
		for( String str : args ){
			i++;
			System.out.println(i+":"+str);
		}
		System.out.println("Hello World!");
	}
}
com.laolang.modle.Student



package com.laolang.modle;

/**
 * 学生类
 */
public class Student {

	/**
	 * Instantiates a new student.
	 */
	public Student() {
		super();
	}

	/**
	 * Instantiates a new student.
	 *
	 * @param stuName
	 *            the stu name
	 * @param stuAge
	 *            the stu age
	 * @param stuSex
	 *            the stu sex
	 */
	public Student(String stuName, int stuAge, String stuSex) {
		super();
		this.stuName = stuName;
		this.stuAge = stuAge;
		this.stuSex = stuSex;
	}

	/**
	 * Instantiates a new student.
	 *
	 * @param stuId
	 *            the stu id
	 * @param stuName
	 *            the stu name
	 * @param stuAge
	 *            the stu age
	 * @param stuSex
	 *            the stu sex
	 */
	public Student(int stuId, String stuName, int stuAge, String stuSex) {
		super();
		this.stuId = stuId;
		this.stuName = stuName;
		this.stuAge = stuAge;
		this.stuSex = stuSex;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Student [stuId=" + stuId + ", stuName=" + stuName + ", stuAge="
				+ stuAge + ", stuSex=" + stuSex + "]";
	}

	/**
	 * Gets the stu id.
	 *
	 * @return the stu id
	 */
	public int getStuId() {
		return stuId;
	}

	/**
	 * Sets the stu id.
	 *
	 * @param stuId
	 *            the new stu id
	 */
	public void setStuId(int stuId) {
		this.stuId = stuId;
	}

	/**
	 * Gets the stu name.
	 *
	 * @return the stu name
	 */
	public String getStuName() {
		return stuName;
	}

	/**
	 * Sets the stu name.
	 *
	 * @param stuName
	 *            the new stu name
	 */
	public void setStuName(String stuName) {
		this.stuName = stuName;
	}

	/**
	 * Gets the stu age.
	 *
	 * @return the stu age
	 */
	public int getStuAge() {
		return stuAge;
	}

	/**
	 * Sets the stu age.
	 *
	 * @param stuAge
	 *            the new stu age
	 */
	public void setStuAge(int stuAge) {
		this.stuAge = stuAge;
	}

	/**
	 * Gets the stu sex.
	 *
	 * @return the stu sex
	 */
	public String getStuSex() {
		return stuSex;
	}

	/**
	 * Sets the stu sex.
	 *
	 * @param stuSex
	 *            the new stu sex
	 */
	public void setStuSex(String stuSex) {
		this.stuSex = stuSex;
	}

	/** 学号 */
	int stuId;

	/** 姓名 */
	String stuName;

	/** 年龄 */
	int stuAge;

	/** 性别 */
	String stuSex;
}
test


com.laolang.cal.TestCal


package com.laolang.cal;


import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;
import org.junit.Before;
import org.junit.Test;

public class TestCal {
	
	
	@Before
	public void setUp(){
		cal = new Cal();
	}
	
	@Test
    public void testAdd(){
        int result = cal.add(1, 3);
        int real = 6;
         
        assertEquals("加法有问题!",real, result);
         
    }
     
    @Test
    public void testMinus(){
        int result = cal.minus(1, 6);
        int real = -5;
         
        assertEquals("减法有问题!",real, result);
         
    }
     
    @Test
    public void testMult(){
        int result = cal.mult(2, 8);
        int real = 16;
         
        assertEquals("乘法有问题!",real, result);
         
    }
     
    @Test
    public void testDivide(){
        int result = cal.divide(9, 3);
        int real = 3;
         
        assertEquals("除法有问题!",real, result);
         
    }
     
    @Test
    public void testTimeout(){
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
         
        System.out.println("hello world!");
    }
     
    @Test
    public void testHamcrest(){
        assertThat(50,allOf(greaterThan(40),lessThan(60)));
        assertThat(50, greaterThan(40));
         
         
    }

	private ICal cal;
}
com.laolang.dao.TestStudentDao



package com.laolang.dao;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class TestStudentDao {

	@Before
	public void setUp(){
		dao = new StudentDaoImpl();
	}
	
	@Test
	public void testdao(){
		String str = "小代码";
		dao.setStr("小代码");
		assertEquals(dao.getStr(),str );
	}
	
	
	private StudentDaoImpl dao ;
}




ant

build.properties


Main-class=com.laolang.main.Main
Build-By=laolang
jar-name=hello.jar
build.xml



<?xml version="1.0" encoding="UTF-8"?>
<project>

	<!--
		fileset可以设定一组文件集来进行操作,dir指明文件集要进行选择的路径,
		通过id可以指定这个文件的名称,在使用的时候进行直接的引入
		include和exclude可以设定包含返回和排除范围**/*.*所有目录中的所有文件
	-->
	<fileset id="src.path" dir="src">
		<include name="**/*.*" />
		<!--<exclude name="**/*.java"/>-->
	</fileset>

	<fileset id="test.src.path" dir="test">
		<include name="**/*.*" />
		<!--<exclude name="**/*.java"/>-->
	</fileset>

	<!--使用属性定义相应的路径时,一定使用location而不要使用value-->
	<property name="build.dir" location="build" />
	<property name="build.classes" location="${build.dir}/classes" />
	<property name="build.src" location="${build.dir}/src" />
	<property name="build.dist" location="${build.dir}/dist" />
	<property name="build.doc" location="${build.dir}/doc" />
	<property name="build.test.dir" location="${build.dir}/test" />
	<property name="build.test.classes" location="${build.test.dir}/classes" />
	<property name="build.test.report" location="${build.test.dir}/report" />

	<property name="src.dir" location="src" />
	<property name="lib.dir" location="lib" />
	<property name="test.src.dir" location="test" />
	<property name="run.test.class" value="**/Test*.class" />

	<!-- 
		打包、运行的相关信息
	-->
	<property file="build.properties">
	</property>

	<path id="compile-path">
		<fileset dir="${lib.dir}" includes="*.jar">
		</fileset>
	</path>

	<path id="compile-test-path">
		<path refid="compile-path">
		</path>
		<pathelement location="${build.classes}" />
	</path>

	<path id="run-test-path">
		<path refid="compile-test-path">
		</path>
		<pathelement location="${build.test.classes}" />
	</path>

	<target name="init" description="创建必要的文件夹">
		<mkdir dir="${build.dir}" />
		<mkdir dir="${build.src}" />
		<mkdir dir="${build.classes}" />
		<mkdir dir="${build.dist}" />
		<mkdir dir="${build.doc}" />
		<mkdir dir="${build.test.dir}" />
		<mkdir dir="${build.test.classes}" />
		<mkdir dir="${build.test.report}" />
		<echo message="初始化完成!" />
	</target>

	<target name="copySrc" depends="init" description="复制文件">
		<copy todir="build/src">
			<fileset refid="src.path">
			</fileset>
		</copy>
		<echo message="复制文件完成!" />
	</target>

	<target name="compile" depends="copySrc" description="编译">
		<javac destdir="build/classes" srcdir="src" includeantruntime="false">
			<!--给编译器指定编码,防止出现:"警告: 编码 GBK 的不可映射字符"-->
			<compilerarg line="-encoding UTF-8 " />
		</javac>
		<echo message="编译完成!" />
	</target>

	<target name="compile-test" depends="compile">
		<echo>编译测试文件</echo>
		<javac failonerror="true" includeantruntime="true" srcdir="${test.src.dir}" destdir="${build.test.classes}" classpathref="compile-test-path">
			<compilerarg line="-encoding UTF-8 " />
		</javac>
	</target>

	<target name="run-test" depends="compile-test">
		<echo>运行单元测试</echo>
		<junit printsummary="false" haltonfailure="false">
			<classpath refid="run-test-path">
			</classpath>
			<formatter type="brief" usefile="false" />
			<!--<test name="${run.test.class}"></test>-->
			<formatter type="xml" />
			<batchtest todir="${build.test.report}">
				<fileset dir="${build.test.classes}" includes="${run.test.class}">
				</fileset>
			</batchtest>
		</junit>
		<junitreport todir="${build.test.report}">
			<fileset dir="${build.test.report}" includes="TEST-*.xml">
			</fileset>
			<report format="frames" todir="${build.test.report}/html" />
		</junitreport>
	</target>


	<target name="jar" depends="compile" description="打包">
		<jar destfile="${build.dist}/${jar-name}" basedir="${build.classes}">
			<fileset dir="${build.classes}">
				<include name="**/*.class" />
			</fileset>
			<manifest>
				<attribute name="Main-Class" value="${Main-class}" />
				<attribute name="Build-By" value="${Build-By}" />
			</manifest>
		</jar>
	</target>

	<target name="execute" depends="jar" description="运行">
		<echo>基于类路径的classname来完成执行</echo>
		<java classname="${Main-class}" classpath="${build.classes}" fork="true">
			<arg value="张三" />
			<arg value="李四" />
			<arg value="王五" />
		</java>

		<echo>基于jar文件执行</echo>
		<java jar="${build.dist}/${jar-name}" fork="true">

			<arg value="张三" />
			<arg value="李四" />
			<arg value="王五" />
		</java>
	</target>

	<target name="doc" depends="copySrc" description="生成javadoc文档">
		<javadoc sourcepath="${src.dir}" private="true" windowtitle="我的JAVA DOC" use="true" packagenames="com.laolang.*" destdir="${build.doc}" charset="UTF-8" docencoding="UTF-8" encoding="UTF-8">
			<classpath path="${build.classes}">
			</classpath>
		</javadoc>
	</target>

	<target name="clean" description="清理">
		<delete dir="${build.dir}" />
	</target>
</project>


运行效果:



D:\code\kongee\ant01>dir
 驱动器 D 中的卷是 软件
 卷的序列号是 000D-CD8E

 D:\code\kongee\ant01 的目录

2015/11/01  19:23    <DIR>          .
2015/11/01  19:23    <DIR>          ..
2015/11/01  16:58               528 .classpath
2015/11/01  15:08               381 .project
2015/11/01  15:08    <DIR>          .settings
2015/11/01  16:58    <DIR>          bin
2015/11/01  15:46                70 build.properties
2015/11/01  17:31             5,177 build.xml
2015/11/01  16:58    <DIR>          lib
2015/11/01  15:09    <DIR>          src
2015/11/01  16:52    <DIR>          test
               4 个文件          6,156 字节
               7 个目录 107,848,986,624 可用字节

D:\code\kongee\ant01>ant init
Buildfile: D:\code\kongee\ant01\build.xml

init:
    [mkdir] Created dir: D:\code\kongee\ant01\build
    [mkdir] Created dir: D:\code\kongee\ant01\build\src
    [mkdir] Created dir: D:\code\kongee\ant01\build\classes
    [mkdir] Created dir: D:\code\kongee\ant01\build\dist
    [mkdir] Created dir: D:\code\kongee\ant01\build\doc
    [mkdir] Created dir: D:\code\kongee\ant01\build\test
    [mkdir] Created dir: D:\code\kongee\ant01\build\test\classes
    [mkdir] Created dir: D:\code\kongee\ant01\build\test\report
     [echo] 初始化完成!

BUILD SUCCESSFUL
Total time: 2 seconds

D:\code\kongee\ant01>ant run-test
Buildfile: D:\code\kongee\ant01\build.xml

init:
     [echo] 初始化完成!

copySrc:
     [copy] Copying 5 files to D:\code\kongee\ant01\build\src
     [echo] 复制文件完成!

compile:
    [javac] Compiling 5 source files to D:\code\kongee\ant01\build\classes
     [echo] 编译完成!

compile-test:
     [echo] 编译测试文件
    [javac] Compiling 2 source files to D:\code\kongee\ant01\build\test\classes

run-test:
     [echo] 运行单元测试
    [junit] Testsuite: com.laolang.cal.TestCal
    [junit] Tests run: 6, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.507 sec
    [junit]
    [junit] ------------- Standard Output ---------------
    [junit] hello world!
    [junit] ------------- ---------------- ---------------
    [junit] Testcase: testAdd(com.laolang.cal.TestCal): FAILED
    [junit] 加法有问题! expected:<6> but was:<4>
    [junit] junit.framework.AssertionFailedError: 加法有问题! expected:<6> but was:<4>
    [junit]     at com.laolang.cal.TestCal.testAdd(Unknown Source)
    [junit]
    [junit]
    [junit] Test com.laolang.cal.TestCal FAILED
    [junit] Testsuite: com.laolang.dao.TestStudentDao
    [junit] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.024 sec
    [junit]
[junitreport] Processing D:\code\kongee\ant01\build\test\report\TESTS-TestSuites.xml to C:\Users\laolang\AppD
[junitreport] Loading stylesheet jar:file:/D:/program/program/java/ant/lib/ant-junit.jar!/org/apache/tools/an
[junitreport] Transform time: 3086ms
[junitreport] Deleting: C:\Users\laolang\AppData\Local\Temp\null442577048

BUILD SUCCESSFUL
Total time: 11 seconds

D:\code\kongee\ant01>ant doc
Buildfile: D:\code\kongee\ant01\build.xml

init:
     [echo] 初始化完成!

copySrc:
     [echo] 复制文件完成!

doc:
  [javadoc] Generating Javadoc
  [javadoc] Javadoc execution
  [javadoc] 正在加载程序包com.laolang.cal的源文件...
  [javadoc] 正在加载程序包com.laolang.dao的源文件...
  [javadoc] 正在加载程序包com.laolang.main的源文件...
  [javadoc] 正在加载程序包com.laolang.modle的源文件...
  [javadoc] 正在构造 Javadoc 信息...
  [javadoc] 标准 Doclet 版本 1.7.0_67
  [javadoc] 正在构建所有程序包和类的树...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\com\laolang\cal\Cal.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\com\laolang\cal\ICal.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\com\laolang\dao\StudentDaoImpl.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\com\laolang\main\Main.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\com\laolang\modle\Student.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\overview-frame.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\com\laolang\cal\package-frame.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\com\laolang\cal\package-summary.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\com\laolang\cal\package-tree.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\com\laolang\dao\package-frame.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\com\laolang\dao\package-summary.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\com\laolang\dao\package-tree.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\com\laolang\main\package-frame.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\com\laolang\main\package-summary.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\com\laolang\main\package-tree.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\com\laolang\modle\package-frame.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\com\laolang\modle\package-summary.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\com\laolang\modle\package-tree.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\constant-values.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\com\laolang\cal\class-use\ICal.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\com\laolang\cal\class-use\Cal.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\com\laolang\dao\class-use\StudentDaoImpl.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\com\laolang\main\class-use\Main.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\com\laolang\modle\class-use\Student.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\com\laolang\cal\package-use.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\com\laolang\dao\package-use.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\com\laolang\main\package-use.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\com\laolang\modle\package-use.html...
  [javadoc] 正在构建所有程序包和类的索引...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\overview-tree.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\index-all.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\deprecated-list.html...
  [javadoc] 正在构建所有类的索引...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\allclasses-frame.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\allclasses-noframe.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\index.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\overview-summary.html...
  [javadoc] 正在生成D:\code\kongee\ant01\build\doc\help-doc.html...

BUILD SUCCESSFUL
Total time: 9 seconds

D:\code\kongee\ant01>
可以看到其中加法有问题,现在修正错误,再次运行:



D:\code\kongee\ant01>ant run-test
Buildfile: D:\code\kongee\ant01\build.xml

init:
     [echo] 初始化完成!

copySrc:
     [echo] 复制文件完成!

compile:
     [echo] 编译完成!

compile-test:
     [echo] 编译测试文件
    [javac] Compiling 1 source file to D:\code\kongee\ant01\build\test\classes

run-test:
     [echo] 运行单元测试
    [junit] Testsuite: com.laolang.cal.TestCal
    [junit] Tests run: 6, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.47 sec
    [junit]
    [junit] ------------- Standard Output ---------------
    [junit] hello world!
    [junit] ------------- ---------------- ---------------
    [junit] Testsuite: com.laolang.dao.TestStudentDao
    [junit] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.02 sec
    [junit]
[junitreport] Processing D:\code\kongee\ant01\build\test\report\TESTS-TestSuites.xml to C:\Users\laolang\AppData\Local\Temp\null834728945
[junitreport] Loading stylesheet jar:file:/D:/program/program/java/ant/lib/ant-junit.jar!/org/apache/tools/ant/taskdefs/optional/junit/xsl/junit-frames.xsl
[junitreport] Transform time: 2901ms
[junitreport] Deleting: C:\Users\laolang\AppData\Local\Temp\null834728945

BUILD SUCCESSFUL
Total time: 10 seconds

D:\code\kongee\ant01>


html格式报告:


ant+junit_第1张图片

javadoc文档

ant+junit_第2张图片







你可能感兴趣的:(ant+junit)