+ Start a Discussion
siva kurmasiva kurma 

Testclass help for before delete scenario? Test class is giving me 75% coverage but i want 100%

APEX CLASS
public class AccountOwnerCheck {
    public static void OwnerCheck(list<Account> VarAccList){
        for(Account VarAccounts:VarAccList){
            if(VarAccounts.OwnerId != UserInfo.getUserId()) {
                VarAccounts.addError('You dont have the permission to delete the record, Only Owner can delete');
            }          
        }
    }
}

APEX TRIGGER
trigger AccountDeleteTrigger on Account (before Delete) {
    
    if(Trigger.iSBefore == true && Trigger.isDelete == True){
        AccountOwnerCheck.OwnerCheck(Trigger.Old);
    }
}


TEST CLASS
@isTest
class AccountOwnerCheckTest {
   static testMethod void testMethod1() 
    {
            Account newAcc = new Account() ;
            newAcc.Name = 'Cole';
            insert newAcc;

            try
            {
                Delete     newAcc;
            }
            catch(Exception ee)
            {}
    }
}
Best Answer chosen by siva kurma
Ajay K DubediAjay K Dubedi
Hi Siva,
Try this test class:
@isTest
class AccountOwnerCheck_Test {
    @isTest static void testmethod1()
    {
        String orgId = UserInfo.getOrganizationId();
        String dateString = String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','');
        Integer randomInt = Integer.valueOf(math.rint(math.random()*1000000));
        String uniqueName = orgId + dateString + randomInt;
        Test.startTest();
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
        User usr = new User(Alias = 'standt', Email= uniqueName + '@test' + orgId + '.org', 
                            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                            LocaleSidKey='en_US', ProfileId = p.Id, 
                            TimeZoneSidKey='America/Los_Angeles', UserName= uniqueName + '@test' + orgId + '.org');
        insert usr ;
        createAcc(usr.Id);
    }
    @Future
    public static void createAcc(Id usrId)
    {
        try{
            
            Account newAcc = new Account() ;
            newAcc.Name = 'Test';
            newAcc.OwnerId = usrId;
            insert newAcc;
            delete newAcc;
        }
        catch(Exception ex)
        {
            System.debug('Exception on Line Number ' + ex.getLineNumber() + ' Message ---- ' + ex.getMessage());
        }
    }
}



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 Siva,
Try this test class:
@isTest
class AccountOwnerCheck_Test {
    @isTest static void testmethod1()
    {
        String orgId = UserInfo.getOrganizationId();
        String dateString = String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','');
        Integer randomInt = Integer.valueOf(math.rint(math.random()*1000000));
        String uniqueName = orgId + dateString + randomInt;
        Test.startTest();
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
        User usr = new User(Alias = 'standt', Email= uniqueName + '@test' + orgId + '.org', 
                            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                            LocaleSidKey='en_US', ProfileId = p.Id, 
                            TimeZoneSidKey='America/Los_Angeles', UserName= uniqueName + '@test' + orgId + '.org');
        insert usr ;
        createAcc(usr.Id);
    }
    @Future
    public static void createAcc(Id usrId)
    {
        try{
            
            Account newAcc = new Account() ;
            newAcc.Name = 'Test';
            newAcc.OwnerId = usrId;
            insert newAcc;
            delete newAcc;
        }
        catch(Exception ex)
        {
            System.debug('Exception on Line Number ' + ex.getLineNumber() + ' Message ---- ' + ex.getMessage());
        }
    }
}



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
siva kurmasiva kurma
yes the coverage is 100% but the code is too lengthy i felt. Can we reduce it?
Also I wrote a trigger to update contact fields based on Account fileds. I always thought after triggers are needed if you hvae to work on 1 record bassed on another record. Starnegly i could finish the job with before trigger. How ?