项目中使用junit4.4,虽然一直在用,但对testcase 的执行细节却不大清楚,我做了一些例子来测试一下
JunitTest.java
private Logger logger = Logger.getLogger(JunitTest.class);
public JunitTest() {
super("junit test");
logger.info("junit test contruct");
}
public void setUp() {
logger.info("setUp");
}
public void testMethod1() {
logger.info("method1");
}
public void testMethod2() {
logger.info("method2");
}
@Test
public void method3() {
logger.info("annotion method3");
}
private void testMethod4() {
logger.info("private method4");
}
@Test
private void testMethod5() {
// error Test method isn't public
logger.info("annotion private method5");
}
public void mthod6() {
logger.info("method6");
}
private void mthod7() {
logger.info("method6");
}
public void tearDown() {
logger.info("tear down");
}
日志输出:
01-13 10:58:44,439 INFO test.com.hiksoft.prototype.modules.advancedtemplate.junittest.JunitTest.(JunitTest.java:15) junit test contruct 01-13 10:58:44,439 INFO test.com.hiksoft.prototype.modules.advancedtemplate.junittest.JunitTest.(JunitTest.java:15) junit test contruct 01-13 10:58:44,455 INFO test.com.hiksoft.prototype.modules.advancedtemplate.junittest.JunitTest.setUp(JunitTest.java:18) setUp 01-13 10:58:44,455 INFO test.com.hiksoft.prototype.modules.advancedtemplate.junittest.JunitTest.testMethod1(JunitTest.java:21) method 01-13 10:58:44,455 INFO test.com.hiksoft.prototype.modules.advancedtemplate.junittest.JunitTest.tearDown(JunitTest.java:45) tear down 01-13 10:58:44,455 INFO test.com.hiksoft.prototype.modules.advancedtemplate.junittest.JunitTest.setUp(JunitTest.java:18) setUp 01-13 10:58:44,455 INFO test.com.hiksoft.prototype.modules.advancedtemplate.junittest.JunitTest.testMethod2(JunitTest.java:24) method2 01-13 10:58:44,455 INFO test.com.hiksoft.prototype.modules.advancedtemplate.junittest.JunitTest.tearDown(JunitTest.java:45) tear down
junit执行结果
testMethod1 success;
testMethod2 success;
warning 这个什么错也没有,我又测试了一下 发现是 testMethod4 的问题
warning 这个报错了, Test Method isn't public: testMethod5
test 3 没有出现 ,难道junit4中方法必需要有 test前缀吗,
从日志分析:
JunitTest 执行了 两次 构造函数,这个原因还不知道,需要看具体的执行代码。
执行每个TestMethod 时 都会 执行一次setUp() 和 tearDown(); 应该是为method清理环境
还有 测试方法应该有 test 前缀 ,而且为public
乌龙了,确实是junit4的包,不过确实junit3的用法。
下面是 junit 4的用法;
public class Junittest4 { private static Logger logger = Logger.getLogger(Junittest4.class); @Before public void before() { logger.info("before setUp"); } @After public void after() { logger.info("After teardown"); } public void testMethod1() { logger.info("no test annotion public method"); } @Test public void Method2() { logger.info("test annotion public method"); } @Ignore("not complete") @Test public void Method3() { logger.info("test annotion private method"); } @Test(timeout = 5) public void testMethod4() throws InterruptedException { Thread.sleep(4); logger.info("test annotion public has test method"); } @Test(timeout = 5) public void testMethod5() throws InterruptedException { Thread.sleep(5); logger.info("test annotion public has test method"); } @Test(timeout = 5) public void testMethod6() throws InterruptedException { Thread.sleep(6); logger.info("test annotion public has test method"); } }
日志输出
01-13 11:45:50,502 INFO test.com.hiksoft.prototype.modules.advancedtemplate.junittest.Junittest4.before(Junittest4.java:13) before setUp 01-13 11:45:50,517 INFO test.com.hiksoft.prototype.modules.advancedtemplate.junittest.Junittest4.Method2(Junittest4.java:24) test annotion public method 01-13 11:45:50,517 INFO test.com.hiksoft.prototype.modules.advancedtemplate.junittest.Junittest4.after(Junittest4.java:17) After teardown 01-13 11:45:50,564 INFO test.com.hiksoft.prototype.modules.advancedtemplate.junittest.Junittest4.before(Junittest4.java:13) before setUp 01-13 11:45:50,580 INFO test.com.hiksoft.prototype.modules.advancedtemplate.junittest.Junittest4.testMethod4(Junittest4.java:34) test annotion public has test method 01-13 11:45:50,580 INFO test.com.hiksoft.prototype.modules.advancedtemplate.junittest.Junittest4.after(Junittest4.java:17) After teardown 01-13 11:45:50,580 INFO test.com.hiksoft.prototype.modules.advancedtemplate.junittest.Junittest4.before(Junittest4.java:13) before setUp 01-13 11:45:50,595 INFO test.com.hiksoft.prototype.modules.advancedtemplate.junittest.Junittest4.after(Junittest4.java:17) After teardown 01-13 11:45:50,595 INFO test.com.hiksoft.prototype.modules.advancedtemplate.junittest.Junittest4.before(Junittest4.java:13) before setUp 01-13 11:45:50,611 INFO test.com.hiksoft.prototype.modules.advancedtemplate.junittest.Junittest4.after(Junittest4.java:17) After teardown
测试结果
method2 suucess
method3 ignore
method4 success
method5 fail
method6 fail