Spring 使用Junit 进行单元测试

本文介绍了Spring如何使用Junit进行单元测试,包括对测试框架的支持、依赖注入、事务管理,以及@Configration、@DirtiesContext、@TransactionConfigration和@Rollback等测试标签的详细解释。同时,文章还阐述了Spring对junit4标签如@Test(Expected=...)、@Test(timeout=3000)、@Repeat和@ActiveProfiles的兼容和支持,帮助开发者提高测试效率。
摘要由CSDN通过智能技术生成

1.概述

Spring 提供了单元测试的强大支持,主要特性包括:

  • 支持主流的测试框架 Junit 和 TestNG
  • 支持在测试类中使用依赖注入 Denpendency Injection
  • 支持测试类的自动化事务管理
  • 支持使用各种注释标签,提高开发效率和代码简洁性
  • Spring 3.1 更是支持在测试类中使用非 XML 配置方法和基于 Profile 的 bean 配置模式
下面提供一个Spring基于junit的测试用例,数据操作使用Spring data jpa ,因为以前刚好配置过,就拿过来用。

2.具体实现

实体类:User ,简单的Java bean ,就不贴了

dao:
public interface UserDao extends CrudRepository<User, Long>{
	
	
	
	Page<User> findAll(Pageable pageable);
	
	User findById(long id);
	
	User findByName(String name);
	
	User findByLoginName(String loginName);
}

解释:使用data jpa 提供的借口CrudRepository,此接口提供基本的增删改查操作。

Service类

@Service
public class AccountService implements IAccoutService{
	
	@Autowired
	private UserDao userDao;
	
	
	@Transactional
	public void saveUser(User user){
		userDao.save(user);
	}
	
	public List<User> findUserByPage(int page){
		Pageable pageable = new PageRequest(page, 10);
		Page<User> page1 = userDao.findAll(pageable);
		
		return page1.getContent() ;
	}
	
	public User findUserById(long id){
		return userDao.findById(id);
	}
	
	public User findUserByLoginName(String name){
		return userDao.findByLoginName(name);
	}
	
	
	
	Logger logger = LoggerFactory.getLogger(AccountService.class);

}

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd"
	default-lazy-init="true">

	<description>Spring公共配置</description>

	<!-- 读配置数据 -->
	<context:property-placeholder
		ignore-resource-not-found="true" location="classpath*:/application.properties" />
		
	<!-- 注解支持 -->
	<context:annotation-config />

	<!-- 使用annotation 自动注册bean,并检查@Required,@Autowired的属性已被注入 -->
	<context:component-scan base-package="com.blueinfo.jee">
		<context:exclude-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
		<context:exclude-filter type="annotation"
			expression="org.springframework.web.bind.annotation.ControllerAdvice" />
	</context:component-scan>




	<!-- Jpa Entity Manager 配置 -->
	<bean id="entityManagerFactory"
		class="org.springframework.orm.jpa.LocalConta
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值