<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>
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(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>