另一个常用的单元测试工具:
Mockito链接:http://blog.csdn.net/onlyqi/article/details/6396646
Mockito的API链接:http://mockito.googlecode.com/svn/branches/1.6/javadoc/org/mockito/Mockito.html
以下教程文章来源:http://www.vogella.de/articles/JUnit/article.html#easymock
1.介绍
单元测试用于开发者对于代码中特定功能进行测试,它能保证代码如 预期般工作 并且 验证在代码修改后是否还保持原来功能。
JUnit4.x是一个测试框架,使用注释来识别待测试的方法。JUit保证所有测试方法可以以任意顺序进行。所以JUit测试不应依赖于其他测试。
要用JUit实现测试:
以@org.junit.Test注释一个方法
使用JUnit提供的方法来检测代码结果
2.安装
Eclipse的用户可以直接使用在Eclipse中集成的JUnit进行测试;
如果想在自己的代码中使用JUit,在http://www.junit.org/地址下载"junit-4.x.jar"包,并将其添加到你的Java工程和classpath内
3.使用
3.1 准备
创建一个新工程junit.first. 我们将在测试与源代码分离,当然这种分离不是强制性的,但是提倡这样做。你也可以为测试类创建一个新的工程。
选择/properties/Java Build Path/Source
点击右边的"Add folder"在当前工程中添加新文件,点击"create new folder"
这两步准备也可以在工程名上右击添加folder即可
3.2创建Java类
在src文件中,创建如下代码文件junitTest:
代码为:
package junit.first;
public class JunitTest {
public int multiply(int x, int y) {
return x / y;
}
}
3.3建立JUnit测试
选中JunitTest.java文件,右击选择New/JUnit Test Case,选择 “New JUnit 4 test", 将源文件夹设为test。这样产生的test类位于test文件夹中
下一步,选择测试方法:
然后将JUnit library添加到classpath中即可。
JunitTestTest.java中添加如下代码:
package junit.first;
import static org.junit.Assert.*;
import org.junit.Test;
public class JunitTestTest {
@Test
public void testMultiply() {
JunitTest tester = new JunitTest();
assertEquals("Result", 50, tester.multiply(10, 5));
}
}
3.4 通过Eclipse运行测试
选择 Run As/ Junit test, 得到如下结果:红色条表示出错
将除号“/”改为“*”,即可得到正确结果。此时得到绿色条纹
当有较多测试时,可以将其连接为测试集,
选择要测试的类,右击/New/Other/Java/JUnit/Junit Test Suite,改变如下代码,得到想要的测试结果:
package junit.first;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({})
public class AllTests {
}
3.5通过代码运行test
类org.junit.runner.JUnitCore提供了runClasser()方法允许运行一个或多个测试类。它返回Result类型,代表测试的信息。
可以在test文件夹里,创建如下新类 MyTestRunner,该类实现测试,并将错误信息输出到控制台:
package junit.first;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class MyTestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(JunitTestTest.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
}
}
4.JUnit 细节
4.1用Eclipse静态导入
JUnit使用了很多静态方法,Eclipse不能总是自动静态导入,可以通过内容协助使用JUnit测试方法
打开Window -> Preferences ->
→ → → .使用“New Member"导入想要的方法。
建议至少导入上图中四个方法。
4.2注释
Table 1. Annotations
Annotation | Description |
---|---|
@Test public void method() | The annotation @Test identifies that a method is a test method. |
@Before public void method() | Will execute the method before each test. This method can prepare the test environment (e.g. read input data, initialize the class). |
@After public void method() | Will execute the method after each test. This method can cleanup the test environment (e.g. delete temporary data, restore defaults). |
@BeforeClass public void method() | Will execute the method once, before the start of all tests. This can be used to perform time intensive activities, for example to connect to a database. |
@AfterClass public void method() | Will execute the method once, after all tests have finished. This can be used to perform clean-up activities, for example to disconnect from a database. |
@Ignore | Will ignore the test method. This is useful when the underlying code has been changed and the test case has not yet been adapted. Or if the execution time of this test is too long to be included. |
@Test (expected = Exception.class) | Fails, if the method does not throw the named exception. |
@Test(timeout=100) | Fails, if the method takes longer than 100 milliseconds. |
4.3 Assert 声明
Table 2. Test methods
Statement | Description |
---|---|
fail(String) | Let the method fail. Might be used to check that a certain part of the code is not reached. Or to have failing test before the test code is implemented. |
assertTrue(true) / assertTrue(false) | Will always be true / false. Can be used to predefine a test result, if the test is not yet implemented. |
assertTrue([message], boolean condition) | Checks that the boolean condition is true. |
assertsEquals([String message], expected, actual) | Tests that two values are the same. Note: for arrays the reference is checked not the content of the arrays. |
assertsEquals([String message], expected, actual, tolerance) | Test that float or double values match. The tolerance is the number of decimals which must be the same. |
assertNull([message], object) | Checks that the object is null. |
assertNotNull([message], object) | Checks that the object is not null. |
assertSame([String], expected, actual) | Checks that both variables refer to the same object. |
assertNotSame([String], expected, actual) | Checks that both variables refer to different objects. |