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
Mike Tol 1Mike Tol 1 

Tests for add and delete contact

Hi!
Please help me to write negative and positive tests for two methods:

 @AuraEnabled
    public static void deleteContact(Id recordId) {   
        List<Contact> contact = 
        [SELECT Id FROM Contact WHERE Id = :recordId];
        delete contact;   
   }
    
    @AuraEnabled
    public static void addContact() {
        Contact contact = new Contact();
            try {
                insert contact;
            } catch (exception ex) {
                throw new auraHandledException(ex.getMessage());
            }
    }   

I tried, but it turns out something terrible...
@isTest
    public static void deleteContact_success_deletedContactById() { 
        Contact newContact = new contact(LastName = 'Bab');
        insert newContact;
        
        System.debug('newContact:' + newContact);
      
        List<Contact> deletedContact = 
        [SELECT Id FROM Contact WHERE LastName = 'Bab'];
        
        System.debug('deletedContact:' + deletedContact);
        
        delete deletedContact;

        Test.startTest();    
        List<Contact> Contact = ContactTableProjectSecond.deleteContact(recordId);
        Test.stopTest();     

        System.assertEquals(deletedContact, contact, 'Contacts not searched'); 
   }
    
    @isTest
    public static void addContact_success_addedContact() {               
        Contact newContact = new contact(LastName = 'Sam');
        insert newContact;
        List<Contact> addedContact = [SELECT Id, LastName FROM Contact WHERE LastName = 'Sam'];    
        
        System.debug('addedContact:' + addedContact); 
                
        Test.startTest();    
        List<Contact> contact = ContactTableProjectSecond.addContact();        
        Test.stopTest();  
        
        System.debug('contact:' + contact); 
        
        System.assertEquals(contact, addedContact, 'Contact not added');        
    }       
CharuDuttCharuDutt
Hiii Mike Toi
Try below test Class
@isTest
public class exportobjectascsvbatchTest {
    @isTest
    public Static Void unitTest(){
        Account Acc = New Account();
        Acc.Name = 'Test Account';
        Insert Acc;
        Contact Con = new Contact();
        Con.LastName = 'Test Contact';
        Con.AccountId=  Acc.id;
        Insert Con;
        
        
        test.startTest();    
          ContactTableProjectSecond.deleteContact(Con.Id);
        ContactTableProjectSecond.addContact();
     
        test.stopTest();
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
AnkaiahAnkaiah (Salesforce Developers) 
Hi Mike,

What is the error you are getting?

Thanks!!
Mike Tol 1Mike Tol 1

Thank youа CharuDutt! But the test does not cover the code.
 
Mike Tol 1Mike Tol 1
Ankaiah, There is no error, just the test does not cover the code
mukesh guptamukesh gupta
Hi Mike,

Can you please share screen shot of class and with code coverage % in rightside of bottom in developer console.

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 



 
Mike Tol 1Mike Tol 1
User-added imageUser-added image
Mike Tol 1Mike Tol 1
User-added image
mukesh guptamukesh gupta
Hi Mike,

It's quite simple test class should be execute, i have tested this code in my org it's working. Please follow below url

https://atrium.ai/resources/how-to-test-code-coverage-in-salesforce-useful-tips-and-techniques/

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh