Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Test Edition
 ExpectedExceptionAttribute Construc...
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Team Test API
ExpectedExceptionAttribute Constructor (Type, String)

Initializes a new instance of the ExpectedExceptionAttribute class with and expected exception type and a message that describes the exception.

Namespace:  Microsoft.VisualStudio.TestTools.UnitTesting
Assembly:  Microsoft.VisualStudio.QualityTools.UnitTestFramework (in Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll)

Visual Basic (Declaration)
Public Sub New ( _
    exceptionType As Type, _
    message As String _
)
Visual Basic (Usage)
Dim exceptionType As Type
Dim message As String

Dim instance As New ExpectedExceptionAttribute(exceptionType, _
    message)
C#
public ExpectedExceptionAttribute(
    Type exceptionType,
    string message
)
Visual C++
public:
ExpectedExceptionAttribute(
    Type^ exceptionType, 
    String^ message
)
JScript
public function ExpectedExceptionAttribute(
    exceptionType : Type, 
    message : String
)

Parameters

exceptionType
Type: System..::.Type

An expected type of exception to be thrown by a method.

message
Type: System..::.String

A message to be attached to the exception.

If exceptionType or message is nullNothingnullptra null reference (Nothing in Visual Basic), a diagnostic message will be sent to a trace listener.

The following class contains the method to test:

C#
using System;

namespace MyCSNamespace
{
    public class DivisionClass
    {
        public int Divide(int numerator, int denominator)
        {
            return numerator / denominator;
        }
    }
}
Visual Basic
Public Class DivisionClass
   Public Function Divide(ByVal numerator As Integer, ByVal denominator As Integer) As Integer
      Return numerator \ denominator
   End Function
End Class

The following test method tests the Divide method of the DivisionClass object. It tests for the existence of a DivideByZeroException.

C#
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MyCSNamespace;

namespace MyCSTestProject
{
    [TestClass()]
    public class DivisionClassTest
    {
        [TestMethod()]
        [ExpectedException(typeof(System.DivideByZeroException), "MyMessage")]
        public void DivideTest()
        {
            DivisionClass target = new DivisionClass();
            int numerator = 4;
            int denominator = 0;
            int actual;
            actual = target.Divide(numerator, denominator);
        }
    }
}
Visual Basic
Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports MyVBProject

<TestClass()> _
Public Class DivisionClassTest
    <TestMethod()> _
    <ExpectedException(GetType(System.DivideByZeroException), "MyMessage")> _
    Public Sub DivideTest()
        Dim target As DivisionClass = New DivisionClass
        Dim numerator As Integer = 4
        Dim denominator As Integer = 0
        Dim actual As Integer
        actual = target.Divide(numerator, denominator)
    End Sub
End Class
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker