0

I have one class and to cover test class i have written one HttpCalloutMock class

    global HttpResponse res(HttpRequest req) {
    HttpResponse res = new HttpResponse();
    res.setHeader('Content-Type', 'text/xml;charset=UTF-8');

    String endPoint = req.getEndpoint();

    String reqBody = req.getBody();


    String xmlBody = '<?xml version="1.0" encoding="UTF-8"?>' +
                     '</soapenv:Envelope>';

    res.setBody(xmlBody);  
    res.setStatusCode(200); 
    return res;     
}

This class is covering if condition for status code 200 but not else How can achieve this

0

One way is to add a parameter to the mock class's constructor and then use various instances in your tests:

public class YourMock implements HttpCalloutMock {

    private Integer statusCode;

    public YourMock(Integer statusCode) {
        this.statusCode = statusCode;
    }

    public HttpResponse respond(HttpRequest req) {
        HttpResponse res = new HttpResponse();
        ...
        res.setStatusCode(statusCode);
        return res;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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