Spring中Junit单元测试的2种姿势

22 篇文章 0 订阅
2 篇文章 0 订阅

平时开发过程中,完成一个功能,都需要单元测试,接口的话,可以通过浏览器或者postman进行接口测试, 有时开发job类功能或者只调整部分代码,junit单元测试就很方便了,接下来简单介绍下。

1、引入基础依赖

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

2、创建测试service类

package org.lee.junit.service;

import org.springframework.stereotype.Service;

@Service
public class UserService {

    public String getUser() {
        return "程序猿";
    }
}

3、创建spring的配置类applicationContext.xml

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 配置组件包扫描的位置 -->
    <context:component-scan base-package="org.lee.junit"/>

</beans>

4、test包下新建Junit测试类

package org.lee.junit;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.lee.junit.config.SpringConfig;
import org.lee.junit.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class UserServiceTest {

    @Autowired
    private UserService userService;

    @Test
    public void test() {
        System.out.println(userService.getUser());
    }
}

注意:如果使用的spring版本小于4.3, 需要使用:

@RunWith(SpringJUnit4ClassRunner.class)

上面的方法是通过xml配置文件的方式,还可以使用Java Config的方式:

新建配置类SpringConfig

package org.lee.junit.config;

import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages = "org.lee.junit")
public class SpringConfig {

}

测试用例中使用如下注解:

@ContextConfiguration(classes = {SpringConfig.class})

此方法也适用于SpringBoot。

由于

@RunWith和@ContextConfiguration 都是可继承的

项目中如果单元测试用例较多,我们可以写个基础测试类BaseTest,然后其他的测试用例继承BaseTest,就可以省略

@RunWith和@ContextConfiguration了

package org.lee.junit;

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class BaseTest {
}

UserServiceTest就可以这样写了:

package org.lee.junit;

import org.junit.Test;
import org.lee.junit.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

public class UserServiceTest extends BaseTest {

    @Autowired
    private UserService userService;

    @Test
    public void test() {
        System.out.println(userService.getUser());
    }
}

好了,spring中junit的大致用法就介绍完了,其他的用法大家有兴趣可以去探究。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值