Junit4测试Service中的方法
来源:互联网 发布:如何下载excel2010软件 编辑:程序博客网 时间:2024/06/01 11:30
1.添加pom依赖,如下两个依赖:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.7.RELEASE</version>
<scope>test</scope>
</dependency>
2.创建测试类,范例如下:
package com.iborch.health.service.user;
import com.iborch.health.dao.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import static org.junit.Assert.*;
/**
* Created by lhp on 2017/9/4.
*/
@RunWith(SpringJUnit4ClassRunner.class)//指定执行类
@ContextConfiguration(locations = {"classpath*:spring-servlet.xml"})
//加载配置,将测试置于spring环境当中
public class UserServiceImplTest {
@Autowired
UserService userService;
@Resource
UserMapper userMapper;
@Before
public void setUp() throws Exception {
}
@Test
public void searchById() throws Exception {
User user = userService.searchById("029a34aa-b90a-47bb-acba-47362fd29d69");
System.out.println("测试的结果:返回用户的姓名:"+user.getName());
assertEquals("dap3533", user.getName());
}
}
3.批量执行测试类,找个包建如下类,右键运行,就实现了,@Suite.SuiteClasses({UserServiceImplTest.class})这个注解配置要执行的测试类,多个测试类之间用逗号隔开。
范例:
package com.iborch.health.service.user;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
/**
* Created by lhp on 2017/9/5.
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({UserServiceImplTest.class})
public class RunTest {
//没有任何的逻辑代码,为空
}
- Junit4测试Service中的方法
- junit4测试service-spring
- Junit4中的测试注解
- Junit测试service中的方法
- junit4 测试dao层 和service层
- 如何用JUnit4测试spring service
- junit4中的参数化测试
- Junit4测试ssh中的action
- Junit4测试ssh中的action
- Junit4:测试方法执行顺序
- 关于JUNIT4 中的多线程测试的疑惑
- junit4的测试方法的执行顺序
- JUnit4测试方法异常抛出实例
- JUnit4 测试带参数的方法
- maven下配置junit4测试SSH架构Service层
- Junit4测试
- 测试 [ JUnit4 ]
- JUnit4测试
- 推荐系统(1)——推荐系统概述
- HDU 1010Tempter of the Bone(dfs+剪枝)
- Game Prediction POJ
- codeforce 851 B Arpa and an exam about geometry(思路)
- 初识rabbitmq
- Junit4测试Service中的方法
- SIFT,SURF,ORB,FAST 特征提取算法比较
- LeetCode——104. Maximum Depth of Binary Tree
- 【CUGBACM15级BC第31场 B】hdu 5179 beautiful number
- 简历制作
- 提出一种数据结构支持push和pop操作以及第三种操作findMin,返回最小值,所有操作均以O(1)最坏时间运行
- 模拟JVM内存溢出
- eclipse项目导入GitHub
- spring学笔记七