• 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文件中。

    记得快乐
  • 相关阅读:
    4.Windows下安装ZooKeeper
    3. ZAB与Paxos算法的联系与区别。
    2. ZooKeeper的ZAB协议。
    1. 初识ZooKeeper。
    27. Spring Boot 部署与服务配置
    26.SpringBoot事务注解详解
    25.Spring @Transactional工作原理
    24. Spring Boot 事务的使用
    罗辑思维CEO李天田:我们是这样玩儿公司的
    mysql中char,varchar与text类型的区别和选用
  • 原文地址:https://www.cnblogs.com/Y-wee/p/13820316.html
Copyright © 2020-2023  润新知