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
Internal PartnerInternal Partner 

How to write a test class for a webservice callout?

Hi all,

I am struggling trying to fix a test class for an APEX class (WSDL) which has the following form:
 
global class Example {
   webService static String getUserName(String name) {
       return UserInfo.getUserEmail();
   }
}

My test class is this:
 
@isTest
private class Example_Test {
    @isTest static void Example() {              
        // This causes a fake response to be generated
        Test.setMock(WebServiceMock.class, new getUserName());  
    }
}

I am getting this error:
 
System.TypeException: Mock object doesn't implement the supplied interface
Class.System.Test.setMock: line 57, column 1
Class.Example_Test.Example: line 5, column 1
Any advice would be appreciated :)
 
Best Answer chosen by Internal Partner
Raj VakatiRaj Vakati
My mistake pls 


try the below code its 100%

 
@isTest
public class ExampleTest {
    static testMethod void testMyWebSvc()
    {
        Profile pf= [Select Id from profile where Name='System Administrator']; 
        
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = 'testasdafsghdfgasf@gam.com', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 
        
        
        insert uu;
        System.runAs(uu){
            
            Test.startTest();
            Example.getUserName('testasdafsghdfgasf@gam.com') ;
            Test.stopTest();
        }
    }
}

 

All Answers

Raj VakatiRaj Vakati
Refer this linnk 



https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_wsdl2apex_testing.htm


Change your code as below
 
@isTest
private class Example_Test {
    @isTest static void Example() {              
        // This causes a fake response to be generated
        Test.setMock(WebServiceMock.class, new WebServiceMockImpl ());  
    }
}
Mock webservice class
 
@isTest
global class WebServiceMockImpl 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) {
       docSample.EchoStringResponse_element respElement = 
           new docSample.EchoStringResponse_element();
       respElement.EchoStringResult = 'Mock response';
       response.put('response_x', respElement); 
   }
}


 
Internal PartnerInternal Partner
That would be my test class? do I need to change something in your suggested code?. Sorry, you are not helping :(
Raj VakatiRaj Vakati
In you test class you need to set the mock responce for below line


        Test.setMock(WebServiceMock.class, new getUserName()); 

getUserName --> need to be implemented  
WebServiceMock  


​​​​​​​
Raj VakatiRaj Vakati
My mistake pls 


try the below code its 100%

 
@isTest
public class ExampleTest {
    static testMethod void testMyWebSvc()
    {
        Profile pf= [Select Id from profile where Name='System Administrator']; 
        
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = 'testasdafsghdfgasf@gam.com', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 
        
        
        insert uu;
        System.runAs(uu){
            
            Test.startTest();
            Example.getUserName('testasdafsghdfgasf@gam.com') ;
            Test.stopTest();
        }
    }
}

 
This was selected as the best answer
Internal PartnerInternal Partner
Thanks Raj!!!. The test was successful :)