51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1804|回复: 2
打印 上一主题 下一主题

[原创] junit4 spring集成测试

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2019-2-13 14:54:05 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

很多时候我看见小伙伴这样测试spring的service或者dao,每个类后面写个main,使用new方式加载spring配置文件,获取需要测试的实例,然后对实例进行测试。稍微好一点的在junit测试类里面new加载spring配置文件进行测试。

其实junit测试spring可以很方便的进行。这里会用到spring-test-xxx.jar,junit4的jar。

其中要注意的是:

  1. @RunWith(SpringJUnit4ClassRunner.class)
复制代码

1、如果spring配置文件applicationContext.xml在classpath路径下,即通常的src目录下,这样加载配置文件,用classpath前缀。

  1. @ContextConfiguration(locations = { "classpath:applicationContext.xml" })
复制代码

2、但是在web项目中,有些人喜欢把spring配置文件applicationContext.xml放在WEB-INF目录下,这里不是classpath目录。这种情况可以按如下方式配置:用file前缀,指定配置文件的绝对路径。貌似这种方式不是很友好。

  1. @ContextConfiguration(locations = { "file:F:\\workspace\\web-test\\src\\main\\resources\\"
  2.                 + "applicationContext.xml" })
复制代码

spring配置文件applicationContext.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.         xmlns:context="http://www.springframework.org/schema/context"
  4.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.         xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  7.         http://www.springframework.org/schema/context
  8.         http://www.springframework.org/schema/context/spring-context-3.2.xsd  ">
  9.        
  10.         <context:component-scan base-package="com.wss.lsl.junit.demo" />

  11. </beans>
复制代码

待测试的service,就一个方法,求两个整数的和。

  1. package com.wss.lsl.junit.demo.service;

  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.stereotype.Service;

  5. @Service
  6. public class CalculateService {

  7.         private Logger logger = LoggerFactory.getLogger(getClass());

  8.         /**
  9.          * 计算:返回两个整数的和
  10.          *
  11.          * @param first
  12.          * @param second
  13.          * @return
  14.          */
  15.         public int sum(int first, int second) {

  16.                 logger.info("求和参数:first={}, second={}", first, second);

  17.                 return first + second;
  18.         }
  19. }
复制代码

测试基类,所有其他的测试类集成此类,从而无需再重复配置加载spring配置文件。

  1. package com.wss.lsl.junit.demo;

  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.test.context.ContextConfiguration;
  5. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

  6. @RunWith(SpringJUnit4ClassRunner.class)
  7. @ContextConfiguration(locations = { "classpath:applicationContext.xml" })
  8. public class BaseTest {

  9.         @Test
  10.         public void _() {
  11.         }
  12. }
复制代码

测试类如下,我们可以很方便把要测试的实例@Autowired进来,这样优雅多了。

  1. package com.wss.lsl.junit.demo.service;

  2. import static org.junit.Assert.*;

  3. import org.junit.Test;
  4. import org.springframework.beans.factory.annotation.Autowired;

  5. import com.wss.lsl.junit.demo.BaseTest;

  6. public class CalculateServiceTest extends BaseTest {

  7.         @Autowired
  8.         private CalculateService calculateService;

  9.         @Test
  10.         public void testSum() {
  11.                 int expected = 5, first = 3, second = 2;
  12.                 int real = calculateService.sum(first, second);
  13.                
  14.                 // 验证
  15.                 assertEquals(expected, real);
  16.         }

  17. }
复制代码



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-4-25 00:00 , Processed in 0.062305 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表