spring5单元测试入门笔记

上一篇-spring5&AOP

第六章 单元测试

6.1 什么是单元测试

概论:当你完成一段业务逻辑之后,想要测试这段代码运行的结果是否与自己的预期一样,但是,整个业务流程又没有写完,又不能运行服务器测试。这时候该怎么办呢?以往简单的方式就是创建一个主线程类 public static void main(String[] args) {} 来测试该逻辑是否正确。这样做虽然能达到效果,但是效率却不高。

案例:小明在写一块对数据进行增删改查的业务逻辑,刚刚写完 数据访问层(dao层)的业务代码,现在想测试这段代码能不能通过测试。那该怎么测试呢? 通过老方法虽然有效,但是却不怎么高级。所以他用了单元测试方法进行测试。现在,通过单元测试达到了预期的结果,然后开始写业务层(service层)这时候又可以在单元测试进行测试。大大的提高了效率。

6.2 案例演示

6.2.1 单元测试(Junit4)

6.2.1.1 Junit4配置(xml)

引入pom.xml 依赖。

<!--        spring容器-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.10.RELEASE</version>
        </dependency>
<!--      junit4  单元测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

<!--        spring 测试-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
package com.lyf.spring5.ioc.entity;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;


@Component
//@Scope("singleton")
//@Scope("prototype")


//@Repository
//@Service
//@Controller

public class Student {
    private String name;
    private Integer age;

    public Student() {
    }

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

创建一个spring配置文件,并创建一个bean对象 student。

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


    <bean class="com.lyf.spring5.ioc.entity.Student">
        <property name="name" value="王二明"/>
        <property name="age" value="18"/>
    </bean>
</beans>

在src目录下创建test目录,并创建一个测试类

import com.lyf.spring5.ioc.entity.Student;
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;

/**
 * @Author: lyf
 * @CreateTime: 2022-10-14
 * @description:
 */
@RunWith(SpringJUnit4ClassRunner.class) //创建spring容器
@ContextConfiguration("classpath:context.xml") // 加载spring配置文件
public class TestJunit4 {

    // 自动注入 student对象
    @Autowired
    Student student;

    /**
     * 测试
     */
    @Test //该注解标记当前方法是一个测试方法,方法旁边的三角符号表示可运行状态
    public void test(){

        System.out.println(student);
    }
}

运行结果
在这里插入图片描述

总结

spring使用单元测试,省略了创建容器的步骤。
使用单元测试,可以省略以下创建容器的代码

        // 第一种创建容器的方式
        new AnnotationConfigApplicationContext(JavaConfig.class);
        // 第二种 创建容器的方式
         new ClassPathXmlApplicationContext("ByNameAndByType.xml");
         // 第三种创建容器的方式
        new FileSystemXmlApplicationContext()
6.2.1.2 Junit4(注解)

依赖不变
创建bean对象

@Component
public class Student {
    private String name;
    private Integer age;

    public Student() {
    }

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

创建配置类 ,该类相对于配置文件,等于容器。


@Configuration // 标记当前类为配置类
@ComponentScan("com.lyf.spring5.ioc")// 扫描该包下的组件
public class JavaConfig {
}

测试代码

import com.lyf.spring5.ioc.entity.JavaConfig;
import com.lyf.spring5.ioc.entity.Student;
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) //创建spring容器
//@ContextConfiguration("classpath:context.xml") // 加载spring配置文件
@ContextConfiguration(classes = {JavaConfig.class})//加载spring配置类
public class TestJunit4 {

    // 自动注入 student对象
    @Autowired
    Student student;

    /**
     * 测试
     */
    @Test //该注解标记当前方法是一个测试方法,方法旁边的三角符号表示可运行状态
    public void test(){
        student.setName("王二明");
        student.setAge(18);
        System.out.println(student);
    }
}

运行结果
在这里插入图片描述

6.2.2 单元测试(Junit5)

6.2.2.1 Junit5(xml)

导入pom依赖

<!--        spring容器-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.10.RELEASE</version>
        </dependency>
        <!--        spring测试模块-->
		        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
<!--        单元测试5-->
   <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>
package com.lyf.spring5.ioc.entity;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * @Author: lyf
 * @CreateTime: 2022-10-12
 * @description:
 */
@Component
//@Scope("singleton")
//@Scope("prototype")


//@Repository
//@Service
//@Controller

public class Student {
    private String name;
    private Integer age;

    public Student() {
    }

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

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


    <bean class="com.lyf.spring5.ioc.entity.Student">
        <property name="name" value="王二明"/>
        <property name="age" value="18"/>
    </bean>
</beans>

测试代码

import com.lyf.spring5.ioc.entity.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

@SpringJUnitConfig(locations = "classpath:context.xml") // 加载spring配置文件
//@ContextConfiguration(classes = {JavaConfig.class})
public class TestJunit5 {

    // 自动注入 student对象
    @Autowired
    Student student;

    /**
     * 测试
     */
    @Test //该注解标记当前方法是一个测试方法,方法旁边的三角符号表示可运行状态
    public void test(){
        System.out.println(student);
    }
}

运行结果
在这里插入图片描述

6.2.2.2 Junit5(注解)

依赖不变

package com.lyf.spring5.ioc.entity;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;


@Component
public class Student {
    private String name;
    private Integer age;

    public Student() {
    }

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

package com.lyf.spring5.ioc.entity;

import org.springframework.context.annotation.*;

@Configuration
@ComponentScan("com.lyf.spring5.ioc")
public class JavaConfig {
}

import com.lyf.spring5.ioc.entity.JavaConfig;
import com.lyf.spring5.ioc.entity.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

/**
 * @Author: lyf
 * @CreateTime: 2022-10-14
 * @description:
 */
//@SpringJUnitConfig(locations = "classpath:context.xml") // 加载spring配置文件
@SpringJUnitConfig(JavaConfig.class)
public class TestJunit5 {

    // 自动注入 student对象
    @Autowired
    Student student;

    /**
     * 测试
     */
    @Test //该注解标记当前方法是一个测试方法,方法旁边的三角符号表示可运行状态
    public void test(){
        student.setName("王二明");
        student.setAge(18);
        System.out.println(student);
    }
}

运行结果
在这里插入图片描述

6.3 注解扩展

• @Test :表示方法是测试方法。但是与JUnit4的@Test不同,他的职责非常单一不能声明任何属性,拓展的测试将会由Jupiter提供额外测试
• @ParameterizedTest :表示方法是参数化测试。
• @RepeatedTest :表示方法可重复执行。
• @DisplayName :为测试类或者测试方法设置展示名称
• @BeforeEach :表示在每个单元测试之前执行
• @AfterEach :表示在每个单元测试之后执行
• @BeforeAll :表示在所有单元测试之前执行
• @AfterAll :表示在所有单元测试之后执行
• @Tag :表示单元测试类别,类似于JUnit4中的@Categories
• @Disabled :表示测试类或测试方法不执行,类似于JUnit4中的@Ignore
• @Timeout :表示测试方法运行如果超过了指定时间将会返回错误
• @ExtendWith :为测试类或测试方法提供扩展类引用
import org.junit.jupiter.api.Test; //注意这里使用的是jupiter的Test注解!!


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring的Ioc Spring的AOP , AspectJ Spring的事务管理 , 三大框架的整合 目录 1.1 Spring 框架学习路线:...........................................................................................................................4 1.2 Spring 框架的概述:...............................................................................................................................4 1.2.1 什么是 Spring:...........................................................................................................................4 1.2.2Spring 的核心:............................................................................................................................5 1.2.3Spring 的版本:............................................................................................................................5 1.2.4EJB:企业级 JavaBean.................................................................................................................5 1.2.5Spring 优点:.................................................................................................................................5 1.3 Spring入门的程序:...........................................................................................................................6 1.3.1 下载 Spring 的开发包:..............................................................................................................6 1.3.2 创建 web 工程引入相应 jar 包:................................................................................................7 1.3.3 创建 Spring 的配置文件:..........................................................................................................7 1.3.4 在配置中配置类:.......................................................................................................................9 1.3.5 创建测试类:...............................................................................................................................9 1.3.6IOC 和 DI(*****)区别?..............................................................................................................9 1.3.7Spring 框架加载配置文件:......................................................................................................10 1.3.8BeanFactory 与ApplicationContext 区别?..............................................................................10 1.3.9MyEclipse 配置XML 提示:.....................................................................................................11 1.4 IOC 装配 Bean:....................................................................................................................................12 1.4.1Spring 框架Bean 实例化的方式:............................................................................................12 无参数构造方法的实例化:.......................................................................................................12 静态工厂实例化:.......................................................................................................................13 实例工厂实例化:.......................................................................................................................14 1.4.2Bean 标签的其他配置:.............................................................................................................15 id 和 name 的区别:....................................................................................................................15 类的作用范围:...........................................................................................................................15 Bean 的生命周期:......................................................................................................................18 1.4.3Bean 中属性注入:.....................................................................................................................23 构造器注入:...............................................................................................................................23 setter 方法注入:.........................................................................................................................24 setter 方法注入对象属性:.........................................................................................................25 名称空间 p:注入属性:...............................................................................................................25 SpEL:属性的注入(Spring 表达式):.....................................................................................25 1.4.4 集合属性的注入:.....................................................................................................................27 1.4.5 加载配置文件(文件的分离):.............................................................................................29 1.5 IOC 装配 Bean(注解方式)..................................................................................................................29 1.5.1Spring 的注解装配 Bean..........................................................................................................29 1.5.2Bean 的属性注入:.....................................................................................................................31 1.5.3Bean 其他的属性的配置:.........................................................................................................32 1.5.4Spring3.0 提供使用 Java 类定义 Bean 信息的方法(一般不用)......................................32 1.5.5 传统 XML 和注解的混合使用...............................................................................................34 1.5.6 实际开发中使用 XML 还是注解?.........................................................................................36 盲目的拾荒者2015-2016(泣血总结) 牛刚 第 2 页 共 119 页 1.6 Spring 整合web 开发:........................................................................................................................37 1.7 Spring 集成JUnit 测试:......................................................................................................................40 今天的内容总结:...............................................................................................................................................41 1.8 上次课的内容回顾:............................................................................................................................43 1.9 AOP 的概述:........................................................................................................................................43 1.9.1 什么是 AOP:............................................................................................................................43 1.9.2SpringAOP 思想........................................................................................................................44 1.9.3AOP 底层原理;.........................................................................................................................44 1.9.4Spring 的 AOP 代理:................................................................................................................44 1.9.5AOP 的术语:.............................................................................................................................44 1.10 AOP 的底层实现...............................................................................................................................45 1.10.1JDK 动态代理:........................................................................................................................45 1.10.2CGLIB 动态代理:...................................................................................................................48 1.10.3spring 代理知识总结:.............................................................................................................51 1.11 Spring 中的AOP...............................................................................................................................51 1.11.1Spring 的传统 AOP:..............................................................................................................51 1.11.2Spring 中的切面类型:............................................................................................................51 1.11.3Spring 的AOP 的开发:..........................................................................................................52 针对所有方法的增强:(不带有切点的切面)............................................................................52 带有切点的切面:(针对目标对象的某些方法进行增强)........................................................55 1.11.4 自动代理:...............................................................................................................................58 BeanNameAutoProxyCreator :按名称生成代理......................................................................59 DefaultAdvisorAutoProxyCreator :根据切面中定义的信息生成代理...................................60 1.12 Spring 的AspectJ 的 AOP(重点).....................................................................................................62 1.12.1 基于注解:...............................................................................................................................63 AspectJ 的通知类型:.................................................................................................................65 切点的定义:(真正那些方法增强).......................................................................................67 1.12.2 基于XML:.............................................................................................................................67 1.13 Spring 的JdbcTemplate....................................................................................................................70 1.13.1Spring 对持久层技术支持:....................................................................................................71 1.13.2 开发JDBCTemplate 入门:....................................................................................................71 1.13.3 配置连接池:...........................................................................................................................71 Spring 默认的连接池:...............................................................................................................71 DBCP 连接池:............................................................................................................................72 C3P0 连接池:.............................................................................................................................73 1.13.4 参数设置到属性文件中:.......................................................................................................74 1.13.5JdbcTemplate 的 CRUD 的操作:(学会用手册)....................................................................75 今天的内容总结:...............................................................................................................................................83 今日内容....................................................................................................................................................85 上次课的内容回顾:...................................................................................................................................85 1.14 Spring 的事务管理:...........................................................................................................................87 1.14.1 事务:.......................................................................................................................................87 Spring 学习笔记 2015-2016(泣血总结) 牛刚 第 3 页 共 119 页 1.14.2Spring 中事务管理:................................................................................................................87 Spring 提供事务管理API:........................................................................................................87 1.14.3Spring 的事务管理:................................................................................................................89 1.14.4 事务操作的环境搭建:...........................................................................................................89 1.14.5Spring 的事务管理:................................................................................................................94 手动编码的方式完成事务管理:...............................................................................................94 声明式事务管理:(原始方式)....................................................................................................95 声明式事务管理:(自动代理.基于切面**重点掌握 ssh 整合用的就是这个**,基于 tx/aop)97 基于注解的事务管理:...............................................................................................................99 1.15 SSH 框架整合:................................................................................................................................100 1.15.1Struts2+Spring+Hibernate 导包............................................................................................100 1.15.2Struts2 和 Spring 的整合:.....................................................................................................104 1.15.3Struts2 和 Spring 的整合两种方式:.....................................................................................106 Struts2 自己管理 Action:(方式一)..........................................................................................106 Action 交给 Spring 管理:(方式二)..........................................................................................106 Web 层获得 Service:...............................................................................................................107 1.15.4Spring 整合Hibernate:..........................................................................................................110 零障碍整合:(一)......................................................................................................................110 没有 Hibernate 配置文件的形式(二).....................................................................................112 1.15.5HibernateTemplate 的API:...................................................................................................113 1.15.6OpenSessionInView:.............................................................................................................115 1.16 基于注解的方式整合 SSH:...........................................................................................................115

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值