zoukankan      html  css  js  c++  java
  • spring整合junit

    spring整合junit

    首先准备好spring项目,新建spring项目可以参考以下资料

    这里就不再阐述怎么新建spring项目了,准备好spring项目环境之后,就可以开始spring整合junit了

    1、导包

    导入junit的jar包,以及整合所需jar包(当我们使用spring5.x版本时,要求junit的jar必须是4.12及以上)

    • 基于maven项目
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.1.1.RELEASE</version>
        <scope>test</scope>
    </dependency>
    

    2、spring基于xml配置整合junit

    2.1、编写测试类

    package com.yl;
    
    import com.yl.service.IUserService;
    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;
    
    @RunWith(SpringJUnit4ClassRunner.class)//替换原有运行器
    @ContextConfiguration(locations={"classpath:bean.xml"})//指定spring配置文件的位置
    public class UserTest {
        @Autowired
        private IUserService userService;
    
        @Test
        public void test01(){
            userService.queryUser();
        }
    
    }
    
    

    3、spring基于纯注解配置整合junit

    3.1、编写测试类

    package com.yl;
    
    import static org.junit.Assert.assertTrue;
    
    import com.yl.service.IUserService;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    @Configuration//指定当前类为spring配置类
    @ComponentScan(basePackages = "com.yl")//指定要扫描的包
    @RunWith(SpringJUnit4ClassRunner.class)//替换原有运行器
    @ContextConfiguration(classes={AppTest.class})//指定配置文件的位置
    public class AppTest {
        @Autowired
        private IUserService userService;//用户业务层对象
    
        @Test
        public void test01(){
            //查询用户
            userService.queryUser();
        }
    
    }
    
    

    4、为什么不把测试类配置到xml中

    在解释这个问题之前,先解除大家的疑虑,配到XML中能不能用呢?

    答案是肯定的,没问题,可以使用。

    那么为什么不采用配置到xml中的方式呢?

    这个原因是这样的:

    ​ 第一:当我们在xml中配置了一个bean,spring加载配置文件创建容器时,就会创建对象。

    ​ 第二:测试类只是我们在测试功能时使用,而在项目中它并不参与程序逻辑,也不会解决需求上的问题,所以创建完了,并没有使用。那么存在容器中就会造成资源的浪费。

    ​ 所以,基于以上两点,我们不应该把测试配置到xml文件中。

    记得快乐
  • 相关阅读:
    JS基本语法---while循环---练习
    JS基本语法---while循环
    JS基础语法---分支语句总结
    Python开发坦克大战
    基于Python的face_recognition库实现人脸识别
    10个Python 初学者必知编码小技巧
    论如何用python发qq消息轰炸虐狗好友
    Python + Selenium +Chrome 批量下载网页代码修改【新手必学】
    Python爬取mc皮肤【爬虫项目】
    Python 分发包中添加额外文件【新手必学】
  • 原文地址:https://www.cnblogs.com/Y-wee/p/13820316.html
Copyright © 2011-2022 走看看