Spring之junit測試集成


簡介

Spring提供spring-test-5.2.1.RELEASE.jar 可以整合junit。
優勢:可以簡化測試代碼(不需要手動創建上下文,即手動創建spring容器)

使用spring和junit集成的步驟

1.導入jar包

2.創建包com.igeek.test,創建類SpringTest

通過@RunWith注解,使用junit整合spring
通過@ContextConfiguration注解,指定spring容器的位置

3.通過@Autowired注解,注入需要測試的對象
在這里注意兩點:

將測試對象注入到測試用例中

測試用例不需要配置<context:component-scan base-package="com.igeek"/></context:component-scan>,因為使用測試類運行的時候,會自動啟動注解的支持(僅對該測試類啟用)

舉例說明一下

1.第一種:在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"       
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層:

public class UserServiceImpl implements IUserService {

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

測試類:

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

2.第二種:在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"       
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層:

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

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

測試類:

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM