[Spring5.x]Spring5整合JUnit4、整合JUnit5的实例

Spring5整合JUnit4:
在这里插入图片描述
Book.java

package test.entity;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Book {
    @Value("1")
    private int id;
    @Value("李四")
    private String name;

    public Book(int id, String name) {
        this.id = id;
        this.name = name;
    }

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

    public Book() {
    }

}

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

    <!--开启组件扫描-->
    <context:component-scan base-package="test"></context:component-scan>

</beans>

TestJunit4.java:

package test;

import cn.imu.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import test.entity.Book;

@RunWith(SpringJUnit4ClassRunner.class)//单元测试框架,整合JUnit4
@ContextConfiguration("classpath:bean3.xml")//加载配置文件
public class TestJunit4 {
   @Autowired
    private Book book;

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

}

运行结果:
在这里插入图片描述
这里的:
在这里插入图片描述
相当于:
在这里插入图片描述
极大的简化了测试代码,提高了测试效率。
Spring5整合JUnit5:
在这里插入图片描述
TestJunit5.java:

package test;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import test.entity.Book;

@ExtendWith(SpringExtension.class)//单元测试框架,整合JUnit5
@ContextConfiguration("classpath:bean3.xml")//加载配置文件
class TestJunit5 {
    @Autowired
    private Book book;

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


    @Test
    public void test1(){
        ApplicationContext context=new ClassPathXmlApplicationContext("bean3.xml");
        Book book=context.getBean("book",Book.class);
       System.out.println(book);
    }
}

运行测试test函数的结果:
在这里插入图片描述

整合Junit4与Junit5的不同之处:
Junit4:
在这里插入图片描述
Junit5:
在这里插入图片描述
可将2步注解简化为(即使用复合注解整合上述2个注解):
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值