在以前没用使用spring的时候我们是如何进行测试的?
在使用单元测试的时候有两种方法,一种是testCase 一种是testSuite
testCase:测试用例,测试一个方法或多个方法
testSuit:测试集合,将其分成一个组进行测试
这里不做详解,在之前我们已经整合了ssm,在实际的开发中要进行单元测试,接下来我们一起学习一下spring中的junit测试
在进行测试之前我们要进行导报操作其中,这里要导一个spring-test的包,要和你的spring其他jar版本相同,可以去Maven仓库中搜索。这里还要注意的是junit 的版本要高于4.11,最低的版本要在4.12,不然会报错!!!
jar包完成之后咱们就可以快乐的junit测试了,步骤如下:
首先要创建一个test文件夹,在里面再创建一个java 文件夹(注:将他变绿,变绿,变绿!!!)如果存在test文件夹可以不创建
在你想要进行测试的方法上鼠标右键选择go to,里面有test选项,记住咱们要用的是junit4测试,不要选错了。
进入之后咱们就要进行测试了,
首先是注解的配置
这里有两个注解很重要@RunWith和@ContextConfiguration
@RunWith:是指明什么测试
@ContextConfiguration:上下文路径的选择
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-mybatis.xml")
这里要说一下为什么是spring-mybatis.xml
我们在测试的时候要测的是增删改查操作所以要用mybatis的配置,如果要测试service的业务操作,我们可以在spring-mybatis.xml中添加扫描包即可,如果不添加的话会出现错误,
错误的大体意思是:没有相应的文件,Autowired是否正确,所以要添加service的扫描包。
基本的步骤流程就到这里了,剩下的就是操作了:
![06c5e4c6-1d2f-eb11-8da9-e4434bdf6706.png](http://p02.5ceimg.com/content/06c5e4c6-1d2f-eb11-8da9-e4434bdf6706.png)
![07c5e4c6-1d2f-eb11-8da9-e4434bdf6706.png](http://p03.5ceimg.com/content/07c5e4c6-1d2f-eb11-8da9-e4434bdf6706.png)
进去选择一下进入junit测试:
package com.zmz.service.impl;
import com.zmz.entity.Books;
import com.zmz.mapper.BooksMapper;
import com.zmz.service.BooksService;
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 java.util.List;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-mapper.xml")
public class BooksServiceImplTest {
@Autowired
private BooksMapper booksMapper;
@Autowired
private BooksService booksService;
@Test
public void getBooksAll() {
List<Books> booksAll = booksMapper.getBooksAll();
System.out.println(booksAll);
}
@Test
public void getBooksall(){
List<Books> booksAll = booksService.getBooksAll();
System.out.println(booksAll);
}
}
getBookAll的运行结果
[Books(bId=1, bName=Java基础, bAuthor=詹姆斯.高斯林, bPrice=20.0, bPublish=阳光出版时, bTotal=200, bCount=200, bTime=2020-01-01, aId=3, iId=0, abc=1, area=Area(aId=3, address=二层东区三号, kId=2, abc=1, k=Kind(kId=2, kName=教育, abc=1)), img=null), Books(bId=2, bName=数据库深入浅出, bAuthor=。。, bPrice=15.0, bPublish=六角丛书, bTotal=100, bCount=100, bTime=2020-01-02, aId=3, iId=0, abc=1, area=Area(aId=3, address=二层东区三号, kId=2, abc=1, k=Kind(kId=2, kName=教育, abc=1)), img=null), Books(bId=3, bName=H5, bAuthor=。。, bPrice=20.0, bPublish=延边教育, bTotal=200, bCount=200, bTime=2020-01-06, aId=3, iId=0, abc=1, area=Area(aId=3, address=二层东区三号, kId=2, abc=1, k=Kind(kId=2, kName=教育, abc=1)), img=null), Books(bId=4, bName=python, bAuthor=。。, bPrice=10.0, bPublish=光明出版社, bTotal=120, bCount=120, bTime=2020-02-19, aId=3, iId=0, abc=1, area=Area(aId=3, address=二层东区三号, kId=2, abc=1, k=Kind(kId=2, kName=教育, abc=1)), img=null), Books(bId=5, bName=C从小白到入坟, bAuthor=青檬, bPrice=20.0, bPublish=数字传媒, bTotal=201, bCount=201, bTime=2020-02-01, aId=3, iId=0, abc=1, area=Area(aId=3, address=二层东区三号, kId=2, abc=1, k=Kind(kId=2, kName=教育, abc=1)), img=null), Books(bId=6, bName=操作系统的春天, bAuthor=NALA, bPrice=50.0, bPublish=数字传媒, bTotal=20, bCount=20, bTime=2020-01-22, aId=3, iId=0, abc=1, area=Area(aId=3, address=二层东区三号, kId=2, abc=1, k=Kind(kId=2, kName=教育, abc=1)), img=null)]