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
Rohan SRohan S 

Need to increase code coverage to 100%

I have written an Apex class and its Test class which restricts a user from deleting an Account record if he is not the Account owner, but the class currently has 75% code coverage. How do I increase it to 100%?

Apex Class:
public class AccountDeleteCheckClass {
//Declare function if account owner is not the deleting user
public static void AccountDeleteCheck(list<Account> AccountsList){
for(Account VarA : AccountsList){
if(VarA.OwnerId != UserInfo.getUserId()){
VarA.addError('Only owner can delete');
}
}
}
}

Test Class:
@isTest
class AccountDeleteCheckClassTest
{
static testmethod void Function()
{
//Insert & delete a record
Account VarA = new Account();
VarA.Name = 'ABC Corp';
insert VarA;

//Check deletion
try {
delete VarA;
}
catch(Exception e)
{
System.assert(e.getMessage().contains('Only owner can delete'));
}
}
}
Best Answer chosen by Rohan S
Ajay K DubediAjay K Dubedi
Hi Rohit,
Update your test class from below code its cover the 100% code coverage:

//Test class 
 
@isTest
class AccountDeleteCheckClassTest{
    static testmethod void Function(){
        //Insert & delete a record
        List<Account> accList = new  List<Account>();
        Account accObj = new Account();
       
        accObj.Name = 'ABC Corp';
        accList.add(accObj);
        insert accList;
        
        Test.startTest();
        //Check deletion
        try {
            AccountDeleteCheckClass.AccountDeleteCheck(accList);
        }catch(Exception e){
            System.assert(e.getMessage().contains('Only owner can delete'));
        }
        Test.stopTest();
    }
}



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

Thanks,
Ajay Dubedi

All Answers

Ajay K DubediAjay K Dubedi
Hi Rohit,
Update your test class from below code its cover the 100% code coverage:

//Test class 
 
@isTest
class AccountDeleteCheckClassTest{
    static testmethod void Function(){
        //Insert & delete a record
        List<Account> accList = new  List<Account>();
        Account accObj = new Account();
       
        accObj.Name = 'ABC Corp';
        accList.add(accObj);
        insert accList;
        
        Test.startTest();
        //Check deletion
        try {
            AccountDeleteCheckClass.AccountDeleteCheck(accList);
        }catch(Exception e){
            System.assert(e.getMessage().contains('Only owner can delete'));
        }
        Test.stopTest();
    }
}



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

Thanks,
Ajay Dubedi
This was selected as the best answer
Rohan SRohan S
It worked. Thanks Ajay :)
Deepali KulshresthaDeepali Kulshrestha
Hi Rohit,

- I read your problem and implemented it in my Org and it is working fine.
- Please use the below code (Solved with 100% code coverage)

----------------Test Class-------------------
@isTest public class AccountDeleteCheckClass_Test {
    public static testmethod void TestAccountProcessorTest()
        {
            Account a = new Account();
            a.Name = 'Test Account';
            Insert a;
            List<Account> acclist=new List<Account>();
            acclist.add(a);
            Test.startTest();
            AccountDeleteCheckClass.AccountDeleteCheck(acclist);
            System.assertEquals ( acclist.size() ,1);
            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,
Deepali Kulshrestha.