Best way to test exceptions with Assert to ensure they will be thrown

前端 未结 9 1746
悲哀的现实
悲哀的现实 2020-12-02 06:58

Do you think that this is a good way for testing exceptions? Any suggestions?

Exception exception = null;
try{
    //I m sure that an exeption will happen he         


        
9条回答
  •  自闭症患者
    2020-12-02 07:51

    I have a couple of different patterns that I use. I use the ExpectedException attribute most of the time when an exception is expected. This suffices for most cases, however, there are some cases when this is not sufficient. The exception may not be catchable - since it's thrown by a method that is invoked by reflection - or perhaps I just want to check that other conditions hold, say a transaction is rolled back or some value has still been set. In these cases I wrap it in a try/catch block that expects the exact exception, does an Assert.Fail if the code succeeds and also catches generic exceptions to make sure that a different exception is not thrown.

    First case:

    [TestMethod]
    [ExpectedException(typeof(ArgumentNullException))]
    public void MethodTest()
    {
         var obj = new ClassRequiringNonNullParameter( null );
    }
    

    Second case:

    [TestMethod]
    public void MethodTest()
    {
        try
        {
            var obj = new ClassRequiringNonNullParameter( null );
            Assert.Fail("An exception should have been thrown");
        }
        catch (ArgumentNullException ae)
        {
            Assert.AreEqual( "Parameter cannot be null or empty.", ae.Message );
        }
        catch (Exception e)
        {
            Assert.Fail(
                 string.Format( "Unexpected exception of type {0} caught: {1}",
                                e.GetType(), e.Message )
            );
        }
    }
    

提交回复
热议问题