我正在尝试将Apex类从沙箱部署到生产。但是我得到了一个代码覆盖错误。
我的代码覆盖率是69%,我不知道如何将其提高到75%。但我的代码在沙箱上运行良好。
请帮我。
我的代码是:
@RestResource(urlMapping='/TestAPI')
global with sharing class TestingAPI {
@HttpPost
global static string doPost(string company, string first_name)
{
Lead objLead = new Lead();
objLead.Company = company;
objLead.FirstName = first_name;
insert objLead;
return 'Submitted Successfully';
}
}
根据这个答案,Lead对象上的标准必填字段是Company、LastName和Status,所以我不得不稍微更改您的类。我还将返回的字符串移动到一个常量中,以使其在测试类中也可用。
@RestResource(urlMapping='/TestAPI')
global with sharing class TestingAPI {
public static final String SUCCESS_MSG = 'Submitted Successfully';
@HttpPost
global static string doPost(String company, String lastName) {
Lead objLead = new Lead();
objLead.Company = company;
objLead.LastName = lastName;
insert objLead;
return SUCCESS_MSG;
}
}
这方面的测试类可能看起来像以下(100%覆盖率(:
@IsTest
public class TestingAPITest {
@IsTest
static void test_doPost() {
String company = 'Test Company';
String lastName = 'TestName';
String returnedStr = TestingAPI.doPost(company, lastName);
System.assertEquals(TestingAPI.SUCCESS_MSG, returnedStr);
Lead testLead = [
SELECT Company, LastName
FROM Lead
LIMIT 1
];
System.assertEquals(company, testLead.Company);
System.assertEquals(lastName, testLead.LastName);
}
}