在Eclipse中,我们可以采用Junit插件进行调试,那么Junit的执行及加载顺序是什么呢?下面我们通过实际的代码来分析一下。
首先展示的是一个类,功能是判断字符串是否为空,代码如下
package com.yourdomian.test;
public class TestClass{
public void myJunit(){
System.out.println("in TestClass's myJunit");
}
}
下面是Junit的测试类
package com.yourdomian.test;
import junit.framework.Assert;
import junit.framework.TestCase;
public class TestClassTest extends TestCase {
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
//myStringUtility = new StringUtility();
System.out.println("in Junit's setUp");
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
System.out.println("in Junit's tearDown");
}
public void testMyJunit() {
System.out.println("in TestClassTest 's testMyJunit");
}
}
运行TestClassTest ,控制台结果为
in Junit's setUp
in StringUtilityTest's testMyJunit
in Junit's tearDown
如果在TestClassTest 中在加入一段代码,请看下面
package com.yourdomian.test;
import junit.framework.Assert;
import junit.framework.TestCase;
public class TestClassTest extends TestCase {
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
//myStringUtility = new StringUtility();
System.out.println("in Junit's setUp");
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
System.out.println("in Junit's tearDown");
}
public void testMyJunit() {
System.out.println("in TestClassTest 's testMyJunit");
}
public void testOther() {
//这一段为新加的代码
}
}
运行,控制台的结果为
in Junit's setUp
in StringUtilityTest's testMyJunit
in Junit's tearDown
in Junit's setUp
in Junit's tearDown
其实Junit没什么神秘的,现在应该对这个过程一目了然了吧~