备忘:
Action:需要依赖
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-junit-plugin</artifactId>
<version>2.3.16</version>
<scope>test</scope>
</dependency>
Service:
DAO:
Action:需要依赖
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-junit-plugin</artifactId>
<version>2.3.16</version>
<scope>test</scope>
</dependency>
package action;
import com.opensymphony.xwork2.ActionProxy;
import com.renyuandao.actionImp.SocialContactActionImp;
import com.renyuandao.model.UserFriends;
import org.apache.struts2.StrutsSpringTestCase;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.transaction.TransactionConfiguration;
/**
* 此类测试action,测试功能
* Author: Dream.YnagLong
* Date: 13-12-19
* Time: 下午12:47
* E-mail:410357434@163.com
*/
@ContextConfiguration(locations={"classpath*:applicationContext*.xml",
})
@TransactionConfiguration(transactionManager="transactionManager",defaultRollback=false)
public class TempleActionTest extends StrutsSpringTestCase{
private SocialContactActionImp socialAction;
private ActionProxy proxy;
public void testSocial() throws Exception{
proxy = this.getActionProxy("/socialAction!socialContent.do");
socialAction = (SocialContactActionImp)proxy.getAction();
UserFriends userFriends = new UserFriends();
userFriends.setUserid(17);
userFriends.setFriendid(18);
userFriends.setFstatus(1);
socialAction.setUserFriends(userFriends);
String doaction = socialAction.socialContent();
this.assertEquals("social", doaction);
}
}
Service:
package service;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
/**
* @author DR.YangLong
* spring3集成测试模板类,不允许提交SVN版本库
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath*:applicationContext*.xml",
})
@TransactionConfiguration(transactionManager="transactionManager",defaultRollback=false)
public abstract class SupperServiceTest extends AbstractTransactionalJUnit4SpringContextTests{
}
package service;
import com.renyuandao.manager.service.SuperuserService;
import com.renyuandao.model.UserDept;
import com.renyuandao.model.UserFriends;
import com.renyuandao.model.UserInfo;
import com.renyuandao.service.SocialContactService;
import com.renyuandao.service.UserInfoService;
import com.renyuandao.util.PagerHelper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
/**
* Author: Dream.YnagLong
* Date: 13-12-19
* Time: 下午12:08
* E-mail:410357434@163.com
*/
@RunWith(SpringJUnit4ClassRunner.class)
public class TempleServiceTest extends SupperServiceTest {
//自动注入 @Autowired,推荐使用@Resource
@Resource(name = "service_userinfo")
private UserInfoService userInfoServiceervice;
@Autowired
private SuperuserService superuserService;
@Autowired
private SocialContactService socialContactService;
//涉及事务的方法都要加注解,Rollback属性默认为true,事务不会提交数据库,保护现场请设true
@Transactional
@Rollback(false)
@Test
public void testRegister(){
UserInfo user=new UserInfo();
UserDept dept=new UserDept();
dept.setDept("集成测试");
dept.setDeptadress("都市枫林");
user.setUsername("集成测试");
user.setEmail("444444@163.com");
userInfoServiceervice.addUser(user,dept);
user.getId();
}
@Transactional
@Rollback(false)
@Test
public void testGetList(){
PagerHelper pg=new PagerHelper();
UserInfo user=new UserInfo();
user.setVerifystatus(0);
int total=superuserService.getUserInfoCount(user);
pg.init(total,5);
List<UserInfo> users=superuserService.getUserInfoList(user,pg);
for(UserInfo usera:users){
System.out.println(usera.getVerifystatus());
}
user.getId();
}
@Test
@Transactional
@Rollback(false)
public void testAddFriend(){
UserFriends userFriends=new UserFriends();
userFriends.setFriendid(17);
userFriends.setUserid(18);
userFriends.setFstatus(1);
//更新状态
socialContactService.removeOrBlackFriend(userFriends);
}
}
DAO:
package dao;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
/** 用spring测试DAO,不用加载配置文件获取sqlSession
* Author: Dream.YnagLong
* Date: 13-12-20
* Time: 下午4:17
* E-mail:410357434@163.com
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath*:applicationContext*.xml",
})
@TransactionConfiguration(transactionManager="transactionManager",defaultRollback=false)
public abstract class DaoTestBySpring {
}
package dao;
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import com.renyuandao.dao.UserInfoMapper;
import com.renyuandao.model.UserInfo;
/**
* dao测试类,没有采用spring注入,所有DAO测试可以继承该类进行测试
* @author DR.YangLong
*
*/
public abstract class DaoTest {
// sqlSessionFactory
private static SqlSessionFactory sqlSessionFactory;
// mybatis file
private static final String MYBATIS_CONFIG_FILENAME = "mybatis-config.xml";
static {
Reader reader = null;
try {
reader = Resources.getResourceAsReader(MYBATIS_CONFIG_FILENAME);
} catch (IOException e) {
System.out.println(e.getMessage());
}
// build SqlSessionFactory by SqlSessionFactoryBuilder,the builder used once
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}
// the method get SqlSessionFactory
public static SqlSessionFactory getSqlSessionFactory() {
return sqlSessionFactory;
}
// the test method,this is templates method
@Test
public void addUser() {
// SqlSession
SqlSession sqlSession = getSqlSessionFactory().openSession();
try {
//create dao by SqlSession
UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class);
UserInfo userinfo = new UserInfo();
userinfo.setUsername("Maven");
userinfo.setPasw("thisistest");
userinfo.setBirthday("1999-10-11");
int id=mapper.insertSelective(userinfo);
sqlSession.commit();
System.out.println(id);
} finally {
sqlSession.close();
}
}
}