[size=large]
今天学习了Spring的集成测试,分享给大家学习:
第一种:用传统的TestCase做测试,先说下非Spring的,
首先是一个类Abc中写了add方法,
public class Abc {
public int add(int i,int j ){
System.out.println(i+j);
return i+j;
}
}
接下来对这个方法进行一些测试:
public class AbcTest extends TestCase {
public void testAdd(){
assertEquals(3, new Abc().add(1, 2));
}
}
右键-->run as junit test
在Spring中用传统的TestCase测试,如下:
给出一个要测试的类,AccountManager
@Component
//默认将类中的所有函数纳入事务管理.
@Transactional
public class AccountManager {
private static Logger logger = LoggerFactory.getLogger(AccountManager.class);
private UserDao userDao;
private RoleDao roleDao;
private AuthorityDao authorityDao;
//-- User Manager --//
@Transactional(readOnly = true)
public User getUser(Long id) {
return userDao.get(id);
}
public void saveUser(User entity) {
userDao.save(entity);
}
@Autowired
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Autowired
public void setRoleDao(RoleDao roleDao) {
this.roleDao = roleDao;
}
@Autowired
public void setAuthorityDao(AuthorityDao authorityDao) {
this.authorityDao = authorityDao;
}
}
----------------------测试类--------------
public class TestAccountManager extends TestCase {
public ApplicationContext ctx = null; ①Spring容器引用
private static String[] CONFIG_FILES = { ②Spring配置文件
"classpath:applicationContext.xml"
};
protected void setUp() throws Exception {③启动Spring容器
ctx = new FileSystemXmlApplicationContext(CONFIG_FILES);
}
public void testgetUser() { ④测试方法一
④-1从容器中获取Bean
AccountManager aManager = (AccountManager ) ctx.getBean("accountManager ");
User u = aManager.getUser(1L);
syso(u.getId);
}
public void testsaveUser() { ④测试方法一
④-1从容器中获取Bean
AccountManager aManager = (AccountManager ) ctx.getBean("accountManager ");
User user = new User();
user.setId(10L);
user.setName("test");
User u = aManager.saveUser(user);
syso(u.getId);
}
}
说明----->:这种方法经过测试有的可能不使用,会报错no session ,和延迟加载有关系的。
于是我直接换了第二种:可以解决延迟加载,也不会破坏数据库的。
AbstractTransactionalSpringContextTests
----------测试类2------------------
package com.jlusoft.oss.mytest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.test.AbstractTransactionalSpringContextTests;
import com.jlusoft.oss.entity.account.User;
import com.jlusoft.oss.service.account.AccountManager;
import junit.framework.TestCase;
public class TestAccountManager extends AbstractTransactionalSpringContextTests {
public ApplicationContext ctx = null;
private static String files = "classpath:applicationContext.xml";
private AccountManager aManager;
public void setaManager(AccountManager aManager) {
this.aManager = aManager;
}
@Override
protected String[] getConfigLocations() {
return new String[] { "classpath:applicationContext.xml" };
}
//测试getUser
public void testgetUser() {
User user = aManager.getUser(1L);
assertEquals(user.getName(), "test");
System.out.println(user.getEmail());
System.out.println(user.getName());
}
//测试saveUser
public void testSaveUser(){
User user = new User();
user.setEmail("..@thinkjoy.cn");
user.setId(8L);
user.setName("..");
user.setLoginName("...");
user.setPassword("734cfc93fd961ef53ba0a15dc71a26f6");
aManager.saveUser(user);
System.out.println(user.getId());
}
// 可以设置不执行的方法
private static String[] IGNORED_METHODS = {"testgetUser","testSaveUser"};
@Override
protected boolean isDisabledInThisEnvironment(String testMethodName) {
for (String method : IGNORED_METHODS) {
if (method.equals(testMethodName)) {
return true;
}
}
return false;
}
}
[/size]
今天学习了Spring的集成测试,分享给大家学习:
第一种:用传统的TestCase做测试,先说下非Spring的,
首先是一个类Abc中写了add方法,
public class Abc {
public int add(int i,int j ){
System.out.println(i+j);
return i+j;
}
}
接下来对这个方法进行一些测试:
public class AbcTest extends TestCase {
public void testAdd(){
assertEquals(3, new Abc().add(1, 2));
}
}
右键-->run as junit test
在Spring中用传统的TestCase测试,如下:
给出一个要测试的类,AccountManager
@Component
//默认将类中的所有函数纳入事务管理.
@Transactional
public class AccountManager {
private static Logger logger = LoggerFactory.getLogger(AccountManager.class);
private UserDao userDao;
private RoleDao roleDao;
private AuthorityDao authorityDao;
//-- User Manager --//
@Transactional(readOnly = true)
public User getUser(Long id) {
return userDao.get(id);
}
public void saveUser(User entity) {
userDao.save(entity);
}
@Autowired
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Autowired
public void setRoleDao(RoleDao roleDao) {
this.roleDao = roleDao;
}
@Autowired
public void setAuthorityDao(AuthorityDao authorityDao) {
this.authorityDao = authorityDao;
}
}
----------------------测试类--------------
public class TestAccountManager extends TestCase {
public ApplicationContext ctx = null; ①Spring容器引用
private static String[] CONFIG_FILES = { ②Spring配置文件
"classpath:applicationContext.xml"
};
protected void setUp() throws Exception {③启动Spring容器
ctx = new FileSystemXmlApplicationContext(CONFIG_FILES);
}
public void testgetUser() { ④测试方法一
④-1从容器中获取Bean
AccountManager aManager = (AccountManager ) ctx.getBean("accountManager ");
User u = aManager.getUser(1L);
syso(u.getId);
}
public void testsaveUser() { ④测试方法一
④-1从容器中获取Bean
AccountManager aManager = (AccountManager ) ctx.getBean("accountManager ");
User user = new User();
user.setId(10L);
user.setName("test");
User u = aManager.saveUser(user);
syso(u.getId);
}
}
说明----->:这种方法经过测试有的可能不使用,会报错no session ,和延迟加载有关系的。
于是我直接换了第二种:可以解决延迟加载,也不会破坏数据库的。
AbstractTransactionalSpringContextTests
----------测试类2------------------
package com.jlusoft.oss.mytest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.test.AbstractTransactionalSpringContextTests;
import com.jlusoft.oss.entity.account.User;
import com.jlusoft.oss.service.account.AccountManager;
import junit.framework.TestCase;
public class TestAccountManager extends AbstractTransactionalSpringContextTests {
public ApplicationContext ctx = null;
private static String files = "classpath:applicationContext.xml";
private AccountManager aManager;
public void setaManager(AccountManager aManager) {
this.aManager = aManager;
}
@Override
protected String[] getConfigLocations() {
return new String[] { "classpath:applicationContext.xml" };
}
//测试getUser
public void testgetUser() {
User user = aManager.getUser(1L);
assertEquals(user.getName(), "test");
System.out.println(user.getEmail());
System.out.println(user.getName());
}
//测试saveUser
public void testSaveUser(){
User user = new User();
user.setEmail("..@thinkjoy.cn");
user.setId(8L);
user.setName("..");
user.setLoginName("...");
user.setPassword("734cfc93fd961ef53ba0a15dc71a26f6");
aManager.saveUser(user);
System.out.println(user.getId());
}
// 可以设置不执行的方法
private static String[] IGNORED_METHODS = {"testgetUser","testSaveUser"};
@Override
protected boolean isDisabledInThisEnvironment(String testMethodName) {
for (String method : IGNORED_METHODS) {
if (method.equals(testMethodName)) {
return true;
}
}
return false;
}
}
[/size]