Writing Unit Tests against actual legacy data

Hi Friends,

In today’s discussion, we will talk about writing Unit Tests, but it is not about how to get started with Unit Tests. Rather it is about, if you have a scenario where in you need to construct data set which is very primitive and it needs lot of work to construct even mock data. So, the best thing which i find is you can collect the actual data either from SOAP tool, if it is some kind service response and have the same in xml. And then you can write Unit Tests against that like shown below.

For one of the scenarios. Below, I have collected and constructed my XML.

<?xml version="1.0" encoding="utf-8" ?>
<CustomerResponseDTO>
  <ContractList>
    <Contract>
      <ContractNumber>530000000112</ContractNumber>
      <ContractIdPK>713343129248515901</ContractIdPK>
      <CurrencyType>1000001</CurrencyType>
      <CurrencyValue>USD</CurrencyValue>
      <FrequencyModeValue>B</FrequencyModeValue>
      <BillingValue>10 Days Inv</BillingValue>
      <ServiceProvId>1</ServiceProvId>
      <CompanyNumber>19</CompanyNumber>
      <IssueLocation>US</IssueLocation>
      <ContractLastUpdateDate>2015-05-10 16:14:45.158</ContractLastUpdateDate>
      <ContractLastUpdateUser>mdmadmin</ContractLastUpdateUser>
      <ContractLastUpdateTxId>626143129248478201</ContractLastUpdateTxId>
      <AgreementName>MaxWell</AgreementName>
      <ComponentID>1000944</ComponentID>
      <ContractExtension>
        <CreditLimit>222</CreditLimit>
        <ShippingChargeFlag>Y</ShippingChargeFlag>
        <TaxableFlag>Y</TaxableFlag>
        <CustomerClass>SA</CustomerClass>
        <CreatedBy>DSA</CreatedBy>
        <LinkNumDesc>Test</LinkNumDesc>
        <SalesPerson/>
        <ModifyBy>XMODIFY_BY</ModifyBy>
      </ContractExtension>
      <AdminNativeKey>
        <AdminNativeKey>
          <adminNativeKeyIdPK>712243</adminNativeKeyIdPK>
          <adminContractId>530000</adminContractId>
          <adminFieldNameType>CustomerAccountNumber</adminFieldNameType>
          <adminFieldNameValue>123456</adminFieldNameValue>
          <contractId>7133431</contractId>
          <nativeKeyLastUpdateDate>2015-05-10 16:14:45.266</nativeKeyLastUpdateDate>
          <nativeKeyLastUpdateUser>mdmadmin</nativeKeyLastUpdateUser>
          <nativeKeyLastUpdateTxId>6261431</nativeKeyLastUpdateTxId>
        </AdminNativeKey>
      </AdminNativeKey>
    </Contract>
  </ContractList>
</CustomerResponseDTO>

Then, you can write test case like shown below:-

[TestMethod]
        [DeploymentItem(CILXMLFile)]
        public void Should_Have_Customer_Number()
        {
            var actual = GetSampleData();
            //Assert
            Assert.AreEqual(530000, actual.CustomerNumber);
        }

        [TestMethod]
        [DeploymentItem(CILXMLFile)]
        public void Should_Have_Company_Name()
        {
            var actual = GetSampleData();
            //Assert
            Assert.AreEqual("MaxWell", actual.CompanyName);
        }

Then, you can get the GetSampleData() like shown below.

const string XMLFile = @"TestData/TestData.xml";
        private static T DeserializeTestData<T>(string xmlPath) where T : class
        {
            var serializer = new XmlSerializer(typeof(T));

            using (var reader = new StreamReader(xmlPath))
            {
                return (T)serializer.Deserialize(reader);
            }
        }

        private Adapers.Customer GetSampleData()
        {
            //Arrange
            
            var customerResponseDto = DeserializeTestData<CustomerResponseDTO>("TestData.xml");

            Mock<ICustomerSearchServiceWrapper> mock = new Mock<ICustomerSearchServiceWrapper>();



            mock.Setup(
                      obj =>
                          obj.GetCustomer(It.IsAny<GetCustomerRequestDTO>(), It.IsAny<PagingInput>(),
                              It.IsAny<ClientApplicationInfo>()))
                  .Returns(customerResponseDto);

            var objectUnderTest = new TestSearchServiceAdapter(mock.Object,new Mock<IConfigurableSettings>().Object);
            //Act

            var actual = objectUnderTest.GetAddressDetails("1234567890", "US", "BLG");
            return actual;
        }

With the above changes in place, when i run the same, it will produce the below result.

5th

Thanks for joining me.

Thanks,
Rahul Sahay
Happy Coding

1,821 total views, 1 views today