spring 中运用单元测试

直接上代码

package cn.pconline.bbs7.dao;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.*;

import junit.framework.Assert;

import org.apache.commons.lang.time.DateUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.pconline.bbs7.dao.log.LogDao;
import cn.pconline.bbs7.dao.xdb.ModXDB;
import cn.pconline.bbs7.model.Post;
import cn.pconline.bbs7.model.Topic;
import cn.pconline.bbs7.model.User;
import cn.pconline.bbs7.model.log.Log;
import cn.pconline.bbs7.model.log.TopicLog;
import cn.pconline.bbs7.test.TestCommon;
import cn.pconline.bbs7.utils.pager.Pager;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:appConfig/bbs7-context.xml" })
public class PostDaoTest extends AbstractTransactionalJUnit4SpringContextTests {

	private Post post;
	private PostDao postDao;
	private long tid;
	private long uid;

	@Before
	public void setUp() {
		postDao = (PostDao) this.applicationContext.getBean("postDao");
		post = new Post();
		tid = 2l;
		uid = 1l;
		post.setTid(tid);
		post.setUid(uid);
		post.setFloor(1);
		post.setMessage("test");
		post.setStatus(1);
		post.setCreateIp("192.167.57.160");
		post.setUpdateIp("192.167.57.160");
		post.setCreateAt(new Date(Long.parseLong("1345695299364")));
		post.setUpdateAt(new Date(Long.parseLong("1345695299364")));
        post.setAgent(1);
	}

	@Test
	public void testCreatePost() {
		long newPostId = postDao.createPost(post);
		Assert.assertNotNull(newPostId);
		Assert.assertTrue(newPostId > 0);
	}

	
}

结构大概如上所示,测试类要继承AbstractTransactionalJUnit4SpringContextTests。注意上面的注解,

@ContextConfiguration(locations = { "classpath:appConfig/bbs7-context.xml" })

其中bbs7-context.xml在classpath路径下的appConfig文件夹里面,是正常普通的spring配置文件,里面有一些数据源的初始化配置等。

如果是用maven来构建的话,classpath路径位于生成的target文件夹下,如:

源代码生成的classpath:D:\eclipseWorkspace\bbs7\branches\bbs7.0_pie_20130905\target\classes

测试代码生成的classpath:D:\eclipseWorkspace\bbs7\branches\bbs7.0_pie_20130905\target\test-classes

如果spring配置文件在WEB-INF下面,则用下面这个注解:

ContextConfiguration({"file:src/main/webapp/WEB-INF/applicationContext.xml"})  

    package com.sohu.group.service.external;  
      
    import java.util.List;  
      
    import org.junit.Test;  
    import org.junit.runner.RunWith;  
    import org.springframework.beans.factory.annotation.Autowired;  
    import org.springframework.test.context.ContextConfiguration;  
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
      
    @RunWith(SpringJUnit4ClassRunner.class)  
    @ContextConfiguration({"file:src/main/webapp/WEB-INF/applicationContext.xml"})  
    public class SuFriendServiceImplOverRMITest {  
      
        @Autowired  
        private SuFriendService suFriendService;  
          
        @Test  
        public void getUserFollowerListTest(){  
            List<String> list = suFriendService.getUserFollowerList("[email protected]");  
            System.out.println("------"+list);  
        }  
    }  
 

猜你喜欢

转载自breezylee.iteye.com/blog/1990768