上篇文章中我们介绍了如何在struts环境下,进行模拟action的请求测试,以及我们使用了EasyMock框架,来模拟对象的行为。这篇文章我们会继续介绍spring mvc环境下如何对controller进行单元测试。另外我们带来一种全新的mock框架mockito。
一、准备工作,引入以下maven坐标
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.3.RELEASE</version>
<scope>test</scope>
</dependency>
<!--mockito用于spirng单元测试-->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.2.9</version>
<scope>test</scope>
</dependency>
<!--mockito要依赖eljar包,这个是一定要有的,不然会报异常,见异常图-->
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>
当然别忘记了junit,我们所使用的spring-test框架都是基于junit
<!-- junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
</dependency>
二、整体文件
所有测试文件,均要求放入test目录下(一种规范)
MyControllerTest.java