Writing Unit Tests against actual Legacy Data

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 found 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 as in the following.

For one of the scenarios I have collected and constructed my XML as in the following code snippet.

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <CustomerResponseDTO>  
  3.   <ContractList>  
  4.     <Contract>  
  5.       <ContractNumber>530000000112</ContractNumber>  
  6.       <ContractIdPK>713343129248515901</ContractIdPK>  
  7.       <CurrencyType>1000001</CurrencyType>  
  8.       <CurrencyValue>USD</CurrencyValue>  
  9.       <FrequencyModeValue>B</FrequencyModeValue>  
  10.       <BillingValue>10 Days Inv</BillingValue>  
  11.       <ServiceProvId>1</ServiceProvId>  
  12.       <CompanyNumber>19</CompanyNumber>  
  13.       <IssueLocation>US</IssueLocation>  
  14.       <ContractLastUpdateDate>2015-05-10 16:14:45.158</ContractLastUpdateDate>  
  15.       <ContractLastUpdateUser>mdmadmin</ContractLastUpdateUser>  
  16.       <ContractLastUpdateTxId>626143129248478201</ContractLastUpdateTxId>  
  17.       <AgreementName>MaxWell</AgreementName>  
  18.       <ComponentID>1000944</ComponentID>  
  19.       <ContractExtension>  
  20.         <CreditLimit>222</CreditLimit>  
  21.         <ShippingChargeFlag>Y</ShippingChargeFlag>  
  22.         <TaxableFlag>Y</TaxableFlag>  
  23.         <CustomerClass>SA</CustomerClass>  
  24.         <CreatedBy>DSA</CreatedBy>  
  25.         <LinkNumDesc>Test</LinkNumDesc>  
  26.         <SalesPerson/>  
  27.         <ModifyBy>XMODIFY_BY</ModifyBy>  
  28.       </ContractExtension>  
  29.       <AdminNativeKey>  
  30.         <AdminNativeKey>  
  31.           <adminNativeKeyIdPK>712243</adminNativeKeyIdPK>  
  32.           <adminContractId>530000</adminContractId>  
  33.           <adminFieldNameType>CustomerAccountNumber</adminFieldNameType>  
  34.           <adminFieldNameValue>123456</adminFieldNameValue>  
  35.           <contractId>7133431</contractId>  
  36.           <nativeKeyLastUpdateDate>2015-05-10 16:14:45.266</nativeKeyLastUpdateDate>  
  37.           <nativeKeyLastUpdateUser>mdmadmin</nativeKeyLastUpdateUser>  
  38.           <nativeKeyLastUpdateTxId>6261431</nativeKeyLastUpdateTxId>  
  39.         </AdminNativeKey>  
  40.       </AdminNativeKey>  
  41.     </Contract>  
  42.   </ContractList>  
  43. </CustomerResponseDTO> 
Then, you can write test case as in the following. 
  1. [TestMethod]  
  2.     [DeploymentItem(CILXMLFile)]  
  3.     public void Should_Have_Customer_Number()  
  4.     {  
  5.         var actual = GetSampleData();  
  6.         //Assert  
  7.         Assert.AreEqual(530000, actual.CustomerNumber);  
  8.     }  
  9.    
  10.     [TestMethod]  
  11.     [DeploymentItem(CILXMLFile)]  
  12.     public void Should_Have_Company_Name()  
  13.     {  
  14.         var actual = GetSampleData();  
  15.         //Assert  
  16.         Assert.AreEqual("MaxWell", actual.CompanyName);  
  17.     } 
Then, you can get the GetSampleData() as in the following. 
  1. const string XMLFile = @"TestData/TestData.xml";    
  2. private static T DeserializeTestData<T>(string xmlPath) where T : class    
  3. {    
  4.     var serializer = new XmlSerializer(typeof(T));    
  5.   
  6.     using (var reader = new StreamReader(xmlPath))    
  7.     {    
  8.         return (T)serializer.Deserialize(reader);    
  9.     }    
  10. }    
  11.   
  12. private Adapers.Customer GetSampleData()    
  13. {    
  14.     //Arrange    
  15.         
  16.     var customerResponseDto = DeserializeTestData<CustomerResponseDTO>("TestData.xml");    
  17.   
  18.     Mock<ICustomerSearchServiceWrapper> mock = new Mock<ICustomerSearchServiceWrapper>();    
  19.   
  20.   
  21.   
  22.     mock.Setup(    
  23.               obj =>    
  24.                   obj.GetCustomer(It.IsAny<GetCustomerRequestDTO>(), It.IsAny<PagingInput>(),    
  25.                       It.IsAny<ClientApplicationInfo>()))    
  26.           .Returns(customerResponseDto);    
  27.   
  28.     var objectUnderTest = new TestSearchServiceAdapter(mock.Object,new Mock<IConfigurableSettings>().Object);    
  29.     //Act    
  30.   
  31.     var actual = objectUnderTest.GetAddressDetails("1234567890""US""BLG");    
  32.     return actual;    
  33. }  
With the above changes in place, when I will run the same, it will produce the following result.  

5th

Thanks for Joining me.
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now