Junit-学习笔记
1.下载地址:http://www.junit.org/。我下载的版本是junit4.10。
2.安装:下载好后,解压到F盘
3.环境变量设置:添加F:\junit4.10\junit-4.10.jar到classpath
4.命令行输入:java org.junit.runner.JUnitCore org.junit.tests.AllTests
没有错误,则表明环境搭建好了,just like this:

1.如何写并且运行一个测试案例?
1.新建一个类:
package junitfaq; import org.junit.*; import static org.junit.Assert.*; import java.util.*; public class SimpleTest {2.写一个方法,需要注解为@test,并且方法里要写一个判断结果的断言语句:
@Test public void testEmptyCollection() { Assert.assertTrue(new Random().nextInt(5) > 1); }3.虽然在IDE里,可以方便的用插件运行,但是你也可以通过这么写来运行你的测试案例:
public static void main(String args[]) { org.junit.runner.JUnitCore.main("junitfaq.SimpleTest"); } }4.运行测试案例:
在控制台里运行,两种方式:
java org.junit.runner.JUnitCore junitfaq.SimpleTest
java junitfaq.SimpleTest
5.查看运行结果:
Time: 0OK (1 tests)
2.如何使用before和after注解?
1假设你写了这么一个测试案例:
package junitfaq; import org.junit.*; import static org.junit.Assert.*; import java.util.*; public class SimpleTest { private Collection<Object> collection; @Before public void setUp() { collection = new ArrayList<Object>(); } @Test public void testEmptyCollection() { assertTrue(collection.isEmpty()); } @Test public void testOneItemCollection() { collection.add("itemA"); assertEquals(1, collection.size()); } }这个案例将执行的顺序是:
setUp()testEmptyCollection()setUp()testOneItemCollection()2.如果你这么写:
package junitfaq; import org.junit.*; import static org.junit.Assert.*; import java.io.*; public class OutputTest { private File output; @Before public void createOutputFile() { output = new File(...); } @After public void deleteOutputFile() { output.delete(); } @Test public void testSomethingWithFile() { ... } }执行的顺序是:
createOutputFile()testSomethingWithFile()deleteOutputFile()3.如何测试一个没有返回值的方法?
如果是这种情况,就看这个方法都干了什么,都产生了什么样的结果。根据产生的结果来测试。比如,这个方法是向数据库里插入一调数据,那么,就可以测试下,查询这条数据是否在数据库内。
4.在什么样子的情况下,我需要测试get()方法和set()方法?
额。这个。。。。如果这个方法里边可能会出现问题的话,你就可以测试。
5.怎么写一个当所期待的异常抛出时会忽略的测试案例呢?
@Test(expected=IndexOutOfBoundsException.class) public void testIndexOutOfBoundsException() { ArrayList emptyList = new ArrayList();Object o = emptyList.get(0); }6.怎么写一个当不期待的异常抛出时,会标志为false,即测试失败?
@Test public void testIndexOutOfBoundsExceptionNotRaised() throws IndexOutOfBoundsException { ArrayList emptyList = new ArrayList(); Object o = emptyList.get(0); }7.如何测试那些protected的方法?
把你当测试案例放到要测试的类的同一个包下:
例如:
src com xyz SomeClass.java SomeClassTest.java
补充:一般来说,你的代码结构应该是这样的:
src com xyz SomeClass.javatest com xyz SomeClassTest.java
8.如何测试private方法?
你可以把方法复制出来到另个类里测试。或者使用java反射,来调用那个方法。稍微复杂一点,但是也可以实现。
9.为什么在单个测试中,只返回第一次测试错误的信息?
或许你是这么写的:
@Test public void testA() { Assert.assertTrue(true); Assert.assertTrue(true); Assert.assertTrue(false); }
测试的结果是:
JUnit version 4.10
.E
Time: 0.005
There was 1 failure:
1) testA(org.example.junit.Main)
java.lang.AssertionError
at org.junit.Assert.fail(Assert.java:92)
这个嘛,本身设计就是这样的。在这种情况,最好是这么写:
@Test public void testA() { Assert.assertTrue(true); } @Test public void testB() { Assert.assertTrue(true); } @Test public void testC() { Assert.assertTrue(false); }10.我需要为每一个类都写一个测试类吗?
在每一个类下面应该都有一个测试类相对应,这虽然是一种协定,但是不是必须的。
一般来说,一开始的时候,你会先为每个类都写一个测试类,但是后来,如果你发现,某些测试类的内容差不多,或者可以有重用的部分,那你就应该把他们抽取出来放在另一个共用的类里,比如测试基类。
11.我想要一个简单的测试模版?
import org.junit.*;import static org.junit.Assert.*;public class SampleTest { private java.util.List emptyList; /** * Sets up the test fixture. * (Called before every test case method.) */ @Before public void setUp() { emptyList = new java.util.ArrayList(); } /** * Tears down the test fixture. * (Called after every test case method.) */ @After public void tearDown() { emptyList = null; } @Test public void testSomeBehavior() { assertEquals("Empty list should have 0 elements", 0, emptyList.size()); } @Test(expected=IndexOutOfBoundsException.class) public void testForException() { Object o = emptyList.get(0); }}
12.如何对抽象类进行测试?
可以参考:http://c2.com/cgi/wiki?AbstractTestCases
13.在我所有的测试过程中,如何只运行一次setUp()和tearDown()方法?
你可以这两个注解:
@BeforeClass和@AfterClass,例如:
package junitfaq; import org.junit.*; import static org.junit.Assert.*; import java.util.*; public class SimpleTest { private Collection collection; @BeforeClass public static void oneTimeSetUp() { // one-time initialization code } @AfterClass public static void oneTimeTearDown() { // one-time cleanup code } @Before public void setUp() { collection = new ArrayList(); } @After public void tearDown() { collection.clear(); } @Test public void testEmptyCollection() { assertTrue(collection.isEmpty()); } @Test public void testOneItemCollection() { collection.add("itemA"); assertEquals(1, collection.size()); } }执行顺序是这样的:
oneTimeSetUp()setUp()testEmptyCollection()tearDown()setUp()testOneItemCollection()tearDown()oneTimeTearDown()14.如何在windows命令行中运行测试案例?
首先设置CLASSPATH,然后再命令行里输入:
java org.junit.runner.JUnitCore <test class name>
15.如何通过Ant运行测试案列?
1.设置必要的属性:
<property name="src" value="./src" /><property name="lib" value="./lib" /><property name="classes" value="./classes" /><property name="test.class.name" value="com.xyz.MyTestSuite" />2.设置CLASSPATH:
<path id="test.classpath"> <pathelement location="${classes}" /> <pathelement location="/path/to/junit.jar" /> <fileset dir="${lib}"> <include name="**/*.jar"/> </fileset></path>3.定义Junit Task:
<target name="test"> <junit fork="yes" haltonfailure="yes"> <test name="${test.class.name}" /> <formatter type="plain" usefile="false" /> <classpath refid="test.classpath" /> </junit></target>4.运行 ant
ant test
16.使用Ant测试,如何把测试结果生成到Html文件里边?
1.确保ant的optional.jar包添加到了CLASSPATH里边
2.添加一个properties:
<property name="test.reports" value="./reports" />
3.定义Task:
<target name="test-html"> <junit fork="yes" printsummary="no" haltonfailure="no"> <batchtest fork="yes" todir="${test.reports}" > <fileset dir="${classes}"> <include name="**/*Test.class" /> </fileset> </batchtest> <formatter type="xml" /> <classpath refid="test.classpath" /> </junit> <junitreport todir="${test.reports}"> <fileset dir="${test.reports}"> <include name="TEST-*.xml" /> </fileset> <report todir="${test.reports}" /> </junitreport></target>4.运行:
ant test-html