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", "test@example.com");
    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", "test@example.com");
    string LCResult = LeadConvert.convertLead("123", "123", "false", "test@example.com");
    System.assertEquals("failed", LCResult);
}
 }
  • 1
    By the way 'true' == 'True', so there's no need to check both. – sfdcfox Feb 15 '17 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. – Jeremy Nottingham Feb 16 '17 at 1:53
  • @Jeremy Nottingham Can you please elaborate on how I can create a boiler plate template of my class using HTTPMock? – chillax786 Feb 16 '17 at 3:28
  • 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 Feb 16 '17 at 4:08
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.

| 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.