You need to sign in to do that
Don't have an account?
Rohan 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'));
}
}
}
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'));
}
}
}
Update your test class from below code its cover the 100% code coverage:
//Test class
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
Update your test class from below code its cover the 100% code coverage:
//Test class
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
- 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-------------------
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.