Maven简单的配置Junit测试及使用简单的mock
2024-08-20 20:35:39
1、maven依赖配置如下
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>RunJob</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
</plugin>
</plugins>
</build>
2、编写测试代码
public class SimpleTest {
@Test
public void test() {
Assert.assertTrue("a".equals("a"));
} @Test
public void testMock() {
List m = mock(List.class);
m.add(1);
m.clear(); verify(m).add(1);//验证执行过add(1)操作
verify(m).clear();//验证执行过clear操作
} @Test
public void when_thenReturn() {
//mock一个Iterator类
Iterator iterator = mock(Iterator.class);
//预设当iterator调用next()时第一次返回hello,第n次都返回world
when(iterator.next()).thenReturn("hello").thenReturn("world");
//使用mock的对象
String result = iterator.next() + " " + iterator.next() + " " + iterator.next();
//验证结果
assertEquals("hello world world", result);
}
}
最新文章
- 1.把二元查找树转变成排序的双向链表[BST2DoubleLinkedList]
- 虚拟机下samba简单安装配置
- php基础21:上传文件
- 淘宝(阿里百川)手机客户端开发日记第六篇 Service详解(二)
- MySQL(二) —— 数据类型与操作数据表
- Educational Codeforces Round 1(C. Nearest vectors)
- Java学习系列(一)Java的运行机制、JDK的安装配置及常用命令详解
- Ubuntu12.10 下搭建基于KVM-QEMU的虚拟机环境(十三)
- unity3d NGUI入门(描述和使用插件参数)
- [每日一题] OCP1z0-047 :2013-07-19 Rules of Precedence――括号的使用
- 【WCF】错误处理(三):错误协定
- a.变量及基本数据结构
- net user命令详解
- java宜立方商城项目
- Jenkins + Gradle + pgyer + Android自动发布
- yield(),wait(),sleep(),join()
- flex学习笔记 使用函数,显示实时更新的标签
- [Python 从入门到放弃] 1. 列表的基本操作
- 【Asp.net入门06】第一个ASP.NET 应用程序-案例说明
- 使用mysql遇到的异常
热门文章
- Redis字符串类型的操作
- 关于Visual Studio 20**自动添加头部注释信息
- OC开发_代码片段——使用Xib自定义tableViewCell
- ST-LINK使用注意
- 【HTTP header】【Access-Control-Allow-Credentials】跨域Ajax请求时是否带Cookie的设置
- mysql GROUP_CONCAT 用法
- Spring Boot - Building RESTful Web Services
- elk----es settings--logstash--performance---bigdesk---logstash plugin online/offline
- ES_DEVOPS-1
- 【Javascript】Windows下Node.js与npm的安装与配置