[关闭]
@DingCao-HJJ 2015-09-03T12:18:29.000000Z 字数 2422 阅读 2609

使用junit测试java程序方法

编程工具/环境 Java junit


参考资料

junit_百度百科

下载安装

下载地址

使用方法(以HelloWorld为例子)

先写好要测试的类和用以测试的类,如下:

  1. // HelloWorld.java
  2. import java.util.*;
  3. public final class HelloWorld {
  4. private String str;
  5. public static void main(String[] args) {
  6. HelloWorld hw = new HelloWorld();
  7. hw.hello();
  8. System.out.println(hw.str);
  9. }
  10. public void hello() {
  11. str = "Hello World!";
  12. }
  13. public String getStr() {
  14. return str;
  15. }
  16. }

在junit4.x版本中,我们可以用@test来表示一个测试。

  1. // HelloWorldTes.java
  2. import static org.junit.Assert.*;
  3. import org.junit.Test;
  4. public class HelloWorldTest {
  5. public HelloWorld helloworld = new HelloWorld();
  6. @Test
  7. public void testHello() {
  8. helloworld.hello();
  9. assertEquals("Hello World!", helloworld.getStr());
  10. }
  11. }

按照类似的目录组织好文件

/HelloWorld
  |-- build.xml
  |-- src
        |-- HelloWorld.java
        |-- HelloWorldTest.java
  |-- lib
        |-- junit-4.9.jar
  |-- build
        |-- classes
              |-- HelloWorld.class
              |-- HelloWorldTest.class

如上,junit的jar包放在了/HelloWorld/lib/文件夹下。

命令行

windows

要对HelloWorld进行测试,我们需要用到HelloWorldTest.java, 要先将工作目录转到HelloWord/src/

cd HelloWord/src

然后进行编译:

HelloWord/src$ javac -classpath .;../lib/junit-4.9.jar HelloWorldTest.java

接下来转到HelloWorld/build/classes目录,运行HelloWorldTest.

HelloWord/src$ cd ../build/classes
HelloWord/src$ java -classpath .;../../lib/junit-4.9.jar org.junit.runner.JUnitCore HelloWorldTest

运行结果:

Linux

语法和windows环境类似,只是把命令当中的;替换成:即可。

使用ant运行junit测试

如下,在build.xml里添加junit的target,并为其设置要引用的变量。注意,这里<junit>下的<classpath>表示目标测试class所在的目录,而再里面的path才表示测试时需要引用的第三方包。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- the only mark of project -->
  3. <project name="HelloWorld" default="run">
  4. <!-- essential properties to refer to -->
  5. <property name="build.classes.dir" location="build/classes"/>
  6. <property name="src.dir" location="src" />
  7. <property name="lib.dir" location="lib" />
  8. <path id="compile.path">
  9. <pathelement location="${lib.dir}/junit-4.9.jar" />
  10. </path>
  11. <!-- target is a task, can build, change directory, Junit and so on -->
  12. <!-- clean up the class files that compiled last time -->
  13. <target name="clean">
  14. <delete dir="${build.classes.dir}" />
  15. <mkdir dir="${build.classes.dir}"/>
  16. </target>
  17. <target name="compile">
  18. <javac srcdir="${src.dir}" destdir="${build.classes.dir}" classpathref="compile.path" includeantruntime="true">
  19. </javac>
  20. </target>
  21. <!-- auto run after a compilation -->
  22. <target name="run" depends="clean, compile">
  23. <java fork="true" classname="HelloWorld">
  24. <classpath path="${build.classes.dir}" />
  25. </java>
  26. </target>
  27. <!-- tests the methods in HelloWorld.java -->
  28. <target name="junit" depends="compile">
  29. <junit printsummary="true">
  30. <classpath path="${build.classes.dir}">
  31. <path refid="compile.path" />
  32. </classpath>
  33. <test name="HelloWorldTest" />
  34. </junit>
  35. </target>
  36. </project>
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注