-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
2Questions
-
4Replies
Callout Unit Test - Static methods cannot be invoked through an object instance: getChecks()
I'm new to Salesforce and I was wondering why do I get the following error for my code. if you can please write me back code would be greatfully aprreciated.
API class
API CALL class
TEST class
So I'm getting
API class
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; } }
API CALL class
global with sharing class DigitalChecksChargeController { public DigitalChecksChargeController(ApexPages.StandardController stdController) { } @RemoteAction public static String processCharge() { return '200'; // Simulate the status code of the <charge> request } @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); } }
TEST class
@isTest public class CheckbookAPI_UsingStaticResources { public testmethod static void testWS() { String testBody = 'This is a test :-)'; StaticResourceCalloutMock mock = new StaticResourceCalloutMock(); mock.setStaticResource('Test_WS'); mock.setStatusCode(200); Test.setMock(HttpCalloutMock.class, mock); CheckbookAPI callout = new CheckbookAPI (); HttpResponse resp = callout.getChecks(); // <== GETTING ERROR : Static methods cannot be invoked through an object instance: getChecks() //System.assertEquals(200, resp.getStatusCode()); //System.assertEquals(resp.getBody(), testBody); } }
So I'm getting
Static methods cannot be invoked through an object instance: getChecks()
-
- Alireza Seifi
- March 17, 2016
- Like
- 0
- Continue reading or reply
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.
Any help would be greatfully aprreciated.
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.
-
- Alireza Seifi
- March 17, 2016
- Like
- 0
- Continue reading or reply
Callout Unit Test - Static methods cannot be invoked through an object instance: getChecks()
I'm new to Salesforce and I was wondering why do I get the following error for my code. if you can please write me back code would be greatfully aprreciated.
API class
API CALL class
TEST class
So I'm getting
API class
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; } }
API CALL class
global with sharing class DigitalChecksChargeController { public DigitalChecksChargeController(ApexPages.StandardController stdController) { } @RemoteAction public static String processCharge() { return '200'; // Simulate the status code of the <charge> request } @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); } }
TEST class
@isTest public class CheckbookAPI_UsingStaticResources { public testmethod static void testWS() { String testBody = 'This is a test :-)'; StaticResourceCalloutMock mock = new StaticResourceCalloutMock(); mock.setStaticResource('Test_WS'); mock.setStatusCode(200); Test.setMock(HttpCalloutMock.class, mock); CheckbookAPI callout = new CheckbookAPI (); HttpResponse resp = callout.getChecks(); // <== GETTING ERROR : Static methods cannot be invoked through an object instance: getChecks() //System.assertEquals(200, resp.getStatusCode()); //System.assertEquals(resp.getBody(), testBody); } }
So I'm getting
Static methods cannot be invoked through an object instance: getChecks()
- Alireza Seifi
- March 17, 2016
- Like
- 0
- Continue reading or reply
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.
Any help would be greatfully aprreciated.
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.
- Alireza Seifi
- March 17, 2016
- Like
- 0
- Continue reading or reply