Monday, March 25, 2013

Unit Test with Exception

There was a developer came to me, asking me how to perform unit test that check for exception. If the expected exception is thrown, it should consider the test as a success.

We can use ExpectedException attribute to assert exception.

So, here are the sample code:


[TestMethod(), ExpectedException(typeof(ApplicationException),
    "Something is wrong, no exception had been thrown.")]
public void TestMethod1()
{
    //write some code here which will throw ApplicationException
    throw new ApplicationException("Test assert exception");
}

No comments:

Post a Comment

Send Transactional SMS with API

This post cover how to send transactional SMS using the Alibaba Cloud Short Message Service API. Transactional SMS usually come with One Tim...