当前位置:首页 > 开发 > 系统架构 > 架构 > 正文

Junit测试

发表于: 2014-01-21   作者:DR-YangLong   来源:转载   浏览次数:
摘要: 备忘: Action:需要依赖       <dependency>             <groupId>org.apache.struts</groupId>   &n
备忘:
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();
		}
	}
}

Junit测试

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
分析 JUnit 框架源代码 理解 JUnit 测试框架实现原理和设计模式 文档选项 <td wi
1、概述 2、软件测试 3、Junit框架 4、Assert 5、TestCase类 6、创建被测试类 package com.ljb.juni
JUnit基础及第一个单元测试实例(JUnit3.8) 单元测试   单元测试(unit testing) ,是指对软件
转载请注明出处:http://blog.csdn.net/lhy_ycu/article/details/45281449 前言 好久没有写来CSDN写
最近在整java,从C#转战java的经历让我无限鄙视java。啰嗦几句:红红的一大片,到处是异常;有了异
第10章 从Maven2中运行JUnit测试 10.1 Maven的功能 Maven不仅仅是一个构建工具,以下是Maven的一些
这里以一个加减乘除的类为例子。 最终工程如下: 1、导入junit的jar,不建议直接使用eclipse 中提供
1.junit generator自动生成测试代码缺省和java类在同一报下,不匹配maven项目标准测试目录。修改设
之前的三篇文章《android基础知识12:android自动化测试03—基于junit的android测试框架01、02、03
本人最近在研究java单元测试技术,有点心得,这里分享给测试同行朋友!那么,今天我们研究的主题是
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号