You need to sign in to do that
Don't have an account?
Andrew 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:
Not sure what's wrong with these?
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?
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:
and ContactsManagerTest.cls
Regards
Gulshan Raj
All Answers
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:
and ContactsManagerTest.cls
Regards
Gulshan Raj