function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Allen2Allen2 

Help me to write test class for web service class.. first time I am writing and created mock also.. may be wrong mock...help me to cover the code below..

APEX CLASS
public class Async_SFDCtoNextNotifyC {
    public class NotifyCRespFuture extends System.WebServiceCalloutFuture {
        public SFDCtoNextNotifyC.NotifyCResType getValue() {
            SFDCtoNextNotifyC.NotifyCResType response = (SFDCtoNextNotifyC.NotifyCResType)System.WebServiceCallout.endInvoke(this);
            return response;
        }
    }
    public class AsyncSOAPOverHTTPs_0 {
        public String endpoint_x = 'https://nextb-dev.abb.com:443/CommonInterface/Customer-2.0';
        public Map<String,String> inputHttpHeaders_x;
        public String clientCertName_x;
        public Integer timeout_x;
        private String[] ns_map_type_info = new String[]{'https://nextb.abb.com/CommonInterface/Customer-2.0', 'SFDCtoNextNotifyC'};
        public Async_SFDCtoNextNotifyC.NotifyCRespFuture beginNotifyC(System.Continuation continuation,SFDCtoNextNotifyC.NotifyCReqType[] notifyC) {
            SFDCtoNextNotifyC.NotifyCType request_x = new SFDCtoNextNotifyC.NotifyCType();
            request_x.notifyCustomer = notifyCustomer;
            return (Async_SFDCtoNextNotifyC.NotifyCRespFuture) System.WebServiceCallout.beginInvoke(
              this,
              request_x,
              Async_SFDCtoNextNotifyC.NotifyCRespFuture.class,
              continuation,
              new String[]{endpoint_x,
              'notifyCAction2_0',
              'https://nextb.abb.com/CommonInterface/Customer-2.0',
              'notifyCustomersRequest',
              'https://nextb.abb.com/CommonInterface/Customer-2.0',
              'notifyCustomersResponse',
              'SFDCtoNextNotifyC.NotifyCResType'}
            );
        }
    }
}

TEST CLASS
@isTest
public class Async_SFDCtoNextNotifyCTest {
    
    static testmethod void NotifyCus(){
        Test.setMock(WebServiceMock.class, new NotifyCMock());
        new Async_SFDCtoNextNotifyC.AsyncSOAPOverHTTPs_0();
        new Async_SFDCtoNextNotifyC.NotifyCRespFuture();
    }
}

MOCK CLASS
@isTest
global class NotifyCMock implements WebServiceMock {
    
    global void doInvoke(
           Object stub,
           Object request,
           Map<String, Object> response,
           String endpoint,
           String soapAction,
           String requestName,
           String responseNS,
           String responseName,
           String responseType) {
               
        Async_SFDCtoNextNotifyC.AsyncSOAPOverHTTPs_0 response_x = new Async_SFDCtoNextNotifyC.AsyncSOAPOverHTTPs_0();
        SFDCtoNextNotifyC.NotifyCResType response1 = new SFDCtoNextNotifyC.NotifyCResType();
        
        response.put('response_x', response_x);
           }
}
karthikeyan perumalkarthikeyan perumal
Hello,

Try below code, 

Http Mock test class: 
 
@isTest
global class MockHttpResponseGenerator implements HttpCalloutMock {
     
    global HTTPResponse respond(HTTPRequest req) {
         
        System.assertEquals('http://example.com/example/test', req.getEndpoint());
        System.assertEquals('GET', req.getMethod());
        
        // Create a fake response
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setBody('{"example":"test"}');
        res.setStatusCode(200);
        return res;
    }
}

Test class 
 
@isTest
public class Async_SFDCtoNextNotifyCTest {
    
    static testmethod void NotifyCus(){

        Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());

          Async_SFDCtoNextNotifyC.AsyncSOAPOverHTTPs_0();
          Async_SFDCtoNextNotifyC.NotifyCRespFuture();
    }
}

Hopw this will help you.. 

Thanks
karthik
 
Allen2Allen2
Hi Karthikeyan,

We are not give httpcall in the main apex class so it should be webservice mock not the httpcallout mock.... So, we can't write this mock class for this kind of web service class...
even for you i tried it but not woking....
kindly help to write webservicemock..

thank you