TestNG exception
以下内容引自: https://howtodoinjava.com/testng/testng-expected-exception-and-expected-message-tutorial/
TestNG – Expected Exception and Expected Message Tutorial
November 20, 2014 by Lokesh Gupta
While writing unit tests there can be certain scenarios where we need to verify that an exception is being thrown by the program during execution. TestNG provides a feature to test such scenarios by allowing the user to specify the type of exceptions that are expected to be thrown by a test methodduring execution. It supports multiple values being provided for verification. If the exception thrown by the test is not part of the user entered list, the test method will be marked as failed.
Let’s create a sample test and learn how exception test works in TestNG.
@Test ( expectedExceptions = { IOException.class, NullPointerException.class } )
Let’s see an example to understand it better.
Example of Expected Exception Test
In below test, we have two test methods i.e. exceptionTestOne() and exceptionTestTwo(). Here exceptionTestOne() throws IOException where as exceptionTestTwo() throws Exception. The expected exception to validate while running these tests is mentioned using the expectedExceptions attribute value while using the Test annotation.
public class ExceptionTestDemo{ @Test(expectedExceptions = { IOException.class }) public void exceptionTestOne() throws Exception { throw new IOException(); } @Test(expectedExceptions = { IOException.class, NullPointerException.class }) public void exceptionTestTwo() throws Exception { throw new Exception(); }} |
Output of above test run is given below:
[TestNG] Running: C:\Users\somepath\testng-customsuite.xmlPASSED: exceptionTestOneFAILED: exceptionTestTwoorg.testng.TestException:Expected exception java.io.IOException but got org.testng.TestException:Expected exception java.io.IOException but got java.lang.Exception at org.testng.internal.Invoker.handleInvocationResults(Invoker.java:1497) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1245) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) at org.testng.TestRunner.privateRun(TestRunner.java:767) at org.testng.TestRunner.run(TestRunner.java:617) at org.testng.SuiteRunner.runTest(SuiteRunner.java:334) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291) at org.testng.SuiteRunner.run(SuiteRunner.java:240) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224) at org.testng.TestNG.runSuitesLocally(TestNG.java:1149) at org.testng.TestNG.run(TestNG.java:1057) at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)Caused by: org.testng.TestException:Expected exception java.io.IOException but got java.lang.Exception at org.testng.internal.Invoker.handleInvocationResults(Invoker.java:1497) at org.testng.internal.Invoker.invokeMethod(Invoker.java:754) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231) ... 16 moreCaused by: java.lang.Exception at com.howtodoinjava.test.ExceptionTestDemo.exceptionTestTwo(ExceptionTestDemo.java:16) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84) at org.testng.internal.Invoker.invokeMethod(Invoker.java:714) ... 18 more=============================================== Default test Tests run: 2, Failures: 1, Skips: 0=============================================== |
As you can see from the test results, exceptionTestTwo() was marked as failed by TestNG during execution. The test failed because the exception thrown by the said method does not match the exception list provided in the expectedExceptions list.
Example of Expected Exception Test with Verifying Message
You can also verify a test based on the exception message that was thrown by the test. Regular expression can also be used to verify the error message, this can be done using .*. Depending upon the position of the regular expression we can use it to do pattern matching such as starts-with, contains, and ends-with while verifying the exception message.
Let’s learn how to write a exception test based on the exception message thrown.
public class ExceptionTestDemo{ @Test(expectedExceptions = { IOException.class }, expectedExceptionsMessageRegExp = "Pass Message test") public void exceptionTestOne() throws Exception { throw new IOException("Pass Message test"); } @Test(expectedExceptions = { IOException.class }, expectedExceptionsMessageRegExp = ".* Message .*") public void exceptionTestTwo() throws Exception { throw new IOException("Pass Message test"); } @Test(expectedExceptions = { IOException.class }, expectedExceptionsMessageRegExp = "Pass Message test") public void exceptionTestThree() throws Exception { throw new IOException("Fail Message test"); }} |
Output of above test run is given below:
[TestNG] Running: C:\Users\somepath\testng-customsuite.xmlPASSED: exceptionTestOnePASSED: exceptionTestTwoFAILED: exceptionTestThreeorg.testng.TestException:Expected exception java.io.IOException but got org.testng.TestException:The exception was thrown with the wrong message: expected "Pass Message test" but got "Fail Message test" at org.testng.internal.Invoker.handleInvocationResults(Invoker.java:1497) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1245) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) at org.testng.TestRunner.privateRun(TestRunner.java:767) at org.testng.TestRunner.run(TestRunner.java:617) at org.testng.SuiteRunner.runTest(SuiteRunner.java:334) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291) at org.testng.SuiteRunner.run(SuiteRunner.java:240) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224) at org.testng.TestNG.runSuitesLocally(TestNG.java:1149) at org.testng.TestNG.run(TestNG.java:1057) at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)Caused by: org.testng.TestException:The exception was thrown with the wrong message: expected "Pass Message test" but got "Fail Message test" at org.testng.internal.Invoker.handleInvocationResults(Invoker.java:1481) at org.testng.internal.Invoker.invokeMethod(Invoker.java:754) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231) ... 16 moreCaused by: java.io.IOException: Fail Message test at com.howtodoinjava.test.ExceptionTestDemo.exceptionTestThree(ExceptionTestDemo.java:21) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84) at org.testng.internal.Invoker.invokeMethod(Invoker.java:714) ... 18 more=============================================== Default test Tests run: 3, Failures: 1, Skips: 0=============================================== |
In above test methods exceptionTestThree() failed because expected message didn’t matched.
TestNG exception的更多相关文章
- selenium+testNG+Ant
好几天没写了,抽时间写下,也好有个总结: 1.selenium+testNG+Ant (1)ant 是构建工具 他的作用就是运行你配置好的东西 而tentng.xml你可以认为他是管理test的一个配 ...
- Java Unit Testing - JUnit & TestNG
转自https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaUnitTesting.html yet another insignifican ...
- Exception in thread "main" java.lang.NoSuchMethodError: org.testng.TestNG.configure(Lorg/testng/CommandLineArgs;)V
TestNG运行时报以下错误: Exception in thread "main" java.lang.NoSuchMethodError: org.testng.TestNG. ...
- testng 失败自动截图
testng执行case failed ,testng Listener会捕获执行失败,如果要实现失败自动截图,需要重写Listener的onTestFailure方法 那么首先新建一个Listene ...
- TestNG 与 Junit的比较
转自 http://www.blogjava.net/fanscial/archive/2005/12/14/23780.html 1. JDK 5 Annotations (JDK ...
- APP接口自动化测试JAVA+TestNG(三)之HTTP接口测试实例
前言 前两篇普及相关基础知识后,本篇主要对举例对国家气象局接口自动化测试进行讲解(Get请求及结果断言),以达到自动化测试入门目的,除了前两篇的一些了解外,需要有一定的JAVA知识(HTTP相 ...
- TestNG BeforeClass BeforeMethod Test AfterClass AfterMethod
http://topmanopensource.iteye.com/blog/1983729 1.TestNG测试注解和Junit注解的不同以及生命周期: TestNG测试的一个方法的生命周期: @B ...
- 基于WebDriver&TestNG 实现自己的Annotation @TakeScreenshotOnFailure
相信用过Selenium WebDriver 的朋友都应该知道如何使用WebDriver API实现Take Screenshot的功能. 在这篇文章里,我主要来介绍对failed tests实现 t ...
- testNg vs junit 4.X @Test
http://www.ibm.com/developerworks/cn/java/j-cq08296/ 一个简单的测试用例 初看起来,JUnit 4 和 TestNG 中实现的测试非常相似.为了更好 ...
随机推荐
- linux下64位汇编的系统调用(2)
知道了syscall调用号之后还不算完,还要搞清楚2件事: 1 每种调用号需要传递哪些参数: 2 调用如何传递参数以及结果如何返回: 第一个问题的答案是: 在linux系统中某个程序执行时进行的系统调 ...
- 记一次线上coredump事故
1.事故背景 上周三凌晨,我负责的某个模块在多台机器上连续发生coredump,幸好发生在业务低峰期,而且该模块提供的功能也不是核心流程功能,所以对线上业务影响比较小.发生coredump后,运维收到 ...
- 布局display属性(一)--【Flex】
一.Flex 布局是什么? Flex 是 Flexible Box 的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性. 任何一个容器都可以指定为 Flex 布局. .box ...
- webpack安装使用
一.安装 1.安装node.js,Webpack 需要 Node.js v0.6 以上支持 2.使用npm(软件包管理 )安装webpack (1)全局安装 npm install webpac ...
- 使用JConsole以及VisualVM进行jvm程序的监控,排错,调优
这里只是做一个备份,便于以后继续. 添加两个感觉好的链接吧: http://www.linuxidc.com/Linux/2015-02/113420.htm http://blog.csdn.net ...
- CSS 弹性容器
该文章为英文原文译文及一些自己的拙见墙裂推荐读原文浏览原文请戳这里 : CSS-STRICKS 弹性布局 (Flexbox Layout) 什么是弹性布局 Flexbox Layout 模块旨在提供一 ...
- WebAPP移动前端性能优化规范和设计指导
- 【转】H.264中的NAL技术
NAL技术 1.NAL概述 NAL全称Network Abstract Layer,即网络抽象层.在H.264/AVC视频编码标准中,整个系统框架被分为了两个层面:视频编码层面(VCL)和网络抽象层面 ...
- Activex、OLE、COM、OCX、DLL之间有什么区别?
来源:http://www.blogjava.net/Jack2007/archive/2008/04/27/196392.html 熟悉面向对象编程和网络编程的人一定对ActiveX ...
- Ocelot中文文档-负载均衡
Ocelot能通过可用的下游服务对每个ReRoute进行负载平衡. 这意味着您可以扩展您的下游服务,并且Ocelot可以有效地使用它们. 可用的负载均衡器的类型是: LeastConnection - ...