spring4整合junit測試

1、加入依賴包

1.使用spring4的測試框架須要加入如下依賴包:

JUnit 4

spring-Test (Spring框架中的test包)

spring-aop(Spring框架中的AOP包,spring4整合junit測試須要依賴aop的包了)

使用maven,在基於spring的項目中添加以下依賴:

<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
       <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>

2、建立測試類

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class) // 使用junit4進行測試
@ContextConfiguration(locations = "classpath:applicationContext.xml")//加載配置文件
public class Test1 {

}

解釋所用到的註解

@RunWith 用於指定junit運行環境,是junit提供給其餘框架測試環境接口擴展,爲了便於使用spring的依賴注spring提供了org.springframework.test.context.junit4.SpringJUnit4ClassRunner做爲Junit測試環境

@ContextConfiguration導入配置文件

若是隻有一個配置文件

@ContextConfiguration(locations = "classpath:applicationContext.xml")

若是有多個配置文件

@ContextConfiguration(locations = { "classpath:/applicationContext1.xml", "classpath:/applicationContext2.xml" })

建立本身的測試類

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.huafeng.servicetest.Car;

@RunWith(SpringJUnit4ClassRunner.class) // 使用junit4進行測試
@ContextConfiguration(locations = "classpath:applicationContext.xml")//加載配置文件
public class Test1 {

    @Autowired//spring自動注入
    Car car;

    @Test
    public void funTest(){
        System.out.println(car);
    }
}

控制檯打印輸出

log4j:WARN No appenders could be found for logger (org.springframework.test.context.junit4.SpringJUnit4ClassRunner).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

Car [name=咖菲貓, color=blue]

若是出現了這個異常html

Causedby:java.lang.NoSuchMethodError:org.springframework.cache.ehcache.EhCacheFactoryBean.setMaxEntriesLocalHeap(J)V
    at org.springframework.cache.ehcache.EhCacheFactoryBean.<init>(EhCacheFactoryBean.java:101)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
    ... 75 more

請先註釋掉,具體緣由尚未找到,也能夠調其餘版本java

<dependency>
      <groupId>com.googlecode.ehcache-spring-annotations</groupId>
      <artifactId>ehcache-spring-annotations</artifactId>
      <type>jar</type>
      <version>1.2.0</version>
  </dependency>
相關文章
相關標籤/搜索