IDEA Spring环境下的单元测试

前提:Spring环境已经搭建完毕


1.pom.xml文件下添加juint的依赖
 
 
< dependency > < groupId >junit </ groupId > < artifactId >junit </ artifactId > < version >4.12 </ version > < scope >test </ scope > </ dependency >
注意:版本4.0以上,否则无法使用注解机制
2 .pom.xml文件下添加spring-test的依赖
 
 
< dependency > < groupId >org.springframework </ groupId > < artifactId >spring-test </ artifactId > < version >5.0.5.RELEASE </ version > </ dependency >
 切记:把<scope></scope>标签去掉 否则无法使用 @ContextConfiguration
 
3.在Resources文件夹下新建test文件用于存放所有单元测试类,并Mark as test Sources,否则你将遇到一系列令你头疼或蛋疼的问题,比如Cannot resolve symbol @Runwith(),找不到junit相关的包等等.

4.创建BaseTest类,该类是所有单元测试的基础类,并实现@After 和@Before 下的两个方法,用于统计你要测试的方法执行了多长时间,以此判断你的代码的性能。
 
 
import org.junit. After; import org.junit. Before; import org.junit.runner. RunWith; import org.springframework.test.context. ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * Author: aAron * Date: 2018/4/7 13:24 * Description: Spring单元测试类的基础类 */
@RunWith(SpringJUnit4ClassRunner.class)//使用Springjuint4的Runner
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})//指定Spring的配置文件,这样便可以从IOC容器中取出你要使用的对象
public class BaseTest { private long beforeTime; private long afterTime; @Before public void before(){ beforeTime=System. currentTimeMillis(); } @After public void after(){ afterTime=System. currentTimeMillis(); System. out.println( "该方法共执行了:"+( afterTime- beforeTime)+ " 毫秒"); }}


5.创建你的测试方法并继承与BaseTest。

import com.tpk.curd.bean.Department;
import com.tpk.curd.dao.DepartmentMapper;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Author:   aAron
 * Date:     2018/4/7 13:37
 * Description: 测试EmployeeMapperx\相关接口
 */

public class TestEmployeeMapper extends BaseTest {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Test
    public void insertTest(){
        Department department=new Department();
        department.setDepName("研发部");
      departmentMapper.insert(department);
    }
}
6.完美。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您讲解一下Java Spring单元测试的实现,使用的IDE为IntelliJ IDEA。 首先,在项目中添加JUnit和Spring Test依赖,可以在pom.xml文件中添加以下依赖: ``` <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.3.9.RELEASE</version> <scope>test</scope> </dependency> ``` JUnit是Java中最流行的测试框架之一,Spring Test为Spring框架提供了测试支持。 接下来,创建一个测试类。在类上使用`@RunWith(SpringJUnit4ClassRunner.class)`注解,该注解是JUnit提供的一个运行器,可以让测试类在Spring容器环境下运行。 在测试类中使用`@ContextConfiguration`注解来指定Spring配置文件的位置,例如: ``` @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:spring/applicationContext.xml"}) public class UserServiceTest { //... } ``` 其中,`classpath:spring/applicationContext.xml`表示Spring配置文件的位置。 接着,可以使用`@Autowired`注解来注入需要测试的服务或DAO对象,例如: ``` @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:spring/applicationContext.xml"}) public class UserServiceTest { @Autowired private UserService userService; //... } ``` 在测试方法中,可以使用JUnit提供的各种断言方法来验证方法的正确性,例如: ``` @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:spring/applicationContext.xml"}) public class UserServiceTest { @Autowired private UserService userService; @Test public void testGetUserById() { User user = userService.getUserById(1); assertNotNull(user); assertEquals("张三", user.getName()); } } ``` 以上就是Java Spring单元测试的实现过程,希望对您有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值