Click here to Skip to main content
15,891,372 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Unit Testing Serialisation

Rate me:
Please Sign up or sign in to vote.
3.67/5 (3 votes)
22 Oct 2014CPOL 13.9K   2   2
An example of a unity test that tests an object serialises

Introduction

When you have an object that is marked with a <DataContract>, it is easy to miss out one or more of the members that should be serialised by forgetting to mark them with a <DataMember> attributes. Therefore, each member should have a unit test to check that it does perform a round trip.

Using the Code

Given a class which is serializable using a data contract - for example:

VB.NET
<DataContract>
Public NotInheritable Class QueryParameter(Of TValue)

    <DataMember(Name:="ParameterName")>
    ReadOnly m_name As String
    <DataMember(Name:="ParameterIndex")>
    ReadOnly m_index As Integer
    <DataMember(Name:="ParameterValue")>
    ReadOnly m_value As TValue

    ''' <summary>
    ''' The name of the parameter
    ''' </summary>
    ''' <remarks>
    ''' This should be unique in any given query definition, 
    ''' unless there are multuiple indexed properties with the same name
    ''' </remarks>
    Public ReadOnly Property Name As String 
        Get
            Return m_name
        End Get
    End Property

    ''' <summary>
    ''' The index (zero based) of the parameter
    ''' </summary>
    ''' <remarks>
    ''' For a non-indexed parameter, this will always be zero
    ''' </remarks>
    Public ReadOnly Property Index As Integer 
        Get
            Return m_index
        End Get
    End Property

    ''' <summary>
    ''' The value of the parameter
    ''' </summary>
    Public ReadOnly Property Value As TValue
        Get
            Return m_value
        End Get
    End Property

    ''' <summary>
    ''' Creatre a new parameter instance with the given properties
    ''' </summary>
    ''' <param name="nameInit">
    ''' The name of the parameter
    ''' </param>
    ''' <param name="indexInit">
    ''' The zero-based index of the parameter
    ''' </param>
    ''' <param name="valInit">
    ''' The starting value of the parameter 
    ''' this can be Nothing (null) to indicate that the parameter is not set
    ''' </param>
    Private Sub New(ByVal nameInit As String, ByVal indexInit As Integer, ByVal valInit As TValue)
        m_name = nameInit
        m_index = indexInit
        m_value = valInit
    End Sub

    ''' <summary>
    ''' Create a new parameter for the given properties
    ''' </summary>
    ''' <param name="name">
    ''' The name of the parameter
    ''' </param>
    ''' <param name="index">
    ''' The zero-based index of the parameter
    ''' </param>
    ''' <param name="value">
    ''' The value to use for this parameter
    ''' </param>
    Public Shared Function Create(Of TValueType)(ByVal name As String, ByVal index As Integer, ByVal value As TValueType) As QueryParameter(Of TValueType)
        Return New QueryParameter(Of TValueType)(name, index, value)
    End Function

End Class

You can create a unit test to test that the value services a round-trip thus:

VB.NET
<TestMethod>
Public Sub Value_String_Serialisation_RoundTrip()

    Dim expected As String = "This is a test"
    Dim actual As String = "Actual value"

    Dim paramObj As QueryParameter(Of String) =
        QueryParameter(Of String).Create("test", 0, expected)

    Dim ser As New System.Runtime.Serialization.DataContractSerializer(paramObj.GetType())
    Dim ms As New System.IO.MemoryStream(5000)
    ser.WriteObject(ms, paramObj)
    ms.Seek(0, IO.SeekOrigin.Begin)

    Dim paramObjDeserialised As QueryParameter(Of String)
    paramObjDeserialised = ser.ReadObject(ms)

    actual = paramObjDeserialised.Value

    Assert.AreEqual(expected, actual)

End Sub

Points of interest

  • You should always make sure that the actual and expected values are initialised to different values at the start of the unit test and that neither is set to the default for the data type.
  • You should have one unit test for each serialisable property the class contains

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Ireland Ireland
C# / SQL Server developer
Microsoft MVP (Azure) 2017
Microsoft MVP (Visual Basic) 2006, 2007

Comments and Discussions

 
Question[My vote of 1] just why? Pin
TimMahy200324-Oct-14 23:32
TimMahy200324-Oct-14 23:32 
AnswerRe: [My vote of 1] just why? Pin
Duncan Edwards Jones25-Oct-14 9:08
professionalDuncan Edwards Jones25-Oct-14 9:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.