单元测试是开发中必不可少的一部分,是产品代码的重要保证。

Junit和testNG是当前最流行的测试框架,Junit是使用最广泛的测试框架,有兴趣的话自己baidu一下。

testNG基于Junit和Nunit,并引入了很多特性,使其更强大和更易于使用。

有兴趣的可以直接看一下官网:http://testng.org/doc/index.html

用maven可以很方便的构建测试框架。

在这里以maven为例搭建一个测试框架

编辑pom.xml引入必要的jar包

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<powermock.version>1.6.3</powermock.version>
<cobertura.version>2.1.1</cobertura.version>
<sonar.version>5.1</sonar.version>
<junit.version>4.12</junit.version>
</properties> <dependencies>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-testng</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.cobertura</groupId>
<artifactId>cobertura</artifactId>
<version>${cobertura.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.sonar</groupId>
<artifactId>sonar-plugin-api</artifactId>
<version>${sonar.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

基本的框架已经搭建,下面写一个最简单的测试类

很多例子取自<<PowerMock实战手册>><<Instant Mock Testing with PowerMock>>

http://wenku.baidu.com/link?url=hKW9B15IRoqIrjpWKYDeR_cGL9hRgYvbT7-rbds59i19l70qzGeW7x3Tzr24Vwuq-vWHggOq6XOs_saW33Li7E492UlXWM8JIpyKMyhvhwa

待测试类:

public class EmployeeService {    

    public String hello(){
return "Hello word!";
}
}

测试类:

public class EmployeeServiceTest {
@Test
public void hello(){
Assert.assertEquals(employeeService.hello(), "Hello word!");
}
}

这是一个不能再简单的例子了,如果IDE有testNG插件的话,直接右键EmployeeServiceTest→run as→testNG test就可以运行。

结果如下:

[TestNG] Running:
C:\Users\changzhz\AppData\Local\Temp\testng-eclipse--447685701\testng-customsuite.xml PASSED: hello ===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
=============================================== ===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
=============================================== [TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@5f205aa: 344 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@424c0bc4: 47 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@2ff5659e: 196 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@1996cd68: 4 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@27ddd392: 13 ms

或者用maven运行 :mvn clean test

用maven调用时,请确保测试类以Test或testCase结尾

这个例子太简单了,只是简单说明怎么构架,怎么运行。

用maven搭建 testNG+PowerMock+Mockito测试框架的更多相关文章

  1. 利用Maven, 搭建最简单的SpringMVC框架

    本文介绍使用maven搭建SpringMVC最简单的框架程序过程,适合初学者上手. 文章下载

  2. dubbo应用程序的单元测试环境搭建(springtest,powermock,mockito)

    转:http://blog.csdn.net/yys79/article/details/66472797 最近,项目中频繁用到dubbo,而且java工程用引用了几十个关联系统的服务(如用户认证,基 ...

  3. 使用强大的 Mockito 测试框架来测试你的代码

    原文链接 : Unit tests with Mockito - Tutorial 译文出自 : 掘金翻译计划 译者 : edvardhua 校对者: hackerkevin, futureshine ...

  4. 【TestNG测试】TestNG、Maven、testng.xml构建测试工程

    创建一个maven工程 使用Idea创建maven工程 ​ 建立类似如上的工程结构,src/main/java,src/test/java,pom.xml,testng.xml,这里由于我们使用工程是 ...

  5. Maven 搭建与my-app项目测试

    前提条件,安装jdk1.6及以上版本,并配置JAVA_HOME 首先,下载Maven3.2.2,附下载地址:http://mirror.bit.edu.cn/apache/maven/maven-3/ ...

  6. 强大的Mockito测试框架(转)

    1.自动生成Mock类在需要Mock的属性上标记@Mock注解,然后@RunWith中配置Mockito的TestRunner或者在setUp()方法中显示调用MockitoAnnotations.i ...

  7. 强大的Mockito测试框架

    转载:https://blog.csdn.net/dc_726/article/details/8568537 1自动生成Mock类   在需要Mock的属性上标记@Mock注解,然后@RunWith ...

  8. Selenium+Python+jenkins搭建web自动化测测试框架

    python-3.6.2 chrome 59.0.3071.115 chromedriver 2.9 安装python https://www.python.org/downloads/  (Wind ...

  9. 搭建最简单的SpringMVC框架(使用maven)

    本文说明:本文介绍使用maven搭建SpringMVC最简单的框架程序过程,适合初学者上手. 下载链接 点此进入下载链接 1.创建一个maven webapp工程. 2.修改WEB-INF目录下的we ...

随机推荐

  1. android stutio 快捷键

    [F] [F] F2 在错误代码之间切换 F3 往前定位(Shift + F3:往后定位 )有问题 F4\Ctrl+鼠标点击\Ctrl+B 转到定义,查看类继承关系 F5 但不调试进入函数内部. F6 ...

  2. IE7 float:left失效的解决方法

    <div id="a" style="width:500px"> <div id="b" style="widt ...

  3. php之XML转数组函数的方法

    <?/** * xml2array() will convert the given XML text to an array in the XML structure. * Link: htt ...

  4. JQuery 层次选择器

    <!DOCTYPE HTML> <html> <head> <title> 使用jQuery层次选择器 </title> <scrip ...

  5. asp.net 之 数据库导入treeview

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  6. ASP。net treeview xml

    this.TreeView2.ShowLines = false; //显示连接子节点与父节点之间的线条 TreeNodeBinding area = new TreeNodeBinding(); a ...

  7. PetaPoco的几个特性

    在PetaPoco中,Brad并没有定义太多Attribute来修饰Models或Fields.这些为数不多的几个Attribute如下: ColumnAttribute ExplicitColumn ...

  8. bootstrap-validator验证问题总结

    bootstrap-validator是一个优秀的验证器,使用中遇到如下问题,总结如下: 1.<button type="submit" name="submit2 ...

  9. Doolitter分解 三对角矩阵分解 拟三对角分解

    #include <cstdio> #include <cstdlib> #include <algorithm> #include <cmath> # ...

  10. 任意阶幻方(魔方矩阵)C语言实现

    魔方又称幻方.纵横图.九宫图,最早记录于我国古代的洛书.据说夏禹治水时,河南洛阳附近的大河里浮出了一只乌龟,背上有一个很奇怪的图形,古人认为是一种祥瑞,预示着洪水将被夏禹王彻底制服.后人称之为&quo ...