- 浏览: 69083 次
- 性别:
- 来自: 北京
文章分类
最新评论
copy from http://www.cavdar.net/2008/07/21/junit-4-in-60-seconds/
played with JUnit 4 library this weekend and here is the short introduction to it:
-
@Test
Mark your test cases with @Test annotations. You don’t need to prefix your test cases with “test”. In addition, your class does not need to extend from “TestCase” class. (My note: 如果你的class不幸继承了TestCase的话,那么还将沿用Junit3的那一套寻找测试函数的模式(即函数名test***)@Test 这个annotattion将无效。
@Test public void addition() { assertEquals(12, simpleMath.add(7, 5)); } @Test public void subtraction() { assertEquals(9, simpleMath.substract(12, 3)); }
2. @Before and @After
-
Use @Before and @After annotations for “setup” and “tearDown” methods respectively. They run before and after every test case.
@Before
public void runBeforeEveryTest() {
simpleMath = new SimpleMath();
}
@After
public void runAfterEveryTest() {
simpleMath = null;
} -
@BeforeClass and @AfterClass
Use @BeforeClass and @AfterClass annotations for class wide “setup” and “tearDown” respectively. Think them as one time setup and tearDown. They run for one time before and after all test cases.@BeforeClass public static void runBeforeClass() { // run for one time before all test cases } @AfterClass public static void runAfterClass() { // run for one time after all test cases }
-
Exception Handling
Use “expected” paramater with @Test annotation for test cases that expect exception. Write the class name of the exception that will be thrown.@Test(expected = ArithmeticException.class) public void divisionWithException() { // divide by zero simpleMath.divide(1, 0); }
-
@Ignore
Put @Ignore annotation for test cases you want to ignore. You can add a string parameter that defines the reason of ignorance if you want.
@Ignore("Not Ready to Run") @Test public void multiplication() { assertEquals(15, simpleMath.multiply(3, 5)); }
-
Timeout
Define a timeout period in miliseconds with “timeout” parameter. The test fails when the timeout period exceeds.@Test(timeout = 1000) public void infinity() { while (true) ; }
-
New Assertions
Compare arrays with new assertion methods. Two arrays are equal if they have the same length and each element is equal to the corresponding element in the other array; otherwise, they’re not.public static void assertEquals(Object[] expected, Object[] actual);
public static void assertEquals(String message, Object[] expected, Object[] actual);@Test public void listEquality() { List<Integer> expected = new ArrayList<Integer>(); expected.add(5); List<Integer> actual = new ArrayList<Integer>(); actual.add(5); assertEquals(expected, actual); }
-
JUnit4Adapter
Run your Junit 4 tests in Junit 3 test runners with Junit4Adapter.public static junit.framework.Test suite() { return new JUnit4TestAdapter(SimpleMathTest.class); }
Happy coding.
My Note goes here:
文中没有提到JUnit4中TestSuite的用法,新的用法TestSuite.addTest(..)已经不再适用,因为新的testclass都没有extends TestCase.取而代之的用法如下:
import junit.framework.Test; import junit.framework.TestSuite; import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class) @SuiteClasses({YouTestCaseOne.class,YourTestCase2.class}) public class AllTests { }
发表评论
-
How to be a Programmer: A Short,Comprehensive,and Personal Summary
2013-10-28 10:38 550well written. http://samizdat ... -
js module pattern
2013-10-12 16:21 362http://www.adequatelygood.com/ ... -
GZip compressing HTML, JavaScript, CSS etc. makes the data sent to the browser s
2013-07-31 15:48 630this is fun. http://tutorials ... -
java collection matrix
2012-08-07 11:24 701http://www.janeve.me/articles/w ... -
ghost text (aka in-field text)
2012-04-01 11:18 635http://archive.plugins.jquery.c ... -
What is Optimistic Locking vs. Pessimistic Locking
2011-09-09 16:50 801What is Optimistic Locking vs. ... -
what is DAO
2011-04-15 13:42 738http://java.sun.com/blueprints/ ... -
indenting xml in vim with xmllint
2011-01-10 09:48 674I added to my “.vimrc” file: ... -
css sprite
2010-12-15 16:57 611http://css-tricks.com/css-sprit ... -
最牛B 的 Linux Shell 命令
2010-10-30 00:08 679http://hi.baidu.com/hy0kl/blog/ ... -
GPS Bearing VS Heading
2010-10-21 15:40 1642http://gps.about.com/od/glossar ... -
Document Type Declaration
2010-07-19 22:01 802Document Type Declaration h ... -
XML Declaration must be the first line in the document.
2010-06-12 17:54 864The XML declaration typically a ... -
UCM
2010-05-08 11:41 714Two links about UCM The power ... -
What is an MXBean?
2010-01-28 11:10 699refer to http://weblogs.java. ... -
why wait() always in a loop
2010-01-19 00:17 818As we know ,jdk API doc suggest ... -
Locks in Java
2010-01-18 22:48 904copied from http://tutorials.je ... -
use jps instead of ps to find jvm process
2010-01-11 14:21 782copied from http://java.sun.com ... -
My first error of Hello Wolrd Struts
2010-01-04 09:10 843It's my first time to touch Str ... -
Unit Testing Equals and HashCode of Java Beans
2009-12-29 10:07 1280copy from http://blog.cornetdes ...
相关推荐
JUnit4
java junit4 jar完整包,junit可用来项目的单元测试。
运行junit4所需jar包,包括junit-4.11.jar与 hamcrest-core-1.3.jar
JUnit in Action中文版JUnit in Action中文版JUnit in Action中文版JUnit in Action中文版
junit4学习文档,有实例,非常详细,不懂的可以参考
这是一个junit4测试的jar包,copy进去就可用
赠送jar包:powermock-module-junit4-2.0.9.jar; 赠送原API文档:powermock-module-junit4-2.0.9-javadoc.jar; 赠送源代码:powermock-module-junit4-2.0.9-sources.jar; 赠送Maven依赖信息文件:powermock-...
junit4教程(《Junit4初探》)
JUnit+in+Action中文版 JUnit实战
·用mock objects进行隔离测试(★★★★★) ·用Cactus进行容器内测试 ·用Ant和Maven进行自动构建(★★★★★) ·在Eclipse内进行测试 ·对如下内容进行单元测试 ◆Java应用程序(★★) ...
JUnit in Action 3nd Edition
JUnit in Action 中文版英文版 JUnit Recipies好书哦!
JUnit in Action 中文版电子书
JUnit4基础文档
junit in action 中文版
JUnit in Action中文版
JUnit.in.Action中文版(3)
junit in action 源码 java版
JUnit.in.Action中文版 第二部分
因 jdk5 中的新特性,JUnit4 也因此有了很大的改变。下面详细介绍 JUnit4 的使用方法,只是举个例子说明junit4 的使用