Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Assert to verify that an exception has been thrown?

How do I use Assert (or other Test class) to verify that an exception has been thrown?

like image 484
Alex Avatar asked Jun 01 '09 05:06

Alex


People also ask

How do you assert that an exception has been thrown?

In VS built-in unit testing if you simply want to verify that "any exception" is thrown, but you don't know the type, you can use a catch all: [TestMethod] [ExpectedException(typeof(Exception), AllowDerivedTypes = true)] public void ThrowExceptionTest() { //... }

How do you assert an exception is thrown JUnit?

When using JUnit 4, we can simply use the expected attribute of the @Test annotation to declare that we expect an exception to be thrown anywhere in the annotated test method. In this example, we've declared that we're expecting our test code to result in a NullPointerException.

How do you know if a method throws an exception?

The calculate method should check for an exception and if there is no exception, return the calculated value to the main function i.e. v1+v2 or v1-v2; Else if an exception exists then it should print the error statement and the value that is returned from the calculate method to the main method should be 0.0(Not ...

Can we assert exception in Java?

We can test expected exceptions using JUnit 5 assertThrows assertion. This JUnit assertion method returns the thrown exception, so we can use it to assert exception message too.


2 Answers

For "Visual Studio Team Test" it appears you apply the ExpectedException attribute to the test's method.

Sample from the documentation here: A Unit Testing Walkthrough with Visual Studio Team Test

[TestMethod] [ExpectedException(typeof(ArgumentException),     "A userId of null was inappropriately allowed.")] public void NullUserIdInConstructor() {    LogonInfo logonInfo = new LogonInfo(null, "P@ss0word"); } 
like image 171
Kevin Pullin Avatar answered Oct 09 '22 11:10

Kevin Pullin


Usually your testing framework will have an answer for this. But if it's not flexible enough, you can always do this:

try {     somethingThatShouldThrowAnException();     Assert.Fail(); // If it gets to this line, no exception was thrown } catch (GoodException) { } 

As @Jonas points out, this DOES NOT work for catching a base Exception:

try {     somethingThatShouldThrowAnException();     Assert.Fail(); // raises AssertionException } catch (Exception) {     // Catches the assertion exception, and the test passes } 

If you absolutely must catch Exception, you need to rethrow the Assert.Fail(). But really, this is a sign you shouldn't be hand-writing this; check your test framework for options, or see if you can throw a more meaningful exception to test for.

catch (AssertionException) { throw; } 

You should be able to adapt this approach to whatever you like -- including specifying what kinds of exceptions to catch. If you only expect certain types, finish the catch blocks off with:

} catch (GoodException) { } catch (Exception) {     // not the right kind of exception     Assert.Fail(); } 
like image 42
ojrac Avatar answered Oct 09 '22 12:10

ojrac