Spring框架整合junit:包含配置文件的方式以及纯注解开发的方式

Spring框架整合junit:包含配置文件的方式以及纯注解开发的方式

    • [1. 准备工作](#1. 准备工作)
    • [2. 配置文件的方式](#2. 配置文件的方式)
      • [2.1 未整合前](#2.1 未整合前)
      • [2.2 整合以后](#2.2 整合以后)
    • [3. 纯注解开发的方式](#3. 纯注解开发的方式)
      • [3.1 未整合前](#3.1 未整合前)
      • [3.2 整合后](#3.2 整合后)

Spring框架合集:
Spring框架核心IOC的使用:IOC的作用+Bean管理+实例化Bean的方式+DI依赖注入
Spring框架核心IOC的使用:配置式开发+注解式开发+纯注解式开发

1. 准备工作

添加pom依赖

xml 复制代码
        <!-- spring整合junit -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.2.RELEASE</version>
            <scope>test</scope>
        </dependency>

2. 配置文件的方式

如果不整合junit,那每次在测试类中都要加载配置文件,每次都要 new 一个 ClassPathXmlApplicationContext ,整合之后可以直接调用需要测试类的方法

2.1 未整合前

java 复制代码
public class UserServiceTest {


    //spring ioc 的方式创建对象,ioc是一个map,key-value形式,key是对象的标识,value是ioc创建的对象
    @Test
    public void run1(){
        //创建spring ioc工厂,主要是为了加载spring的配置文件
    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取bean对象

	UserService userService = (UserService) ac.getBean("xx");  //强制类型转换
    userService.hello();


    }

}

2.2 整合以后

这里的@ContextConfiguration("classpath:applicationContext.xml")里面的applicationContext.xml是配置文件,@Autowired是为userService注入值

java 复制代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class UserServiceTest {
    @Autowired
    private UserService userService;

    @Test
    public void run1(){

        userService.hello();


    }

}

3. 纯注解开发的方式

3.1 未整合前

未整合前每次需要new一个AnnotationConfigApplicationContext,创建工厂加载配置类,整合以后可以直接调用需要测试的类的方法

整合前的写法

java 复制代码
public class UserServiceTest {
    @Autowired
    private AccountService accountService;
    @Test
    public void run(){


    //创建工厂,加载配置类
    ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class);
    AccountService accountService = (AccountService) ac.getBean("accountServiceImpl");
    System.out.println(accountService.findAll());

    }



}

3.2 整合后

java 复制代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class UserServiceTest {
    @Autowired
    private AccountService accountService;
    @Test
    public void run(){

    System.out.println(accountService.findAll());

    }



}
相关推荐
我学上瘾了6 小时前
Spring Cloud的前世今生
后端·spring·spring cloud
朝新_10 小时前
【Spring AI 】核心知识体系梳理:从入门到实战
java·人工智能·spring
谁怕平生太急11 小时前
面试题记录:在线数据迁移
java·数据库·spring
云烟成雨TD13 小时前
Spring AI Alibaba 1.x 系列【18】Hook 接口和四大抽象类
java·人工智能·spring
Flittly15 小时前
【SpringSecurity新手村系列】(2)整合 MyBatis 实现数据库认证
java·安全·spring·springboot·安全架构
devilnumber16 小时前
java中Redisson ,jedis,Lettuce和Spring Data Redis的四种深度对比和优缺点详解
java·redis·spring
砍材农夫17 小时前
spring-ai 第十一mcp server调用入门(stdio协议)
人工智能·spring·microsoft
码农阿豪17 小时前
一次 AI 调用 15 万 Token 只花了 $0.058?彻底搞懂 Token、缓存读、补全计费机制!(附完整架构图)
人工智能·spring·缓存
awljwlj17 小时前
黑马点评复习—缓存相关【包含可能的问题和基础知识复习】
java·后端·spring·缓存
庞轩px17 小时前
反射与动态代理——Java语言动态性的核心
java·spring·反射·aop·动态代理·类型