- TestNG - TestNG - vs JUnit
- TestNG - Plug with Eclipse
- TestNG - Plug with ANT
- TestNG - Parallel Execution
- TestNG - Asserts
- TestNG - Annotation Transformers
- TestNG - Test Results
- TestNG - Run JUnit Tests
- TestNG - Parameterized Test
- TestNG - Dependency Test
- TestNG - Exception Test
- TestNG - Group Test
- TestNG - Ignore a Test
- TestNG - Suite Test
- TestNG - Executing Tests
- TestNG - Execution Procedure
- TestNG - Basic Annotations
- TestNG - Writing Tests
- TestNG - Environment
- TestNG - Overview
- TestNG - Home
TestNG Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
TestNG - Exception Test
TestNG provides an option of tracing the exception handpng of code. You can test whether a code throws a desired exception or not. Here the expectedExceptions parameter is used along with the @Test annotation. Now, let s see @Test(expectedExceptions) in action.
Create a Class
Create a java class to be tested, say, MessageUtil.java in /work/testng/src. Add an error condition inside the printMessage() method.
/* * This class prints the given message on console. */ pubpc class MessageUtil { private String message; //Constructor //@param message to be printed pubpc MessageUtil(String message) { this.message = message; } // prints the message pubpc void printMessage() { System.out.println(message); int a =0; int b = 1/a; } // add "Hi!" to the message pubpc String salutationMessage() { message = "Hi!" + message; System.out.println(message); return message; } }
Create Test Case Class
Create a java test class, say, ExpectedExceptionTest.java in /work/testng/src.
Add an expected exception ArithmeticException to the testPrintMessage() test case.
Following are the contents of ExpectedExceptionTest.java.
import org.testng.Assert; import org.testng.annotations.Test; pubpc class ExpectedExceptionTest { String message = "Manisha"; MessageUtil messageUtil = new MessageUtil(message); @Test(expectedExceptions = ArithmeticException.class) pubpc void testPrintMessage() { System.out.println("Inside testPrintMessage()"); messageUtil.printMessage(); } @Test pubpc void testSalutationMessage() { System.out.println("Inside testSalutationMessage()"); message = "Hi!" + "Manisha"; Assert.assertEquals(message,messageUtil.salutationMessage()); } }
Create Test Runner
Create testng.xml in /work/testng/src to execute test case(s).
<?xml version = "1.0" encoding = "UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name = "Suite1"> <test name = "test1"> <classes> <class name = "ExpectedExceptionTest" /> </classes> </test> </suite>
Compile the MessageUtil, Test case classes using javac.
/work/testng/src$ javac MessageUtil.java TestJunit.java
Now, run the Test Runner, which will run test cases defined in the provided Test Case class.
/work/testng/src$ java org.testng.TestNG testng.xml
Verify the output. testPrintMessage() test case will be passed.
Inside testPrintMessage() Manisha Inside testSalutationMessage() Hi!Manisha =============================================== Suite1 Total tests run: 2, Failures: 0, Skips: 0 ===============================================Advertisements