Spring junit test of Integration

Brief introduction

Spring provides a spring-test-5.2.1.RELEASE.jar can be integrated junit.
Advantages: test code can be simplified (no need to manually create a context, i.e. to create a manual spring containers)

Junit spring and integrated using a step

1. Import jar package

2. Create a package com.igeek.test, create a class SpringTest

@RunWith annotations by using a spring junit integrated
by @ContextConfiguration annotation, the location specified spring containers

3. By @Autowired annotation, inject the object to be tested
here note two points:

The test object is injected into the test case

No need to configure test cases Because the use of the test class run time, will start to support annotations automatically (the test classes enabled only)

An example to illustrate

1. The first is: do not open the scanned notes in the applicationContext.xml

Profiles:

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

    <bean id="userService" class="com.igeek.service.impl.UserServiceImpl"></bean>
</beans>

service layer:

public class UserServiceImpl implements IUserService {

    @Override    
    public void save() { 
        System.out.println("save...");   
    }
}

Test categories:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class Test01 { 
    @Autowired   
    private IUserService userService;
    
    @Test    
    public void test01(){ 
        userService.save();   
    }
}

2. The second: to open the annotation scanning in the applicationContext.xml

Profiles:

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

<!--开启注解扫描-->    
<context:component-scan base-package="com.igeek"></context:component-scan>
</beans>

service layer:

@Service("userService")
public class UserServiceImpl implements IUserService {

    @Override    
    public void save() { 
        System.out.println("save...");   
    }
}

Test categories:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class Test01 { 
    @Autowired   
    private IUserService userService;
    
    @Test    
    public void test01(){ 
        userService.save();   
    }
}

Guess you like

Origin www.cnblogs.com/hublogs/p/12005596.html