wiki:demo_08

Version 1 (modified by qinhongyuan, 14 years ago) (diff)

--

单元测试数据初始化--数组

论坛权限方法测试,初始化数组数据。数据我们一般是存放到数据库的,但是单元测试并且是在测服务层,我们一般不连数据库的。
自己简单的模拟数据出来又不是很适合,而且重用性低。这时我们可以把数据库的数据搬过来使用,以xml文件的格式

public class UserRightServiceTest {
    final static UserRightService userRightService = new UserRightService();
    private static Mockery context = new JUnit4Mockery() {{
	setImposteriser(ClassImposteriser.INSTANCE);
    }};
    private static States state = context.states("test");
    static UserGroupRepository userGroupRepository;
    static LockService lockService;
    static UserGroup[] userGroups;

	/**
	 * 初始化数组
	 * @throws DocumentException
	 */
    @BeforeClass
    public static void setUp() throws DocumentException {
		userGroupRepository = context.mock(UserGroupRepository.class);
		lockService = context.mock(LockService.class);
		//初始化用户组数据
		userGroups = initUserGroups("bbs6_usergroup.xml", "bbs6_usergroup_item.xml");	
		userRightService.userGroupRepository = userGroupRepository;
		userRightService.lockService = lockService;
		userRightService.userGroups = userGroups;
    }
	public static UserGroup[] initUserGroups(String sourceFilePath, String sourceFileItemPath) throws DocumentException {

		XMLParser items = new XMLParser(sourceFileItemPath);
		Iterator iterItems = items.getIterator("row");
		Map<Long, Map<String, Long>> itemMap = new HashMap<Long, Map<String, Long>>();
		Element ele = null;

		while (iterItems.hasNext()) {
			ele = (Element)iterItems.next();
			long groupId = NumberUtils.toLong(ele.elementText("usergroupid"));
			if (itemMap.containsKey(groupId)) {
				itemMap.get(groupId).put(ele.elementText("name"), NumberUtils.toLong(ele.elementText("value")));
			} else {
				Map<String, Long> tempMap = new HashMap<String, Long>();
				tempMap.put(ele.elementText("name"), NumberUtils.toLong(ele.elementText("value")));
				itemMap.put(groupId, tempMap);
			}
		}

		XMLParser iData = new XMLParser(sourceFilePath);
		Iterator iter = iData.getIterator("row");
		List<UserGroup> userGroupsList = new ArrayList<UserGroup>(iData.getLength());
		
		while (iter.hasNext()) {
			UserGroup ug = new UserGroup();
			ele = (Element)iter.next();
			long groupId = NumberUtils.toLong(ele.elementText("usergroupid"));
			ug.setUserGroupId(groupId);
			ug.setType(NumberUtils.toInt(ele.elementText("type")));
			ug.setName(ele.elementText("name"));
			ug.setLimitMin(NumberUtils.toInt(ele.elementText("limitmin")));
			ug.setItems(itemMap.get(groupId));
			userGroupsList.add(ug);
		}

		return userGroupsList.toArray(new UserGroup[0]);
	}


	
}

class XMLParser {
	private Document doc = null;
	public XMLParser() {}
	public XMLParser(String sourceFilePath) throws DocumentException {
		SAXReader reader = new SAXReader();
		doc = reader.read(UserRightServiceTest.class.getClassLoader().getResourceAsStream(sourceFilePath));
	}

	public Iterator getIterator(String nodeText) {
		return doc.getRootElement().elementIterator(nodeText);
	}

	public int getLength() {
		return doc.getRootElement().elements().size();
	}
}
  • XMLParser是用来读去并解析xml文件的
  • initUserGroups方法是用来初始化我们需要的数据的

多个测试方法都可以重用这个数组的了