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
Raju Mushke 19Raju Mushke 19 

i am writing test class for below apex class

i am writing test class for below apex class. i am new to the apex coding.can anyone please help on this.

Apex class:

public class ContactsapexController {
    
    @AuraEnabled
    public static List<Account> getAccounts(){
        return [select id, Name, description,Owner.Name,Phone from Account];
    }
    
    @AuraEnabled
    public static List<Contact> getContacts(string accountId){
        list<contact> conlist=new list<contact>();
        system.debug(accountId);
        
        list<contact> con=[select id, Name, Title, Email, Phone, Account.Owner.Name from Contact where accountId =: accountId];
        for(contact c:con){
            conlist.add(c);
        }
        return conlist;
    }
    
    @AuraEnabled
    public static List<String> deleteRecords(List<contact> lstId){
        system.debug('lstId'+lstId);
        // for store Error Messages  
        List<String> oErrorMsg = new List<String> ();
        
        List<Contact> contacts = [select id from Contact where id in : lstId];
        Database.DeleteResult[] DR_Dels = Database.delete(contacts, false);
        for (Database.DeleteResult dr: DR_Dels){
            if (dr.isSuccess()) {
                system.debug('successful delete contact');
                // Operation was successful
            } else {
                // Operation failed, so get all errors   
                for (Database.Error err: dr.getErrors()) {
                // add Error message to oErrorMsg list and return the list
                    oErrorMsg.add(err.getStatusCode() + ': ' + err.getMessage());
                }
            }
        }
        return oErrorMsg;
    }
}
Rushita Bavishi 1Rushita Bavishi 1
Hi,
Try with this code:
@isTest
private class ContactsapexControllerTest{

	 static testMethod void testMoveOpenOnly(){

		Test.startTest();

			List<Account> accList = new List<Account>();
			Account objAcc = new Account();
			objAcc.Name = 'Test Account';
			// insert required fields of Account
			accList.add(objAcc);
			insert accList;

			List<Contact> conList = new List<Contact>();
			Contact objContact = new Contact();
			objContact.LastName = 'test Contact';
			objContact.AccontId = objAcc.Id;
			// insert required field of contact
			conList.add(objContact);
			insert conList;
			
            ContactsapexController.getAccounts(accList);
			ContactsapexController.getContacts(objAcc.Id);
			ContactsapexController.deleteRecords(conList);
			
			Contact deletedCon = [SELECT Id, IsDeleted FROM contact WHERE Id = :objContact.Id ALL ROWS];
			//System.asserEquals(deletedCon.IsDeleted, true);
		Test.stopTest();

	}
}

 
sachinarorasfsachinarorasf
Hi Raju,

I have gone through your problem. Please try the below code.
 
@isTest
private class ContactsapexController_Test {
    @isTest static void getAccounts_Method(){
        Account accountObj = new Account();
        accountObj.Name = 'AccName';
        insert accountObj;
        List<Contact> contList = new List<Contact>();
        Contact contObj = new Contact();
        contObj.LastName = 'ContLastName';
        contObj.AccountId = accountObj.Id;
        contList.add(contObj);
        insert contList;
        Test.startTest();
        List<Account> result1 = ContactsapexController.getAccounts();
        System.assertEquals(true,result1!=null);
        List<Contact> result2 = ContactsapexController.getContacts(accountObj.Id);
        System.assertEquals(true,result2!=null);
        List<String> result3 = ContactsapexController.deleteRecords(contList);
        System.assertEquals(true,result3!=null);
        Test.stopTest();
        
    }

}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
Raju Mushke 19Raju Mushke 19

Hi

i have wriiten same as per you code.but i am unable cover the code for below two lines.
else {
                // Operation failed, so get all errors   
                for (Database.Error err: dr.getErrors()) {
                // add Error message to oErrorMsg list and return the list
                    oErrorMsg.add(err.getStatusCode() + ': ' + err.getMessage());
                }

could you please suggest how to write neagtive senroio for deletemethod.

Thanks
raju Mushke.