統合の春のJUnitテスト

簡単な紹介

ばねは、ばね試験5.2.1.RELEASE.jarはJUnitのを統合することができる提供します。
利点:テストコードを簡素化することができる(手動でコンテキストを作成する必要がない、すなわち手動ばねコンテナを作成します)

JUnitの春とステップを使用して統合

1.インポートジャーパッケージ

2.パッケージcom.igeek.testを作成し、クラスSpringTestを作成

集積ばねJUnitを使用して注釈@RunWith
@ContextConfigurationアノテーションでは、位置は、ばねコンテナを指定しました

オブジェクトをテストする注入@Autowiredアノテーションで3は、
ここでは二つの点に注意してください。

テストオブジェクトは、テストケースに注入されます

configureのテストケースへの必要はありません テストクラスの実行時間の使用は、自動的に注釈をサポートするために開始しますので(テストクラスはのみ有効)

説明する例

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>

サービス層:

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("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();   
    }
}

おすすめ

転載: www.cnblogs.com/hublogs/p/12005596.html