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

1 Answer 1

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;
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .