前言

  单元测试是一个程序员必备的技能,我在这里就不多说了,直接就写相应的代码吧。

单元测试基础类

  

  1. import org.junit.runner.RunWith;
  2. import org.springframework.test.context.ContextConfiguration;
  3. import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
  4. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  5.  
  6. //指定bean注入的配置文件
  7. @ContextConfiguration(locations = { "classpath:/spring/spring_root.xml" ,"classpath:/spring/spring_mvc.xml"})
  8. //使用标准的JUnit @RunWith注释来告诉JUnit使用Spring TestRunner
  9. @RunWith(SpringJUnit4ClassRunner.class)
  10. public class SpringTestCase extends AbstractJUnit4SpringContextTests {
  11.  
  12. }

在这里配置好要加载的配置文件,然后用你写的普通单元测试类继承这个类,然后在相应的方法上加上@Test注解就可以进行单元测试了。

功能复杂的单元测试

例如DAO层,整合好的结构如下

 

首先,在src/test/java中写我们的测试类XXXXTest.java然后在类上面加上注解:

  1. import java.util.Date;
  2. import java.util.List;
  3.  
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.test.context.ActiveProfiles;
  10. import org.springframework.test.context.ContextConfiguration;
  11. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  12. import org.springframework.transaction.annotation.Transactional;
  13.  
  14. import com.macow.home.first.entity.User;
  15. import com.macow.home.first.mapper.UserMapper;
  16.  
  17. @RunWith(SpringJUnit4ClassRunner.class)
  18. @ContextConfiguration("/spring-context.xml")
  19. @ActiveProfiles(value="dev")
  20. @Transactional
  21. public class UserMapperTest {
  22. private Logger logger=LoggerFactory.getLogger(this.getClass());
  23. @Autowired
  24. private UserMapper userMapper;
  25.  
  26. @Test
  27. public void testUserInsert() {
  28. User user=new User();
  29. user.setName("杨过");
  30. user.setPassword("222222");
  31. user.setCreateDate(new Date());
  32. userMapper.insert(user);
  33. logger.info("--------->testUserInsert end-------------");
  34. }
  35.  
  36. @Test
  37. public void testUserSelect() {
  38. List<User> select = userMapper.select(null);
  39. for(User user:select){
  40. logger.info("--------->"+user.getName()+"-------------");
  41. }
  42. logger.info("--------->testUserInsert end-------------");
  43. }
  44. }
@RunWith(SpringJUnit4ClassRunner.class)SpringJUnit支持,由此引入Spring-Test框架支持!
@ContextConfiguration(locations = "classpath:applicationContext.xml") 多个配置文件的话可以用数组表示{“applicationContext.xml”,“applicationContext1.xml”},下面我会贴我的配置文件,只有一个配置文件;
@ContextConfiguration("/spring-context.xml")放在根路径下(即类路径下),然后<import resource="spring-dao.xml" />所有的配置文件和资源文件
@Transactional这个非常关键,如果不加入这个注解配置,事务控制就会完全失效! 
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)这里的事务关联到配置文件中的事务控制器(transactionManager = "transactionManager"),同时指定自动回滚(defaultRollback = true)。这样做操作的数据才不会污染数据库! 
AbstractTransactionalDataSourceSpringContextTests要想构建这一系列的无污染纯绿色事务测试框架就必须找到这个基类!(即所有事务均不生效)
@ActiveProfiles(value="dev")配置环境选择

其次,在src/test/resource目录,我们只要放一个spring-context.xm配置文件,把所有的在src/main/resource下配置文件和资源文件加载进来就可以spring-context.xm里面加载到spring中就可以,所以这个目录一般就一个xml文件,其他都是配置的properties文件。

我的配置文件spring-context.xml:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context.xsd
  9. http://www.springframework.org/schema/aop
  10. http://www.springframework.org/schema/aop/spring-aop.xsd">
  11.  
  12. <!-- <context:property-placeholder location="classpath*:jdbc.properties" /> -->
  13.  
  14. <import resource="spring-dao.xml" />
  15. <beans profile="dev" >
  16. <context:property-placeholder location="classpath*:jdbc-dev.properties" />
  17. </beans>
  18. <beans profile="sit" >
  19. <context:property-placeholder location="classpath*:jdbc-sit.properties" />
  20. </beans>
  21. </beans>

需要注意的地方:
测试方法命名:不能叫test方法,类也不能叫Test类

使用Spring+Junit4进行测试的更多相关文章

  1. 使用Spring+Junit4.4进行测试(使用注解)

    http://nottiansyf.iteye.com/blog/345819 使用Junit4.4测试 在类上的配置Annotation @RunWith(SpringJUnit4ClassRunn ...

  2. 用Spring+Junit4.4进行测试(使用注解)

    http://nottiansyf.iteye.com/blog/345819 使用Junit4.4测试 在类上的配置Annotation @RunWith(SpringJUnit4ClassRunn ...

  3. 使用Spring+Junit4.4进行测试

    http://nottiansyf.iteye.com/blog/345819 使用Junit4.4测试 在类上的配置Annotation @RunWith(SpringJUnit4ClassRunn ...

  4. activemq spring 集成与测试

    1.下载安装activemq 2.pom依赖配置 3.spring配置 4.生产消息,消费消息(同步消费),监听消息(异步消费) 4.测试 5.参考博客 http://www.cnblogs.com/ ...

  5. Spring MVC的测试

    测试是保证软件质量的关键. 与 Spring MVC 相关的测试,主要涉及控制器的测试. 为了测试Web项目通常不需要启动项目,需要一些Servlet相关的一些模拟对象,比如MockMVC.MockH ...

  6. Spring Junit4 接口测试

    Junit实现接口类测试 - dfine.sqa - 博客园http://www.cnblogs.com/Automation_software/archive/2011/01/24/1943054. ...

  7. Spring Boot从入门到放弃-Spring Boot 整合测试

    站长资讯摘要:使用Spring Boot 整合测试,对Controller 中某个方法进行测试或者对Service,Mapper等进行测试,不需要运行项目即可查看运行结果是否和期望值相同,Spring ...

  8. Struts2+Spring+Mybatis+Junit 测试

    Struts2+Spring+Mybatis+Junit 测试 博客分类: HtmlUnit Junit Spring 测试 Mybatis  package com.action.kioskmoni ...

  9. spring Scurity终于测试OK了,复杂的功能还待深入研究!发布出来一起探讨吧!

    spring Scurity终于测试OK了,复杂的功能还待深入研究!发布出来一起探讨吧! 就是因 为研究它,我的个天啦!头都大了一圈!还待修改完整版!我的目标不是每个项目拿到它就能使用!到时再说啦.. ...

随机推荐

  1. 第3章 文件I/O(7)_高级文件操作:存储映射

    8. 高级文件操作:存储映射 (1)概念: 存储映射是一个磁盘文件与存储空间的一个缓存相映射,对缓存数据的读写就相应的完成了文件的读写. (2)mmap和munmap函数 头文件 #include&l ...

  2. Dependency Injection in ASP.NET Web API 2 Using Unity

    What is Dependency Injection? A dependency is any object that another object requires. For example, ...

  3. ElasticSearch 搜索原理

    运行结果:返回5条数据 参考代码ESTestDocumentAPI.java package com.dajiangtai.djt_spider.elasticsearch; import java. ...

  4. PS制作gif动图教程

    之前做过一些动图,时间久了就忘记了,每次心血来潮想做的时候又要重新找资料,网上的教程都不够完整,因此整理了一份完整的教程,针对PS新手(对,没错,就是博主自己哈哈). 准备工作:photoshop.图 ...

  5. 进程之间的数据共享 -----Manager模块

    展望未来,基于消息传递的并发编程是大势所趋 即便是使用线程,推荐做法也是将程序设计为大量独立的线程集合,通过消息队列交换数据. 这样极大地减少了对使用锁定和其他同步手段的需求,还可以扩展到分布式系统中 ...

  6. Nginx-ingress-controller部署

    参考官网https://kubernetes.github.io/ingress-nginx/ 部署pod:nginx-ingress-controller/nginx-default-backend ...

  7. 9.求背景图片左边到#box盒子左边外框侧的距离

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. LINUX漏洞-安全防护--防火墙相关

    漏洞扫描 https://blog.csdn.net/e_Inch_Photo/article/details/79072360 基本安全防范: https://blog.csdn.net/holmo ...

  9. mysq在命令行模式下执行shell命令

    mysql可以在命令行模式下执行shell命令 mysql> help For information about MySQL products and services, visit: htt ...

  10. eclipse运行程序时报java.lang.OutOfMemoryError: Java heap space内存不足问题

    System.setProperty("webdriver.firefox.bin", "D:\\Mozilla Firefox\\firefox.exe"); ...