- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to verify an exception that has been thrown in unit testing C#?
There are two ways that we can verify an exception in unit testing.
- Using Assert.ThrowsException
- Using ExpectedException Attribute.
Example
Let us consider a StringAppend method which throws an exception needs to be tested.
using System; namespace DemoApplication { public class Program { static void Main(string[] args) { } public string StringAppend(string firstName, string lastName) { throw new Exception("Test Exception"); } } }
Using Assert.ThrowsException
using System; using DemoApplication; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace DemoUnitTest { [TestClass] public class DemoUnitTest { [TestMethod] public void DemoMethod() { Program program = new Program(); var ex = Assert.ThrowsException<Exception>(() => program.StringAppend("Michael","Jackson")); Assert.AreSame(ex.Message, "Test Exception"); } } }
example, we are calling the StringAppend method using Assert.ThrowsException and exception type and message are validated. So the test case will pass.
Using ExpectedException Attribute
using System; using DemoApplication; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace DemoUnitTest { [TestClass] public class DemoUnitTest { [TestMethod] [ExpectedException(typeof(Exception), "Test Exception")] public void DemoMethod() { Program program = new Program(); program.StringAppend("Michael", "Jackson"); } } }
example, we are using ExpectedException attribute and specifying the type of the expected exception. Since the StringAppend method throw the same type of exception which is mentioned in [ExpectedException(typeof(Exception), "Test Exception")] the test case will pass.
- Related Articles
- How to catch an exception thrown by an async void method in C#?
- How to loop the program after an exception is thrown in java?
- What changes has been introduced in JDK7 related to Exception handling in Java?
- How do you handle an exception thrown by an except clause in Python?
- How to show a figure that has been closed in Matplotlib?
- Unit Testing for C# Code
- How can an exception be thrown manually by a programmer in java?
- How to perform Automated Unit Testing with JavaScript?
- Difference between Unit Testing and Integration Testing
- Difference between Unit Testing and Sandwich Testing
- Difference between Unit Testing and System Testing
- How to determine if an activity has been called by a Notification in Android?
- Unit Testing in Python using Unittest
- Unit Testing using Unittest in Python
- What is Python Unit Testing?

Advertisements