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.
- <?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 as in the following.
- [TestMethod]
- [DeploymentItem(CILXMLFile)]
- public void Should_Have_Customer_Number()
- {
- var actual = GetSampleData();
-
- Assert.AreEqual(530000, actual.CustomerNumber);
- }
-
- [TestMethod]
- [DeploymentItem(CILXMLFile)]
- public void Should_Have_Company_Name()
- {
- var actual = GetSampleData();
-
- Assert.AreEqual("MaxWell", actual.CompanyName);
- }
Then, you can get the GetSampleData() as in the following.
- 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()
- {
-
-
- 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);
-
-
- var actual = objectUnderTest.GetAddressDetails("1234567890", "US", "BLG");
- return actual;
- }
With the above changes in place, when I will run the same, it will produce the following result.
Thanks for Joining me.