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
Alireza SeifiAlireza Seifi 

How can I create a test for my code.

I'm new to Acc Seed and I was worndering how I can create a unit test for my code: and my code covered drop from 95 to 0 when I used developer console to resave, I'm not sure what is causing that.
 
global with sharing class DigitalChecksChargeController {

    public DigitalChecksChargeController(ApexPages.StandardController stdController)
    {

    }    
    
    @RemoteAction
    public static String createCheck(String email, Decimal amount, String firstName, String lastName, String semail, String description) {    
        return CheckbookAPI.createCheck(email, amount, firstName, lastName, semail, description);
    }    
}
I'm getting "methods defined as TestMethod do not support Web service callouts"

Any help would be greatfully aprreciated.

 
Mahesh DMahesh D
Hi Alireza,

Testmethod do not support callout webservice,

Option 1:

Please skip the call of service in webservice class and use the dummy data of response of callouts using Test.istestRunning().

Test class cannot be used to test Web service callout. The Test.isRunningTest() method is a useful method to bypass web service callouts when running automated test cases that would otherwise fail.

For example, in any wsdl2apex generated classes find the WebServiceCallout.invoke() method calls and then wrap them in an test for Test.isRunningTest(). If the test is false the code can still call invoke as generated. Otherwise the code can simulate the web service response, allowing the test cases to complete.
 
 http://www.eltoro.it/ArticleViewer?id=a07A000000NPRjNIAX
 
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_callouts_wsdl2apex_testing.htm
 
http://boards.developerforce.com/t5/Apex-Code-Development/how-to-write-test-class-for-webservice-method/m-p/625245#M115307
 
http://blog.wdcigroup.net/2012/08/salesforce-web-service-callout-test-class/
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Option 2:

Test Web Service Callouts
Generated code is saved as an Apex class containing the methods you can invoke for calling the web service. To deploy or package this Apex class and other accompanying code, 75% of the code must have test coverage, including the methods in the generated class. By default, test methods don’t support web service callouts, and tests that perform web service callouts fail. To prevent tests from failing and to increase code coverage, Apex provides the built-in WebServiceMock interface and the Test.setMock method. Use WebServiceMock and Test.setMock to receive fake responses in a test method.
Specify a Mock Response for Testing Web Service Callouts
When you create an Apex class from a WSDL, the methods in the auto-generated class call WebServiceCallout.invoke, which performs the callout to the external service. When testing these methods, you can instruct the Apex runtime to generate a fake response whenever WebServiceCallout.invoke is called. To do so, implement the WebServiceMock interface and specify a fake response for the Apex runtime to send. Here are the steps in more detail.

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

---------------------------------------------------------------------------------------------------------------------

Also follow the below Links for on similar post:

https://developer.salesforce.com/forums/?id=906F000000094EyIAI

https://developer.salesforce.com/forums/?id=906F0000000BVqxIAG

https://developer.salesforce.com/forums/?id=906F0000000AhBYIA0

https://developer.salesforce.com/forums/?id=906F000000093EuIAI

https://developer.salesforce.com/forums/?id=906F0000000927CIAQ

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

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

http://www.greytrix.com/blogs/salesforce/2015/02/02/testing-web-service-callouts/

http://salesforce-evershine-knowledge.blogspot.com/2012/10/testing-web-service-callouts.html


Please do let me know if it helps you.

Regards,
Mahesh
Alireza SeifiAlireza Seifi
Thank you very much for your help,

I know I'm asking too much but could you please take a look at my code and see why I'm getting this error? I followed the instructor of the link you provided.

User-added image

Thanks,
Al
Alireza SeifiAlireza Seifi
This is the main class I have.
 
public class CheckbookAPI {
    
    // DigitalCheck__APIConfig__c is a custom setting object that stores the API Keys
    public static DigitalCheck__APIConfig__c Config = DigitalCheck__APIConfig__c.getOrgDefaults();

    public CheckbookAPI()
    {
    }

    public static String getChecks() {
        HttpRequest req = CheckbookAPI.getHttpRequest('GET', '/transactions');
        return CheckbookAPI.getHttpResponse(req);
    }    
    public static String createCheck(String email, Decimal amount, String firstName, String lastName, String semail, String description) {
        HttpRequest req = CheckbookAPI.getHttpRequest('POST', '/send_check');

        Map<String,Object> mapParams = new Map<String,Object>();
        mapParams.put('email', email);
        mapParams.put('amount', amount);
        mapParams.put('first_name', firstName);
        mapParams.put('last_name', lastName);
        mapParams.put('sender_email', semail);
        mapParams.put('description', description);
        req.setBody(JSON.serialize(mapParams));
        return CheckbookAPI.getHttpResponse(req);
    }
    

    private static String getHttpResponse(HttpRequest req) {
        Http http = new Http();
        HTTPResponse response = http.send(req);
        return response.getBody();
    }
    
    private static HttpRequest getHttpRequest(String Method, String Path) {
        // Initialize the request
        HttpRequest req = new HttpRequest();
        
        // Build the selected elements
        String SelectedElements = '';
        
        
        // Set the method
        req.setMethod(Method);
        SelectedElements += Method;
        
        // Set the Content-Type header
        if (Method == 'POST') {
            SelectedElements += 'application/json';
            req.setHeader('Content-Type', 'application/json');
        }
        
        // Set the endpoint
        String CompletePath = '/' + CheckbookAPI.Config.DigitalCheck__VersionAPI__c + Path;
        SelectedElements += CompletePath;
        req.setEndpoint(CheckbookAPI.Config.DigitalCheck__ServerURL__c + CompletePath);

        return req;
    }
    


}