I am new to test classes in salesforce. I want to write test class for an apex class which has Web service callouts.
My Apex code is :
public without sharing class UserConnect {
public string myAPIKey{get; set;}
public User currentUser;
public Integer status{get; set;}
public UserConnect(){
currentUser = [SELECT Id,my_API_Key__c FROM User WHERE Id = :UserInfo.getUserId()];
myAPIKey = currentUser.my_API_Key__c;
status = 0;
}
public void saveMyAPIKey(){
System.debug('calling saveMyAPIKey.........');
System.debug(myAPIKey);
if(myAPIKey != ''){
status = testConnection();
if(status == 200){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Successful Connection'));
}
else {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Connection Failed : invalid API key'));
}
System.debug(currentUser);
currentUser.my_API_Key__c = myAPIKey;
Database.SaveResult SR = Database.update(currentUser);
}
}
//check website connection
private Integer testConnection(){
HttpRequest hReq = new HttpRequest();
System.debug(myAPIKey);
hReq.setEndpoint('https://www.webiste.com/data.json?auth_token='+myAPIKey);
hReq.setMethod('GET');
Http h = new Http();
System.debug(hReq);
HttpResponse hResp = h.send(hReq);
return hResp.getStatusCode();
}
}
The Test Class for the above apex class is :
@isTest
public with sharing class UserConnect_TestClass {
static testMethod void myTest() {
string apiKey = 'here_apiKey';
UserConnect class_test = new UserConnect();
// Checking whether my-API-key is equal to the saved key...
System.assertEquals(class_test.myAPIKey, apiKey);
class_test.status = 200;
// Checking whether response status is equal to the 200 or not...
class_test.saveMyAPIKey();
System.assertEquals(class_test.status,200);
}
}
This test class gives me code coverage 71%. Code Coverage view of code
public without sharing class UserConnect {
public string myAPIKey{get; set;}
public User currentUser;
public Integer status{get; set;}
public UserConnect(){
currentUser = [SELECT Id,my_API_Key__c FROM User WHERE Id = :UserInfo.getUserId()];
myAPIKey = currentUser.my_API_Key__c;
status = 0;
}
public void saveMyAPIKey(){
System.debug('calling saveMyAPIKey.........');
System.debug(myAPIKey);
if(myAPIKey != ''){
status = testConnection();
////////// Not covered code Start...
if(status == 200){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Successful Connection'));
}
else {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Connection Failed : invalid API key'));
}
currentUser.my_API_Key__c = myAPIKey;
Database.SaveResult SR = Database.update(currentUser);
/////// Not covered code end.......
}
}
//check website connection
private Integer testConnection(){
HttpRequest hReq = new HttpRequest();
System.debug(myAPIKey);
hReq.setEndpoint('https://www.webiste.com/data.json?auth_token='+myAPIKey);
hReq.setMethod('GET');
Http h = new Http();
System.debug(hReq);
HttpResponse hResp = h.send(hReq);
/// Not Covered Code start....
return hResp.getStatusCode();
/// Not Covered Code end....
}
}
And the failure message is : Methods defined as TestMethod do not support Web service callouts.
Note: The code coverage view contains the comments to show uncovered code.
Please correct my test class according to the error. Any help will be appreciated...