0

I'm trying to deploy my Apex class to my production environment. I was having difficulty writing and organizing my Tests. Here is the Apex Rest Class I created

 @RestResource(urlMapping='/convertlead/*')
 global class LeadConvert extends Auth.ConnectedAppPlugin{
 @HttpPost
global static string convertLead(string leadId, string accountId, string newUser, string email)
{
    Lead lead = [select Id from Lead where Id = :leadId];
    Database.LeadConvert lc = new Database.LeadConvert();
    LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
    lc.setLeadId(lead.Id);
    lc.setAccountId(accountId);
    if(newUser == 'true' || newUser =='True'){
        lc.setDoNotCreateOpportunity(false);
        lc.setOpportunityName(email);
    }
    else{
        lc.setDoNotCreateOpportunity(true);
    }
    lc.setConvertedStatus(convertStatus.MasterLabel);
    Database.LeadConvertResult lcr = Database.convertLead(lc);
    if (lcr.isSuccess())
    {
        return 'success';
    }
    else
    {
        return 'failed';
    }
}
  @isTest
  static void leadConvertClassTest(){
    LeadConvert lct = new LeadConvert();
    string LCResult = lct.convertLead("123", "123", "false", "[email protected]");
    System.assertEquals("failed", LCResult);

    }

}

I want to write a mock unit test that will just let me deploy to my production environment. It doesn't need to validate anything at this point. How can I do that?

I've created another apex Test Class with the following method, still getting 0% test coverage?

  @isTest
   private class LeadConverTest {

static testMethod void testconvertLead() {
    //LeadConvert lct = new LeadConvert();
    //string LCResult = convertLead("123", "123", "false", "[email protected]");
    string LCResult = LeadConvert.convertLead("123", "123", "false", "[email protected]");
    System.assertEquals("failed", LCResult);
}
 }
5
  • 1
    By the way 'true' == 'True', so there's no need to check both.
    – sfdcfox
    Commented Feb 15, 2017 at 23:53
  • It looks like you have a test method in your functional Apex class. If so, you are on a very old API version, and will need to update that and create a separate test class before worrying about HTTPMock. Commented Feb 16, 2017 at 1:53
  • @Jeremy Nottingham Can you please elaborate on how I can create a boiler plate template of my class using HTTPMock?
    – chillax786
    Commented Feb 16, 2017 at 3:28
  • salesforce.stackexchange.com/questions/159444/…
    – Adrian Larson
    Commented Feb 16, 2017 at 4:06
  • 1
    you don't need an httpmock to test this, execute the class directly like any other class. and "just to get it into production" ends up meaning "never getting back around to doing it right"
    – Eric
    Commented Feb 16, 2017 at 4:08

1 Answer 1

0

There's a lot you need to do here. Basically speaking, writing tests just to get the code coverage up is a Very Bad Idea.

That aside, your test is failing on the very first line, because you have failed to create a Lead to test with, and the Lead Id you are passing in is in an invalid format.

Before going any further, I would encourage you to read An Introduction to Apex Test Methods, Testing Best Practices and Understanding Testing in Apex to get a better idea of why these tests are valuable, because writing tests that don't test anything just to get code into production is asking for a world of hurt.

You must log in to answer this question.

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