SSM项目中使用Junit进行单元测试

1、引入jar包
hamcrest-core-1.3.jar
junit-4.12.jar
spring-test-4.3.9.RELEASE.jar
其中要注意spring-test要与Spring版本一致, junit-4.12对应 hamcrest-core-1.3,最好不要改版本号

2、在这里插入图片描述
项目结构如图所示,新建log4j.properties,这是必须的,因为Junit单元测试用到log4j,log4j.properties的内容可自行百度,或参考以下代码:

log4j.rootLogger=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

3.新建测试类
在test包下新建测试类,

package test;

import java.util.List;

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 entity.Course;
import service.CourseService;
 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml") 
public class BaseTest {
 
 //利用依赖注入
	@Autowired
	private CourseService courseService;
	
	@Test
	public void test_getAllCourse() {
		List<Course> courses=courseService.getAllCourse(35);
		System.out.println("查询数量为:"+courses.size());
	}
}

注意:@ContextConfiguration(locations = “classpath:applicationContext.xml”) 指的是Spring配置文件的位置,一定要注意拼写正确,否则可能无法正确找到文件

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值