DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • How To Integrate Microsoft Team With Cypress Cloud
  • Getting Started With the YugabyteDB Managed REST API
  • HashMap Performance Improvements in Java 8
  • Zero Trust Network for Microservices With Istio
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. TestNG @Test Annotation – using expectedExceptions Attribute

TestNG @Test Annotation – using expectedExceptions Attribute

Jagadeesh Motamarri user avatar by
Jagadeesh Motamarri
·
Oct. 15, 13 · Interview
Like (0)
Save
Tweet
Share
14.14K Views

Join the DZone community and get the full member experience.

Join For Free

@Test Annotation provides an attribute “expectedExceptions” allowing the user to specify the type of exceptions that are expected to be thrown by a test method during execution.

This is normally used when you are trying to test certain business use cases and validate input data. A simple example is throwing an exception when the division of 2 numbers is not possible because denominator is 0.

“expectedExceptions” supports multiple values so you can verify different exceptions.

If the exception thrown by the test method is not part of the user entered list, the test method will be marked as failed.

Here is a quick example

Code

Service Class

?
package com.skilledmonster.example;
/**
* Simple calculator service to demonstrate TestNG Framework
*
* @author Jagadeesh Motamarri
* @version 1.0
*/
public interface CalculatorService {
int sum(int a, int b);
int multiply(int a, int b);
int div(int a, int b);
int sub(int a, int b);
}

Service Implementation Class

?
package com.skilledmonster.example;
/**
* Simple calculator service implementation to demonstrate TestNG Framework
*
* @author Jagadeesh Motamarri
* @version 1.0
*/
public class SimpleCalculator implements CalculatorService {
public int sum(int a, int b) {
return a + b;
}
public int multiply(int a, int b) {
return a * b;
}
public int div(int a, int b) {
return a / b;
}
public int sub(int a, int b) {
return a - b;
}
}

Finally, test class using TestNG @Test Annotation’s expectedExceptions attribute

?
package com.skilledmonster.example;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
/**
* Example to demonstrate use of @Test annotation at method level
*
* @author Jagadeesh Motamarri
* @version 1.0
*/
public class TestNGAnnotationExceptionExample {
public CalculatorService service;
@BeforeClass
public void init() {
System.out.println("@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.");
System.out.println("init service");
service = new SimpleCalculator();
}
@Test(expectedExceptions = ArithmeticException.class)
public void testDiv() {
System.out.println("@Test : testDiv()");
int result = service.div(1, 0);
}
@Test(expectedExceptions = ArithmeticException.class)
public void testSum() {
System.out.println("@Test : testDiv()");
int result = service.sum(1, 0);
}
}

Output

 testng_expected_exception_output

As shown in the above console output, testDiv() completed successfully because the method threw an expected exception which is ArithmeticException. On the other hand, testSum() failed because it didn’t throw any exceptions.

Download

 Download expectedExceptions Attribute Example
Testing Attribute (computing) Annotation TestNG Test method

Published at DZone with permission of Jagadeesh Motamarri, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • How To Integrate Microsoft Team With Cypress Cloud
  • Getting Started With the YugabyteDB Managed REST API
  • HashMap Performance Improvements in Java 8
  • Zero Trust Network for Microservices With Istio

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: