Spring17_Spring整合Junit8

一、原始Junit测试Spring的问题

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource = applicationContext.getBean(DataSource.class)

 这两行代码的作用是获取容器,如果不写的话,直接会提示空指针异常。所以又不能轻易删掉。

二、上述问题解决思路

  • 让SpringJunit负责创建Spring容器,但是需要将配置文件的名称告诉它
  • 将需要进行测试Bean直接在测试类中进行注入

三、Spring集成Junit步骤

 1. 导入spring集成Junit的坐标:注意也要导入junit坐标

 2. 使用@Runwith注解替换原来的运行期:现在测试找Spring,Spring再找junit

 3. 使用@ContextConfiguration指定配置文件或配置类

 4. 使用@Autowired注入需要测试的对象

 5. 创建测试方法进行测试

四、Spring集成Junit代码实现

 1. 导入spring集成Junit的坐标

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.itheima</groupId>
    <artifactId>itheima_spring_ioc_anno</artifactId>
    <version>1.0-SNAPSHOT</version>


    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.49</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.22.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.2.22.RELEASE</version>
        </dependency>
    </dependencies>
 </project>

 2. 使用@Runwith注解替换原来的运行期

package com.itheima.test;

import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
public class SpringJunitTest {

}

 3.使用@ContextConfiguration指定配置文件或配置类

  @ContextConfiguration("classpath:applicationContext.xml") //加载spring核心配置文件

  @ContextConfiguration(value={"classpath:applicationContext.xml","classpath:test.xml"})

  @ContextConfiguration(classes = {SpringConfiguration.class}) //加载spring核心配置类

package com.itheima.test;

import com.itheima.config.SpringConfiguration;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration("classpath:applicationContext.xml")  //加载spring核心配置文件
@ContextConfiguration(classes = {SpringConfiguration.class}) //加载spring核心配置类
public class SpringJunitTest {

}

 4. 使用@Autowired注入需要测试的对象

package com.itheima.test;

import com.itheima.config.SpringConfiguration;
import com.itheima.service.UserService;
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(classes = {SpringConfiguration.class}) //加载spring核心配置类
public class SpringJunitTest {

    @Autowired
    private UserService userService;

}

 5. 创建测试方法进行测试

  

package com.itheima.test;

import com.itheima.config.SpringConfiguration;
import com.itheima.service.UserService;
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(classes = {SpringConfiguration.class}) //加载spring核心配置类
public class SpringJunitTest {

    @Autowired
    private UserService userService;

    @Test
    public void test1(){
        userService.save();
    }
}

  执行test1,检查控制台输出:

  

 同理注入DataSource也能成功

package com.itheima.test;

import com.itheima.config.SpringConfiguration;
import com.itheima.service.UserService;
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;

import javax.sql.DataSource;
import java.sql.SQLException;

@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration("classpath:applicationContext.xml")  //加载spring核心配置文件
@ContextConfiguration(classes = {SpringConfiguration.class}) //加载spring核心配置类
public class SpringJunitTest {

    @Autowired
    private UserService userService;

    @Autowired
    private DataSource dataSource;

    @Test
    public void test1() throws SQLException {
        userService.save();
        System.out.println(dataSource.getConnection());

    }
}

 

posted on 2023-05-05 11:38  花溪月影  阅读(30)  评论(0编辑  收藏  举报