1、引入相关的包,这里用maven进行管理的
<!--spring test-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version}</version>
<scope>test</scope>
</dependency>
<!-- ============================junit 依赖项定义 begin============================ -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- ============================junit 依赖项定义 end============================ -->
2、编写好业务层、持久层代码;
ibatis 框架写的crud 实例参考
<!--查询-->3、在test的同级目录下新建Test【业务类名】的测试类,里面部分代码实例如下
<select id="findById" resultType="com.ec.test.logic.vo.TestVo" parameterType="String">
SELECT
id,name
FROM t_sys_test
WHERE delFlag ='0'
AND id=#{id}
</select>
<!--逻辑删除-->
<update id="delFlag" parameterType="String">
UPDATE t_sys_test SET
delFlag='1'
where id=#{id}
</update>
<!--物理删除-->
<delete id="delete" parameterType="String">
delete from t_sys_test where id=#{id}
</delete>
<!--更新-->
<update id="update" parameterType="com.ec.test.logic.vo.TestVo">
UPDATE t_sys_test SET
<if test="name != null and name != ''" >
name=#{name},
</if>
<if test="type != null and type != ''" >
type=#{type},
</if>
<if test="status != null and status != ''" >
status=#{status},
</if>
updTime=#{updTime},
updUserId=#{updUserId},
updMark=#{updMark}
where id=#{id}
</update>
<!--插入-->
<insert id="insert" parameterType="com.ec.test.logic.vo.TestVo">
INSERT INTO t_sys_test (id,
name,
type,
status,
delFlag,
addTime,
addUserId,
addMark,
updTime,
updUserId,
updMark,
rec_code
)
VALUES (
#{id},
#{name},
#{type},
#{status},
#{delFlag},
#{addTime},
#{addUserId},
#{addMark},
#{updTime},
#{updUserId},
#{updMark},
#{rec_code}
)
</insert>
package test.impl;
import com.ec.common.utils.DefaultFieldValueUtils;
import com.ec.test.logic.interfaces.TestService;
import com.ec.test.logic.vo.TestVo;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNotNull;
/**
* junit 测试实例
* Created by liuqi on 2017-05-11.
*/
@RunWith(SpringJUnit4ClassRunner.class) //使用junit4进行测试
@ContextConfiguration
({"classpath:spring-context.xml"}) //加载配置文件
//------------如果加入以下代码,所有继承该类的测试类都会遵循该配置,也可以不加,在测试类的方法上///控制事务,参见下一个实例
//这个非常关键,如果不加入这个注解配置,事务控制就会完全失效!
@Transactional
//这里的事务关联到配置文件中的事务控制器(transactionManager = "transactionManager"),同时//指定自动回滚(defaultRollback = true)。这样做操作的数据才不会污染数据库!
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
public class TestTest {
private static final Logger logger = LoggerFactory.getLogger(TestTest.class);
//自动装配spring应用上下文bean
@Autowired
private ApplicationContext context;
TestService testServiceImpl=null;
//测试实例化之前加载服务
@Before
public void before(){
logger.info("获取对象");
testServiceImpl=(TestService)context.getBean("testServiceImpl");
}
@Test //标明是测试方法
@Transactional //标明此方法需使用事务
@Rollback(false) //标明使用完此方法后事务不回滚,true时为回滚
public void testSayHellow( ) {
logger.info(testServiceImpl.sayHellow("simpo"));
}
/*
* 插入
* */
@Test
@Transactional
@Rollback(false)
public void testInsert(){
TestVo vo=new TestVo();
vo.setId("1");
// vo.setId(UUID.randomUUID().toString());
vo.setName("测试1");
vo.setType("1");
vo.setStatus("1");
DefaultFieldValueUtils.setCommonField4Insert(vo,"测试页面","1");
assertEquals(testServiceImpl.insert(vo),1);//判断两个值是否相等
}
/**
* 查询
*/
@Test
@Transactional
@Rollback(false)
public void testfindById( ) {
TestVo tv=testServiceImpl.findById("1");
assertNotNull(tv);//判断对象是否为不为空
}
/*
* 更新
* */
@Test
@Transactional
@Rollback(false)
public void testUpdate(){
TestVo vo=testServiceImpl.findById("1");
// vo.setId(UUID.randomUUID().toString());
vo.setName("测试33");
vo.setType("3");
vo.setStatus("3");
DefaultFieldValueUtils.setCommonField4Update(vo,"测试页面","3");
assertEquals(testServiceImpl.update(vo),1);
}
/*
* 逻辑删除
* */
@Test
@Transactional
@Rollback(false)
public void testDelFlag(){
assertEquals(testServiceImpl.delFlag("1"),1);
}
/*
* 物理删除
* */
@Test
@Transactional
@Rollback(true)
public void testDelete(){
assertEquals(testServiceImpl.delete("1"),1);
}
}