我用的是Junit4,编写如下基类
子类:
数据文件:
java 代码
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.InvalidPropertiesFormatException;
- import java.util.Properties;
- import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
- public abstract class SpringBaseTestCase extends AbstractTransactionalDataSourceSpringContextTests {
- protected Properties properties;
- public void onSetUpInTransaction() throws InvalidPropertiesFormatException, IOException {
- properties = new Properties();
- InputStream is = ClassLoader.getSystemResourceAsStream(getTestFixturePropertyFile());
- if (is != null) properties.loadFromXML(is);
- loadSet();
- }
- private String getTestFixturePropertyFile() {
- return getClass().getSimpleName() + ".xml";
- }
- protected void loadSet() {
- // String test = properties.getProperty(this.getName() + "_pid");
- }
- @Override
- protected String[] getConfigLocations() {
- return new String[] { "applicationContext-hibernate.xml" };
- }
- }
子类:
java 代码
- import org.junit.Test;
- import basic.SpringBaseTestCase;
- import basic.User;
- public class PointsDaoImplTest extends SpringBaseTestCase {
- private IPointsDao pd;
- private int pidx;
- private int points;
- private int lockPoints;
- private int activePoints;
- public void loadSet() {
- pd = (IPointsDao) applicationContext.getBean("pointsDao");
- pidx = Integer.valueOf(properties.getProperty(getName() + "_userPidx"));
- setUser();
- }
- private void setUser() {
- User user = new User();
- user.setPidx(String.valueOf(pidx));
- pd.setUser(user);
- }
- @Test(expected = java.lang.IllegalArgumentException.class)
- public void testSetUserIllArgument() {
- pd.setUser(null);
- }
数据文件:
xml 代码
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
- <properties>
- <!-- testSetUser -->
- <entry key="testSetUserIllArgument_userPidx">12</entry>
- <entry key="testSetUserIllArgument_points">20</entry>
- <!-- testGetUserPoints -->
- <entry key="testGetUserPoints_userPidx">13</entry>
- <entry key="testGetUserPoints_points">44</entry>