 |
建站必读 |
 |
|
|
 |
|
 |
|
| |
| 当前位置:首页 -> 建站必读 -> JSP技术 |
|
JUNIT测试通过的HelloWorld |
把我刚测试成功的例子(3分钟前)记录下来放在这里,希望会对刚开始研究Junit的朋友有点帮助。
到jakarta当一份apache-ant安装到你的系统上,并把ant/bin加到系统变量path中,使你的ant命令可以在任何地方被调用。
当一份junit3.8.1安装到你的系统上(解压缩,把junit.jar加到系统变量classpath中)。
建立测试目录JunitTest
把java文件,统一放到JunitTest/src目录,根据package建与之相关的目录.
文件1:
HelloWorld.java
package com.company;
public class HelloWorld {
public String sayHello() {
return "Hello World!";
}
public static void main( String[] args ) {
HelloWorld world = new HelloWorld();
System.out.println(world.sayHello());
}
}
文件2:
AllJunitTests.java
package test.com.company;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class AllJunitTests extends TestCase{
public AllJunitTests(String name){
super(name);
}
public static Test suite(){
TestSuite suite = new TestSuite();
//TestSuite suite = new TestSuite();
suite.addTestSuite(HelloWorldTest.class);
return suite;
}
}
文件3:
HelloWorldTest.java
package test.com.company;
import com.company.HelloWorld;
import junit.framework.TestCase;
import junit.framework.AssertionFailedError;
import junit.framework.Assert;
/**
* JUnit 3.8.1 testcases for HelloWorld
*/
public class HelloWorldTest extends TestCase {
public HelloWorldTest(String name) {
super(name);
}
public static void main(String args[]) {
junit.textui.TestRunner.run(HelloWorldTest.class);
}
public void testSayHello() {
HelloWorld world = new com.company.HelloWorld();
assertEquals("Hello World!", world.sayHello() );
}
}
我的build.xml放到JunitTestuild,构造这个文件才可以使用ant命令来执行,包括编译、打包、测试junit用例
<?xml version="1.0" encoding="UTF-8" ?>
<project name="anita" default="main" basedir="../">
<property name="app.name" value="sayhello" />
<property name="build.dir" value="build/classes" />
<target name="JUNIT">
<available property="junit.present" classname="junit.framework.TestCase" />
</target>
<target name="compile" depends="JUNIT">
<mkdir dir="${build.dir}"/>
<javac srcdir="src/" destdir="${build.dir}" >
<include name="**/*.java"/>
</javac>
</target>
<target name="jar" depends="compile">
<mkdir dir="build/lib"/>
<jar jarfile="build/lib/${app.name}.jar"
basedir="${build.dir}" includes="com/**"/>
</target>
<target name="compiletests" depends="jar">
<mkdir dir="build/testcases"/>
<javac srcdir="src/test" destdir="build/testcases">
<classpath>
<pathelement location="build/lib/${app.name}.jar" />
<pathelement path="" />
</classpath>
<include name="**/*.java"/>
</javac>
</target>
<target name="runtests" depends="compiletests" if="junit.present">
<java fork="yes" classname="junit.textui.TestRunner"
taskname="junit" failonerror="true">
<arg value="test.com.company.AllJunitTests"/>
<classpath>
<pathelement location="build/lib/${app.name}.jar" />
< |
| |
|
| |
本站关键词: |
|
|
|
|
 |
|
 |
|