Spring+Spring MVC+mybatisでのjunit 4注入ユニットテスト

1896 ワード

Spring環境でのJUnit 4テスト
1.必要なjarパッケージをダウンロードします.
spring-test-4.0.2.RELEASE.jar
junit-4.11.jar
Maven

			org.springframework
			spring-test
			4.0.2.RELEASE
		
             
			junit
			junit
			4.11
			test
	 

以上の2つのパッケージにspring-testを加える必要がある.JArはspringテスト環境を統合しており、パケットが欠けていると正常に注入できません.一般的なテスト注入が成功しなかったのは、このパッケージが不足したためだ.junitテストパッケージはできるだけ高いバージョンを適用し、私が適用したのは4.11版です.
@RunWith(SpringJUnit4ClassRunner.class)  
  • Springテスト環境Springフレームワークでテストを実行するorg.springframework.test.annotationパッケージには、Java 5以上のバージョンで開発された場合、テストで使用できる一般的なSpring固有の注釈セットが用意されています.
    @ContextConfiguration(locations={"classpath:spring-mvc.xml","classpath:spring-mybatis.xml"})  
  • locations:Springプロファイルの場所を手動で指定し、1つ以上のSpringプロファイルを指定できます.
  • @ContextConfiguration(locations={"spring 1.xml","spring 2.xml"})、locationsパスはclassesフォルダまたはMaven resourcesの下、qかつプロファイルの下のパスはclasspath:で指定されています.そうしないと、特殊な場合はパスの認識が間違っています.
  • inheritLocations:親テストケースクラスのSpringプロファイルを継承するかどうか、デフォルトはtrueです.
  • classpath*:spring-mvc.と書くとxmlとclasspath*:spring-mybatis.xmlはすべてのリソースファイルを表し、まずmainリソースを調べ、testリソース
  • を検索します.
    @RunWith(SpringJUnit4ClassRunner.class)  
    @ContextConfiguration(locations={"classpath:spring-mvc.xml","classpath:spring-mybatis.xml"}) 
    public class cTest {
    
    	@Autowired
    	public AdminService adminService;
    	
    	@Test
    	public void test() {
    		
    		Admin a = new Admin();
    		a.setUsername("user");
    		a.setPassword("sds");
    		adminService.insertSelective(a);
    	}
    
    }

    総括は多くなく、多くの責任を負わないで、文章は単純な個人の総括だけで、よくなければ噴き出さないで、技術は限られていて、間違いがあって面倒を指摘して提出します.本人QQ:3739965070