-1

I am pretty new to Salesforce development and I am having a hard time writing a test class for some code that I am writing for a Jira to Salesforce connector. Below is my class and trigger, but I am not sure how to write a test class for a web service callout. Any help would be appreciated.

Class:

global class JIRAWebserviceCalloutSyncStatus {
@future (callout=true)
WebService static void syncstatus(String status, String jiraKey) {
    //Modify these variables:
    String username = 'salesforceconnector';
    String password = 'xxxxxxx';
    String jiraURL = 'https://xxxxxxxx.com';
    String transitionId;

    //Map Salesforce Status to JIRA transition Id:
     if (status == 'Waiting on Risk') {                  // Salesforce.com Status
        transitionId = '181';                 // JIRA transition ID
    } else if (status == 'Waiting on Customer') {
        transitionId = '21';
    } else if (status == 'Active') {
        transitionId = '161'; 
    } 


    //Construct HTTP request and response
    HttpRequest req = new HttpRequest();
    HttpResponse res = new HttpResponse();
    Http http = new Http();

    //Construct Authorization and Content header
    Blob headerValue = Blob.valueOf(username+':'+password);
    String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
    req.setHeader('Authorization', authorizationHeader);
    req.setHeader('Content-Type','application/json');

    //Construct Endpoint
    String endpoint = jiraURL+'/rest/api/2/issue/'+jiraKey+'/transitions';

    //Set Method and Endpoint and Body
    req.setMethod('POST');
    req.setEndpoint(endpoint);
    req.setBody('{ \"transition\": {\"id\": \"'+transitionId+'\"}}');

    try {
        //Send endpoint to JIRA
        res = http.send(req);
    } catch(System.CalloutException e) {
        System.debug(res.toString());
    }
}

Trigger:

trigger SyncStatus on Case (after update) {
//Identify profile name to be blocked from executing this trigger
String JIRAAgentProfileName = 'JIRA Agent';
List<Profile> p = [SELECT Id FROM Profile WHERE Name=:JIRAAgentProfileName];

//Check if specified Profile Name exist or not
if(!p.isEmpty())
{
    //Check if current user's profile is catergorized in the blocked profile
    if(UserInfo.getProfileId()!= String.valueOf(p[0].id))
    {
        for (Case c : Trigger.new) {
            //Define parameters to be used in calling Apex Class
            String status = c.Status;
            String jiraKey = c.JIRA_Key__c;

            JIRAWebserviceCalloutSyncStatus.syncstatus(status, jiraKey);
        }
    }
}

Test Class:

@isTest
public class TestJIRAWebserviceCalloutSyncStatus {

static testMethod void TestJIRAWebserviceCalloutSyncStatus(){
    Test.startTest();
    JIRAWebserviceCalloutSyncStatus.syncstatus();
    Test.stopTest();
}
4
  • 1
    Welcome to SFSE Frank. What exactly is your question? As it's currently written, this will likely be closed, as posts are generally expected to be specific, on one issue. Jun 16, 2016 at 16:03
  • Please do visit the help center and read How to Ask.
    – Adrian Larson
    Jun 16, 2016 at 16:05
  • 1
    in the test class, you need to pass two string parameters to the method syncstatus(). You should create data for status and jirakey according to your apex class.
    – Anurag
    Jun 16, 2016 at 16:08
  • hi @AnuragA could you provide some more details please?
    – Frank
    Jun 16, 2016 at 18:37

1 Answer 1

1

I suspect IRAWebserviceCalloutSyncStatus.syncStatus() doesn't need to be a webservice. If you are only calling it within your application, you can just call it. It should return the repsonse for later checks.

I'm suprised that this is working. You can't do callouts from a trigger synchronously. I've always needed to create an asynchronous routine (I tend to go for the @future (callout=true) notation) to be able to do a callout from a trigger.

The test itself can not actually test the callout itself. While that does make sense (the test would pass or fail depending on the availability of the site you are connecting to), it does limit the testability of a callout routine. Test it by simulating an answer, as in

if (Test.isTest)
   calloutResponse = '...';
else
   calloutResponse = IRAWebserviceCalloutSyncStatus.syncstatus();

where '...' is a rseponse you would get from the Jira service.

Finally, make sure the password above is not a real password.

Not the answer you're looking for? Browse other questions tagged .