[标题]:[原]深入JUnit4.x
[时间]:2009-7-5
[摘要]:JUnit4.x参数化测试、私有方法测试、测试套件
[关键字]:JUnit、Test、测试、单元测试、addTest not applicable、suite、套件、参数化测试、私有方法、private、反射、断言
[环境]:JUnit4.5、MyEclipse7
[作者]:Winty ([email protected]) http://www.blogjava.net/wintys
[正文]:
测试目标类:
Calculator.java:
package wintys.junit; /** * * @author Winty ([email protected]) http://www.blogjava.net/wintys/ * @version 2009-07-05 */ public class Calculator { /** * 加法 * @param a * @param b * @return a与b的和 */ public int add(int a , int b){ return a + b; } /** * 减法,访问权限设为private * @param a * @param b * @return a与b的差 */ private int sub(int a , int b){ return a - b; } }
简单测试
CalculatorTest.java:
package wintys.junit; import junit.framework.Assert; import org.junit.Before; import org.junit.Test; public class CalculatorTest { Calculator cal; @Before public void setUp() throws Exception { cal = new Calculator(); } @Test public void testAdd(){ int result = cal.add(1, 1); Assert.assertEquals(2 , result); } }
参数化测试CalculatorTestWithParameter.java:
package wintys.junit; import java.util.Arrays; import java.util.Collection; import static org.junit.Assert.assertEquals; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; /** * JUnit4参数化测试 * @author Winty ([email protected]) http://www.blogjava.net/wintys/ * @version 2009-07-05 */ @RunWith(Parameterized.class) public class CalculatorTestWithParameter { private int input1; private int input2; private int result; public CalculatorTestWithParameter(int input1, int input2, int result) { super(); this.input1 = input1; this.input2 = input2; this.result = result; } @Parameters public static Collection<Object[]> initParam(){ Object[][] objArray = new Object[][]{ {1 , 1 , 2}, {2 , 5 , 7}, {-1 , 8 , 7}, {-5 , -1 ,-6} }; return Arrays.asList(objArray); } @Test public void testAdd(){ Calculator cal = new Calculator(); int rt = cal.add(input1, input2); assertEquals(result , rt); } }
测试私有方法CalculatorTestOfPrivate.java:
package wintys.junit; import java.lang.reflect.Method; import junit.framework.Assert; import org.junit.Before; import org.junit.Test; /** * JUnit4测试private方法 * @author Winty ([email protected]) http://www.blogjava.net/wintys/ * @version 2009-07-05 */ public class CalculatorTestOfPrivate { Calculator cal; @Before public void setUp() throws Exception { cal = new Calculator(); } @Test public void testPrivateMethod(){ Class<Calculator> cls = Calculator.class; Method method = null; Object result = 0 ; try { method = cls.getDeclaredMethod("sub", new Class<?>[]{int.class,int.class}); method.setAccessible(true); result = (Object)method.invoke(cal, 1 , 2); Assert.assertEquals(-1, result); } catch (Exception e) { e.printStackTrace(); Assert.fail(); } } }
v
package wintys.junit; import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; /** * JUnit4 Test Suite Style * @author Winty ([email protected]) http://www.blogjava.net/wintys/ * @version 2009-07-04 */ //表示这个类是一个测试套件 @RunWith(Suite.class) //说明这个测试套件所包含的测试类 @SuiteClasses({ SubscriptionTest.class, CalculatorTest.class, CalculatorTestWithParameter.class, CalculatorTestOfPrivate.class }) public class AllTests { //在JUnit4.x中,套件类的内容一般留空,只作为以上Annotation的持有者 // the class remains completely empty, // being used only as a holder for the above annotations }
附录:
JUnit3.8.x测试套件语法:
package test;
import test.gg.ba.util.UtilityTest;
import junit.framework.Test;
import junit.framework.TestSuite;
public class MyTests {
public static Test suite() {
TestSuite suite = new TestSuite("Test for test");
//$JUnit-BEGIN$
suite.addTest(test.gg.ba.util.UtilityTest.class);
//$JUnit-END$
return suite;
}
}
在JUnit4中运行JUnit3.8.x的测试套件,
Runner for use with JUnit 3.8.x-style:
@RunWith(AllTests.class)
public class ProductTests {
public static junit.framework.Test suite() {
...
}
}
[参考资料]:
[1] Test suites using annotations : http://radio.javaranch.com/lasse/2006/07/27/1154024535662.html
[2] JUnit 4 Test Suite - addTest not applicable : http://www.velocityreviews.com/forums/t636846-junit-4-test-suite-addtest-not-applicable.html