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
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Expected exceptions in unit tests

Expected exceptions in unit tests

Denzel D. user avatar by
Denzel D.
·
May. 25, 10 · Interview
Like (0)
Save
Tweet
Share
9.72K Views

Join the DZone community and get the full member experience.

Join For Free

Exceptions can happen in any piece of code. More than that, some of the exceptions can be predicted and tested against. For example, if there is a method performing a division, you can expect that at some point a DivideByZeroException will be triggered.

When using unit tests in .NET, you can specify that a specific exception type is expected vi an attribute. To demonstrate this, I have a sample function:

public int Divide(int a, int b)
{
return a / b;
}

 

It divides two integers, but inside the function itself there is no exception handling and it automatically returns the division value without prior checking. This is a bad way to code something like this, but just to demonstrate the method, it works.

Now, when I create a unit test, I can try creating something like this:

[TestMethod()]
public void DivideTest()
{
Some target = new Some(); // TODO: Initialize to an appropriate value
int a = 12; // TODO: Initialize to an appropriate value
int b = 0; // TODO: Initialize to an appropriate value
target.Divide(a, b);
}

 

Note that Some is the name of the class that holds the function. If I run this test, it will fail due to the fact that an exception will be thrown – DivideByZeroException.

I can add the following attribute to the test method:

[ExpectedException (typeof(DivideByZeroException))]

This is a way to tell the test that you as the developer are aware of such an exception. Even if it appears, the test will still pass. With this attribute being set, try running the above function and you will see that the test passes:
 

If I handle the exception internally, and then define an expected exception, the test will fail, as no exception was thrown, although it is declared in the test. Once you remove the attribute, the test will pass.

I personally wouldn’t recommend over-using this method unless there is a clear need to do so. It is always much better to handle the exceptions in code than let them slide in unit tests. This specific method can mess up some tests if incorrectly used – once it hits an exception, the method will no longer execute. In long term, this might cost an incorrect operation and an eventual problem that will need to be fixed later on.

unit test

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Introduction Garbage Collection Java
  • File Uploads for the Web (2): Upload Files With JavaScript
  • Using Swagger for Creating a PingFederate Admin API Java Wrapper
  • Introduction to Containerization

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
  • +1 (919) 678-0300

Let's be friends: