2

I'm struggling to understand the Salesforce documentation for testing a web service, I can't seem to achieve anything more than 0%.

The apex class I wish to test is powering a javascript button pressed by a user. Clicking this button calls an apex class to do some validation on the user that pressed the button, if they pass the validation a checkbox is checked named make_available_for_opps_and_proposals__c, and a message is returned to the user informing them the action has been completed, or in some cases that the action hasn't been completed.. The class is called availableForOpportunities.

I've read the documentation and I'm not clear on what I need to be able to move the class up into production.

So far I have attempted to write one test class as seen below, however I've read something that may suggest I need to write another class that mocks a webservice call. If somebody can guide me, and let me know how many test classes/mock web service calls I need to write that would be fantastic.

Test Class

@istest global class availableForOpportunities_TEST
{
  static testMethod void availableForOpportunities_Test ()
   {
    Product2 prd1 = new Product2 (Name = 'Test Product Entry 1', Description = 'Test Product Entry 1',
                                  productCode = 'ABC', isActive = false,
                                  RecordTypeId = '0128E0000004m8b',
                                  R_D_Service_Owner__c = '005w0000004x3RiAAI',
                                  Convert_to_Service_Catalogue_Initiated__c = FALSE,
                                  Convert_to_Service_Catalogue_Emailed__c = FALSE,
                                  make_available_for_opps_and_proposals__c = FALSE);
    insert prd1;
    Test.startTest();
    Test.setMock(WebServiceMock.class, new WebServiceMockImpl());

    String output = availableForOpportunities.callEchoString('This service or project is now available for opportunities');
    System.assertEquals('This service or project is now available for opportunities', output);


    Test.stopTest();
  }
}

Web Service Class

global class availableForOpportunities {
   WebService static String convertToOpportunity(Id product2){
    string result; // Return a string telling us if this worked or not
    Id currentProfile = UserInfo.getProfileId(); //Get the current user profile Id
    Id CurrentUser = Userinfo.getUserId();//Get the current user Id

    Set<Id> allowedProfiles = new Set<Id>();//Create a new set to profile Ids
    for(Profile p:[SELECT Id, Name FROM Profile WHERE Name = 'System Administrator']){//Get the System Administrator profile
        allowedProfiles.add(p.Id);//Add them to the set
    }

    Set<Id> allowedUsers = new Set<Id>();//Create a new set to store User Ids
    for(User u:[SELECT Id FROM User WHERE Id =  '005w0000004j93kAAA' OR Id = '005w0000004Noi6AAC' OR Id = '0058E000002CcqcQAC' OR Id = '0058E000002CcqwQAC' ]) {//Select Joy Palmer, Andy McGregor, Jeremey Sharp, and Mark O'Leary Id's} ])
        allowedUsers.add(u.Id);//Add them to the set
    }
    Product2 prod =[SELECT make_available_for_opps_and_Proposals__c, R_D_Service_Owner__r.id , Project_Owner__r.Id FROM Product2 WHERE Id = :product2]; //Get the product ]

    if( //If...
        ((currentUser == prod.R_D_Service_Owner__r.id) || // They own it
         (currentUser == prod.Project_Owner__r.id) || // They own it  
         (allowedProfiles.contains(currentProfile)) || //They're a system admin
         (allowedUsers.contains(currentUser))) // They're a specific user
    ){
        //Do stuff!
        prod.make_available_for_opps_and_proposals__c = true;

        UPDATE prod;

        result = 'This service or project is now available for opportunities';

    }

    else{//Otherwise...
        //Do nothing
        result = 'You must be the service/project owner to make this available for opportunities';

    }
    //Return a message
    return result;    
  }
}
  • 2
    You don't need a mock. A mock is used to simulate a callout from Salesforce to an external service. Here, your web service is inside Salesforce. You just have to instanciate your availableForOpportunities class and call your convertToOpportunity method from the test. – Martin Lezer Jan 18 '17 at 9:20
  • 2
    Avoid using hard coded ids in class – Reshma Jan 18 '17 at 9:28
2

You dont need a mock. You can directly call your webservice static method.

When you consume a webservice then you have to use a mock. here you are not doing anything like that, thus you don't need to use a mock.

@istest global class availableForOpportunities_TEST
{
  static testMethod void availableForOpportunities_Test ()
   {
    Product2 prd1 = new Product2 (Name = 'Test Product Entry 1', Description = 'Test Product Entry 1',
                                  productCode = 'ABC', isActive = false,
                                  RecordTypeId = '0128E0000004m8b',
                                  R_D_Service_Owner__c = '005w0000004x3RiAAI',
                                  Convert_to_Service_Catalogue_Initiated__c = FALSE,
                                  Convert_to_Service_Catalogue_Emailed__c = FALSE,
                                  make_available_for_opps_and_proposals__c = FALSE);
    insert prd1;
    Test.startTest();
    String output=availableForOpportunities .convertToOpportunity(prd1.id);
    Test.stopTest();
    System.assertEquals('This service or project is now available for opportunities', output);

  }
}
| improve this answer | |
1

Try this. Please do not use hard coded ids anywhere.

@istest global class availableForOpportunities_TEST
{
  static testMethod void availableForOpportunities_Test ()
   {
    Product2 prd1 = new Product2 (Name = 'Test Product Entry 1', Description = 'Test Product Entry 1',
                                  productCode = 'ABC', isActive = false,
                                  RecordTypeId = [SELECT Id, Name FROM recordType WHERE sObjectType='Product2' AND name = 'Test'][0].Id,//Replace Test with your recordtype name
                                  R_D_Service_Owner__c = ''; //Create a user and put Id of user you need
                                  Convert_to_Service_Catalogue_Initiated__c = FALSE,
                                  Convert_to_Service_Catalogue_Emailed__c = FALSE,
                                  make_available_for_opps_and_proposals__c = FALSE);
    INSERT prd1;
    Test.startTest();
    String output = availableForOpportunities.convertToOpportunity(prd1.Id);
    System.assertEquals('This service or project is now available for opportunities', output);
    Test.stopTest();
  }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.