Aravind
Aravind

Reputation: 1549

ExpectedException attribute fails to show the expected result C#

I have created a Class library .

I have created unit test project to unit test a class.

I have gone through a particular class called

  " ExpectedExceptionAttribute Class "

I tried to implement it. But if i enable the attribute my code always shows failed. Even if data are correct.

Class :

public class SampleTestClass
{
    public double CheckValidAmount(object Name , double amount)
    {
        try
        {
            if (amount == 1.0 && Name.ToString() == "RamKumar")
                return 10.0 - amount;
            else
                return amount;
        }
        catch (NullReferenceException ex)
        {
            return amount;
        }
    }
}

Unit Test Pass :

 [TestClass]
 public class SampleTester
 {
    [TestMethod]
    public void CheckValidAmount()
    {
        SampleTestClass sp = new SampleTestClass();
        double dd = sp.CheckValidAmount("RamKumar", 1.0);

        Assert.AreEqual(dd, 9.0);
    }
}

Fail :

[TestClass]
public class SampleTester
{
    [TestMethod]
    [ExpectedException(typeof(NullReferenceException))]
    public void CheckValidAmount()
    {
        SampleTestClass sp = new SampleTestClass();
        double dd = sp.CheckValidAmount(null, 1.0); // here i have mentioned NULL
        Assert.AreEqual(dd, 9.0);
    }
}

Expected to be passed :

[TestClass]
public class SampleTester
{
    [TestMethod]
    [ExpectedException(typeof(NullReferenceException))]
    public void CheckValidAmount()
    {
        SampleTestClass sp = new SampleTestClass();
        double dd = sp.CheckValidAmount("RamKumar", 1.0);

        Assert.AreEqual(dd, 9.0);
    }
}

Last one should be passed .. But it always shows Failed...

Reference :

   ExpectedExceptionAttribute Class : 
   https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.expectedexceptionattribute.aspx

My VS shows this message :

         ![enter image description here][1]

Kindly guide me to understand this.. Thanks

https://i.sstatic.net/G2R1b.png

Upvotes: 4

Views: 1886

Answers (3)

Old Fox
Old Fox

Reputation: 8725

You should use ExpectedExceptionAttribute when you want to verify that your method throw the correct exception. In your case you don't throw any exception(from your method). Your method behavior is:

Given amount is 1.0

When I check amount with invalid name(NULL name)

Then I should get 1.0 as amount

As you can see from my GWT your method behavior doesn't expect to throw any exception. Therefore your test failed.

    [TestMethod]
    [ExpectedException(typeof(NullReferenceException))]// remove this attribute to pass the test 
    public void CheckValidAmount()
    {
        SampleTestClass sp = new SampleTestClass();
        double dd = sp.CheckValidAmount("RamKumar", 1.0);

        Assert.AreEqual(dd, 9.0); 
    }

You should use ExpectedExceptionAttribute when your method behavior is similar to:

Given amount is 1.0

When I check amount with invalid name(NULL name)

Then an ArgumentNullException should be raise

In this case your method is:

    public double CheckValidAmount(object Name, double amount)
    {
            if (Name == null)
                throw new ArgumentNullException("Name");

            if (amount == 1.0 && Name.ToString() == "RamKumar")
                return 10.0 - amount;

            return amount;
    }

And your test method should be:

    [TestMethod]
    [ExpectedException(typeof(ArgumentNullException))]
    public void CheckValidAmount_CallWithInvalidName_Throw()
    {
        SampleTestClass sp = new SampleTestClass();
        sp.CheckValidAmount("RamKumar", 1.0);

    }

Upvotes: 3

Leo
Leo

Reputation: 14850

This test...

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void CheckValidAmount()
{
    SampleTestClass sp = new SampleTestClass();
    double dd = sp.CheckValidAmount("RamKumar", 1.0);

    Assert.AreEqual(dd, 9.0);
}

will never pass for the following implementation...

public double CheckValidAmount(object Name , double amount)
{
    try
    {
        if (amount == 1.0 && Name.ToString() == "RamKumar")
            return 10.0 - amount;
        else
            return amount;
    }
    catch (ArgumentNullException ex)
    {
        return amount;
    }
}

because the test is expecting a ArgumentNullException, however, that implementation doesn't throw the argument for the given implementation. Remove the ExpectedException attribute and your test should pass

Upvotes: 0

Stefan Steinegger
Stefan Steinegger

Reputation: 64648

Your unit under test doesn't throw an exception, because you catch it inside. So the unit test doesn't see it.

Decide whether to throw the exception and test it like this or catch it and do not expect an exception.

Upvotes: 2

Related Questions