Spring 使用Junit 进行单元测试

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
    评论
生成一个使用JUnit进行单元测试Spring boot 工程,可以按照以下步骤进行: 1. 使用Spring Initializr在线工具或者在Eclipse、IntelliJ IDEA等IDE中创建一个Spring boot项目,选择Web、JPA和MySQL等依赖。 2. 在pom.xml文件中添加JUnit和Mockito等测试依赖。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <scope>test</scope> </dependency> ``` 3. 创建一个测试类,使用JUnit和Mockito等工具进行测试。 ```java @RunWith(SpringRunner.class) @SpringBootTest public class UserServiceTest { @MockBean private UserRepository userRepository; @Autowired private UserService userService; @Test public void testGetUserById() { User user = new User(); user.setId(1L); user.setName("Test"); user.setAge(18); Mockito.when(userRepository.findById(1L)).thenReturn(Optional.of(user)); User result = userService.getUserById(1L); Assert.assertEquals(result.getName(), "Test"); Assert.assertEquals(result.getAge(), 18); } } ``` 在这个例子中,使用@RunWith和@SpringBootTest注解来配置测试环境,使用@MockBean注解来模拟依赖的UserRepository对象,使用@Autowired注解来注入需要测试的UserService对象,使用Mockito.when和Assert.assertEquals等方法来进行测试。 4. 运行测试用例,查看测试结果。 在Eclipse、IntelliJ IDEA等IDE中,可以右键点击测试类并选择Run As JUnit Test来运行测试用例。测试结果将会在控制台中输出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值