Wednesday, January 25, 2012

How to write unit tests for JSON, XML and SOAP endpoints of WCF Service

Background

In a recent project I worked on WCF service with the following requirements:

    Support requests and responses in SOAP, JSON and XML.
    Support GET and POST requests.
    Be able to Unit Test SOAP, JSON and XML requests

CLICK HERE TO DOWNLOAD SOURCE CODE FOR WCF SERVICE and UNIT TESTS
The WCF Service
For a sample application I made a Math service that allows for Adding two values together.
public class MathService
{
    public int Add(int value1, int value2)
    {
        int sum = value1 + value2;
        return sum;
    }
}

To access this service through WCF, I added the following IService contract:
[ServiceContract(Namespace = "apidoc.sampleapi.com", Name = "SampleApi")]
public interface IService
{
    [WebGet( UriTemplate = "Add?value1={value1}&value2={value2}&apiKey={apiKey}", BodyStyle = WebMessageBodyStyle.Bare)]
            AddRs AddWithHttpGet(int value1, int value2, string apiKey);
 
    [WebInvoke(Method = "POST", UriTemplate = "Add", BodyStyle = WebMessageBodyStyle.Bare)]
    AddRs Add(AddRq rq);
}


Read more: Entech solutions
QR: wcf-web-service-for-soap-json-and-xml-with-unit-tests

Posted via email from Jasper-Net