function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Andrew B 1Andrew B 1 

REST WebService Code Coverage only 50%

I am working on a REST WebService that takes in 2 objects. I was able to create the 2 classes (one for Account, one for Contact). The test for Account gets 100% but for Contact only gets 50% and I'm not sure what I'm missing.

ContactsManager.apxc:
@RestResource(urlMapping='/Contacts/*')
global with sharing class ContactsManager {

       @HttpPost
    global static ID createContact(String contactOwner, String department, String contactEmail) {
        Contact thisContact = new Contact(OwnerId=contactOwner, Department=department, Email=contactEmail);
        insert thisContact;
        return thisContact.Id;
    } 
}
(My Test class)  ContactsManagerTest.apxc:
@IsTest
public class ContactsManagerTest {
    
    @isTest static void testCreateContact() {
        // Call the method to test
        ID thisContactId = ContactsManager.createContact(
            '1', 'CSIS', 'olmsted@cofc.edu');
        // Verify results
        System.assert(thisContactId != null);
        Contact thisContact = [SELECT Id,Department FROM Contact WHERE Id=:thisContactId];
        System.assert(thisContact != null);
        System.assertEquals(thisContact.Department, 'CSIS');
    }
}
When I click on the test area for the 2 lines that are failing, it points to:
insert thisContact;
        return thisContact.Id;

Not sure what's wrong with these?
 
Best Answer chosen by Andrew B 1
GulshanRajGulshanRaj
Hi,

If this is the exact same code you are using then I would suggest few thing which you are missing:
  • You are passing '1' as ownerID in test class, however you have to pass user ID as owner.
  • Contact need LastName as mandatory field  which is missing before insert.
Here is modified code:
ContactsManager.cls:
@RestResource(urlMapping='/Contacts/*')
global with sharing class ContactsManager {

       @HttpPost
    global static ID createContact(String contactOwner, String department, String contactEmail,String contactLastName) {
        Contact thisContact = new Contact(OwnerId=contactOwner, Department=department, Email=contactEmail, LastName=contactLastName);
        insert thisContact;
        return thisContact.Id;
    } 
}

and ContactsManagerTest.cls
@IsTest
public class ContactsManagerTest {
    
    @isTest static void testCreateContact() {
        // Call the method to test
        ID thisContactId = ContactsManager.createContact(
           UserInfo.getUserId()  , 'CSIS', 'olmsted@cofc.edu','Test');
        // Verify results
        System.assert(thisContactId != null);
        Contact thisContact = [SELECT Id,Department FROM Contact WHERE Id=:thisContactId];
        System.assert(thisContact != null);
        System.assertEquals(thisContact.Department, 'CSIS');
    }
}

Regards
Gulshan Raj​

All Answers

GulshanRajGulshanRaj
Hi,

If this is the exact same code you are using then I would suggest few thing which you are missing:
  • You are passing '1' as ownerID in test class, however you have to pass user ID as owner.
  • Contact need LastName as mandatory field  which is missing before insert.
Here is modified code:
ContactsManager.cls:
@RestResource(urlMapping='/Contacts/*')
global with sharing class ContactsManager {

       @HttpPost
    global static ID createContact(String contactOwner, String department, String contactEmail,String contactLastName) {
        Contact thisContact = new Contact(OwnerId=contactOwner, Department=department, Email=contactEmail, LastName=contactLastName);
        insert thisContact;
        return thisContact.Id;
    } 
}

and ContactsManagerTest.cls
@IsTest
public class ContactsManagerTest {
    
    @isTest static void testCreateContact() {
        // Call the method to test
        ID thisContactId = ContactsManager.createContact(
           UserInfo.getUserId()  , 'CSIS', 'olmsted@cofc.edu','Test');
        // Verify results
        System.assert(thisContactId != null);
        Contact thisContact = [SELECT Id,Department FROM Contact WHERE Id=:thisContactId];
        System.assert(thisContact != null);
        System.assertEquals(thisContact.Department, 'CSIS');
    }
}

Regards
Gulshan Raj​
This was selected as the best answer
Andrew B 1Andrew B 1
Thank you!!!!!!!!!!!!!!!!!!!