4

Overview:

The problem I'm trying to solve is - I have some canceled orders in my database that I need to call a government WebService to disable the fiscal numbers associated with these orders.

The specific problem is:

I'm creating a class to create the XML request that should be sent to the government. It has a method that received an order as argument and should return the XML that will be sent to the government.

The process of creating the XML has three steps:

  1. Create the basic XML with some fixed info and some info from the order
  2. Sign this XML
  3. Envelop this XML in a SOAP request

The method I want to test is this:

public class RequestBuilder
{
    public RequestBuilder(DisablerParameters disablerParameters, XmlSigner xmlSigner, XmlEnveloper xmlEnveloper)
    {
        this.disablerParameters = disablerParameters;
        this.xmlSigner = xmlSigner;
        this.xmlEnveloper = xmlEnveloper
    }

    public string BuildXml(Order order)
    {
        string xml = "<inutNFe>";

        int year = order.getYear();
        int orderId = order.getId();

        xml += this.disablerParameters.fillParameters(year, orderId);

        xml += "<inutNFe>";

        string signed_xml = this.xmlSigner.sign(xml);

        string enveloped_xml = this.xmlEnveloper.envelop(signed_xml);

        return enveloped_xml;
    }
}

The DisablerParameters class has the fixed values and a method that received the order dependent data and return a XML with the fixed and order dependent values.

My problem is:

The way I think it would be correct to test this method would be a supply an order and I check if the returned XML is valid. But in order for this to work I would have to provide valid implementations of the dependencies and this would be more like an integration test than a unit test. If I supply mocks and test if the mock methods were called I think I would be testing the implementation not the behavior.

2
  • " to disable the fiscal numbers associated with these orders." didn't mean anything to me. Please elaborate a bit more on what you mean by "disable the fiscal numbers". Apr 4, 2017 at 22:51
  • Each order have an associated "fiscal number". Whenever an order is paid, I have to call a governament WebService with the details of the sale. If, for some reason, the order is canceled, I have to call another service telling that the "fiscal number" associated with that order won't be used to send order details. They call it disable the "fiscal number". Apr 5, 2017 at 1:40

1 Answer 1

1

The purpose of unit testing is to validate implementation. Behavior (what the end user sees) is validated in higher levels of testing.

Your unit tests are good if them have good mutation coverage, i.e., if your target method is changed (causing a regression bug), they will fail, pointing the error.

Since you have control over the behavior of your mocks, your tests will take in consideration and validate the structure of the XML and the data that is inputed by the BuildXml method. Your mocks will be highly tighted to your tests, since their behavior will be something that your tests will take as correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.