Spring Junit4结合使用例子

来源:互联网 发布:赞同科技java笔试题 编辑:程序博客网 时间:2024/05/16 10:15
使用Junit4.4测试
在类上的配置Annotation
@RunWith(SpringJUnit4ClassRunner.class) 用于配置spring中测试的环境
@ContextConfiguration(Locations={"classpath:applicationContext.xml"}) 用于指定配置文件所在的位置。多个配置文件时{"classpath:applicationContextxx.xml","classpath:applicationContextxxx.xml"} 可以导入多个配置文件
@Test标注在方法前,表示其是一个测试的方法 无需在其配置文件中额外设置属性。

例子:SpringJunit4Test

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 com.XX.XXDao; @RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = { "classpath:applicationContext-*.xml" })public class SpringJunitTest{  // 这个注解必须加否则报错 @Autowired  public XXDao xxDao;  @Test public void testGetStrings() {  System.out.println(this.xxDao.findxxByID("2044b8ae-62c0-4bf3-bfa8-75a7d8267df5").size()); }}


使用的Jar包有
junit-4.10.jar,spring-beans-3.1.0.RELEASE.jar,spring-test-3.1.0.RELEASE.jar

学习例子:http://www.ibm.com/developerworks/cn/java/j-lo-springunitest/
原创粉丝点击