Spring IoC container and Bean management 27: Spring Test test module; Integration of spring and JUnit4; (@RunWith,@ContextConfiguration)

Posted by llama on Tue, 21 Dec 2021 12:37:53 +0100

explain:

(1) The rationality of this document is explained as follows:

● in the past, we knew that after each module or function is developed, it needs to be tested in time, and JUnit4 is the test framework we often use;

● naturally, timely testing is also required when developing with Spring framework;

JUnit4 is awesome tool. When testing the code developed by Spring, it also uses JUnit4.

● however, if you want to test the Spring development content, you need to involve IoC container initialization, object injection, etc; That is, how to integrate JUnit 4 and Spring is a problem;

● for this purpose, Spring itself provides the [Spring Test] module, which is used to [use JUnit4 in Spring];

● the main content of this blog is the simple use of the [Spring Test] module; its specific content is [how Spring Test uses JUnit4 to Test the code written based on the Spring framework];

(2) This blog is only a brief introduction, but you should know that the content of this blog is still relatively medium. The content of this blog will be often used in future development; approaching and timely unit testing is a very good habit;

catalogue

1: Introduction to Spring Test module:

2: [Spring] and [JUnit4] integration process

3: Code demonstration

1. Preparations: create a project, introduce dependencies, create a basic class, and create an ApplicationContext XML configuration file;

(1) Create Maven based demonstration project s10;

(2) Introduce Spring dependencies in pom.xml: [Spring context] and [Spring test;]

(3) Create UserDao class and UserService class;

(4) In the resources directory, create the applicationContext.xml file;

 2. Officially start the test

(0) JUnit situation description;

(1) Introduce JUnit dependency in pom.xml;

(2) Under the test package, create test case classes: you need to use [@ RunWith] and [@ ContextConfiguration]; (core!)

1: Introduction to Spring Test module:

  

(1) In the Spring framework, there is a special module, Test module, which is specially used for system testing, as shown in the figure below[ Spring IoC container and Bean management 6: use XML to implement Spring IoC 1: [object instantiation]: instantiate objects based on the construction method, and instantiate objects based on the [default construction method]; ]The following figure is introduced;)

(2) [Spring Test] in daily development, the most commonly used function is [integration with JUnit unit Test framework];

(3) The so-called integration means that [the IoC container can be automatically initialized at the beginning of JUnit unit Test through Spring Test]; this process is completed based on annotations and does not need to [manually initialize the ApplicationContext object] as described above;

(4) [spring test] module, which is often used in daily development;

 

2: [Spring] and [JUnit4] integration process

(1) Step 1: Maven project needs to introduce spring test module;

(2) Step 2: @ RunWith annotation: leave the running process of JUnit4 to Spring. Through this annotation, Spring can take over the control of JUnit4 and complete the initialization of IoC; @ ContextConfiguration annotation: used to describe which configuration file to load during the initialization of IoC container;

(3) Step 3: create a test class to test;

 

3: Code demonstration

1. Preparations: create a project, introduce dependencies, create a basic class, and create an ApplicationContext XML configuration file;

(1) Create Maven based demonstration project s10;

 

(2) Introduce Spring dependencies in pom.xml: [Spring context] and [Spring test;]

<?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.imooc.spring</groupId>
    <artifactId>s10</artifactId>
    <version>1.0-SNAPSHOT</version>

    <repositories>
        <repository>
            <id>aliyun</id>
            <name>aliyun</name>
            <url>https://maven.aliyun.com/repository/public</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.9</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.3.9</version>
        </dependency>
    </dependencies>

</project>

explain:

(1) In[ Spring IoC container and Bean management 4: using XML to implement spring IoC pre-test 1: Spring IoC initial experience 1: IoC container completes [object instantiation]; ]The following situations are introduced for the first time in. After the dependency [spring context] is introduced, it will automatically download 6 related dependencies and underlying dependencies;

(2) However, the [spring test] module needs to be introduced; the version of this module needs to be consistent with the [spring context];

(3) Create UserDao class and UserService class;

 

UserDao class:

package com.imooc.spring.ioc.dao;

public class UserDao {

    /**
     * A test method
     */
    public void insert() {
        System.out.println("call UserDao Medium insert()Method to insert a piece of user data into the database;");
    }
}

UserService class:

package com.imooc.spring.ioc.service;

import com.imooc.spring.ioc.dao.UserDao;

public class UserService {
    private UserDao userDao;

    /**
     * A test method
     */
    public void createUser() {
        System.out.println("call UserService Medium createUser()Method, that is, call the business code to create the user;");
        userDao.insert();
    }
    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
}

explain:

(1) UserDao class and UserService class have nothing to say, just simulate the simple calling relationship

(4) In the resources directory, create the applicationContext.xml file;

applicationContext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="userDao" class="com.imooc.spring.ioc.dao.UserDao"/>
    <bean id="userService" class="com.imooc.spring.ioc.service.UserService">
        <property name="userDao" ref="userDao"/>
    </bean>
</beans>

explain:

(1) Through the strategy of [using XML to implement Spring IoC container], UserDao object and UserService object are created respectively. At the same time, object dependency injection is realized by setter;

 2. Officially start the test

(0) JUnit situation description;

Previously, we all created the SpringApplication entry class to test;  

............................................................

However, in actual development, we often need to test while developing, and sometimes multiple test cases, So, the strategy of creating entrance class to test is obviously awesome. At this point, JUnit4 is a very powerful testing tool (through this test tool, which allows us to develop step by step and step by step); the content of JUnit4 can be referenced. Unit testing and Junit4 ]Contents in the; JUnit is the most commonly used unit testing framework in Java development. It is a very awesome tool.

(1) Introduce JUnit dependency in pom.xml;

(2) Under the test package, create test case classes: you need to use [@ RunWith] and [@ ContextConfiguration]; (core!)

Springtester class:

import com.imooc.spring.ioc.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class SpringTestor {
    @Resource
    private UserService userService;

    @Test
    public void testUserService() {
        userService.createUser();
    }
}

explain:

(1) Code analysis

It is divided into the following steps: first, use [@ RunWith, @ ContextConfiguration] to initialize the IoC container; then, inject the required objects from the IoC container in the current class; finally, call the methods to be tested through the injected objects;

(2) Test run results: