JTestCase+JICE使用小结

JTestCase 是JUnit扩展,它使用XML文件来组织测试用例数据,包括初期值入口参数和期望结果(对DAO的测试结合DBUnit使用效果更好),能够使测试数据和测试代码分离。
JICE是基于XML文件构建和配置Java对象的工具,因为测试准备数据中经常会包含一些复杂对象,所以JTestCase增加了对JICE的支持用于生成这些对象。

以下是官网的一点说明:
JTestCase is a 100% pure Java, open-source microframework that helps in separating test case data from test case units.
Jice is an (excellent) lightweight dependency injection framework now embedded into JTestCase.

JICE is an XML-based Inversion of Control (IoC) Container - a tool for constructing and configuring application objects. JICE consists of:
    * JIC Language - an XML format for describing the objects in a Java application.
    * JIC Engine - A Java application that constructs graphs of Java objects based on the instructions the XML files.

下面是一个实例:

TestData.xml:
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:noNamespaceSchemaLocation="http://jtestcase.sourceforge.net/dtd/jtestcase2.xsd">
	<class name="Service02Impl">
		<method name="getUserInfo">
			<test-case name="001">
				<params>
					<param name="testCaseId" type="java.lang.String">J1_1_S_1</param>
					<param name="userId" type="java.lang.String">0000000001</param>
					<param name="userInfo" type="" use-jice="yes">
				    	<jice>
				    		<bean xmlns="http://www.jicengine.org/jic/2.0" class="com.test.userInfoBean">
					            <userId class="java.lang.String">0000000001</userId>
					            <userName class="java.lang.String">USER_1</userName>
					        </bean>
					    </jice>
				    </param>
				</params>
				<asserts>
					<assert name="returnValue" action="EQUALS" type="java.lang.String">TRUE</assert>
				</asserts>
			</test-case>
		</method>
	</class>
</tests>

Service02ImplTest.java
		jTestCase = new JTestCase("test/TestData.xml", "Service02Impl");
		String userId = null;
		userInfoBean actualUserInfo = null;
		userInfoBean expectUserInfo = null;
		final String METHOD = "getUserInfo";
		
		Vector testCases = jTestCase.getTestCasesInstancesInMethod(METHOD);

		for (int i = 0; i < testCases.size(); i++) {

			TestCaseInstance testCase = (TestCaseInstance) testCases.elementAt(i);
			HashMap params = testCase.getTestCaseParams();

			userId = (String) params.get("userId");
			expectUserInfo = (userInfoBean) params.get("userInfo");
			
			actualUserInfo = service02Impl.getUserInfo(userId);
			
			Assert.assertEquals(expectUserInfo.getUserId(),actualUserInfo.getUserId());
			Assert.assertEquals(expectUserInfo.getUserName(),actualUserInfo.getUserName());
		}
	}


JTestCase使用比较简单,下一篇介绍一下JICE的使用

你可能感兴趣的:(java,DAO,xml,JUnit,IOC)