您的位置:首页 > 编程语言 > Java开发

Spring学习-15:Spring整合Junit

2017-03-31 11:03 274 查看
Spring开发中集成Junit测试。

1、程序中有Junit环境。

右击项目,buildpath,add Libraries:



next,Junit4,finish。

2、导入jar包:Spring与Junit整合的jar包 spring-test-3.2.0.RELEASE.jar

这样功能就添加完成了,下面我们来测试一下这与之前的@Test方式有什么不同:

编写测试类:

package com.js.Test;

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.js.service.UserService;

/**
* 引入了jar包之后,就可以使用一些注解了
* @RunWith(SpringJUnit4ClassRunner.class)写法固定
* @author js
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class SpringTest {
@Autowired
private UserService userService;

@Test
public void demo1(){
userService.sayHello();
}
}

运行测试:



在测试类中直接使用注解加载配置文件、注入Service并进行测试,比之前简便许多。在开发中这种测试方式用的比较普遍。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: