把我刚测试成功的例子(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.divsent" 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.divsent"> <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" /> <pathelement location="build/testcases" /> <pathelement path="" /> <pathelement path="${java.class.path}" /> </classpath> </java> </target> </project>
如果是win系统,在DOS窗口下进入你的工作目录,并到 ../JunitTest/build下,执行ant runtests命令,会看到如下信息
D:TEMPJunitTestTestCaseuild>ant runtests Buildfile: build.xml
JUNIT:
compile: [mkdir] Created dir: D:TEMPJunitTestTestCaseuildclasses [javac] Compiling 3 source files to D:TEMPJunitTestTestCaseuildclasses
jar: [mkdir] Created dir: D:TEMPJunitTestTestCaseuildlib [jar] Building jar: D:TEMPJunitTestTestCaseuildlibsayhello.jar
compiletests: [mkdir] Created dir: D:TEMPJunitTestTestCaseuild estcases [javac] Compiling 2 source files to D:TEMPJunitTestTestCaseuild estcas es
runtests: [junit] . [junit] Time: 0
[junit] OK (1 test)
BUILD SUCCESSFUL Total time: 7 seconds
<中间休息^_^>
下面请把 com/company/HelloWorld.java文件中的return "Hello World!";改成return "Hello world!!",并在../JunitTest/builde目录运行下运行ant runtests。 我们会看到如下情形: …………………………………… …………………………………… runtests: [junit] .F [junit] Time: 0.01 [junit] There was 1 failure: [junit] 1) testSayHello(test.com.company.HelloWorldTest)junit.framework.Comp arisonFailure: expected:<...World...> but was:<...world!...> [junit] at test.com.company.HelloWorldTest.testSayHello(Unknown Source) [junit] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [junit] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAcces sorImpl.java:39) [junit] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMet hodAccessorImpl.java:25)
[junit] FAILURES!!! [junit] Tests run: 1, Failures: 1, Errors: 0 BUILD FAILED
我们可以看到,这是JUNIT测试用例未正常运行通过的错误信息。
|
关键词: JUNIT测试通过的HelloWorld