You need to sign in to do that
Don't have an account?
randheer practise
iam writing test class to DeleteDML class iam gettinh 80%coverage but iam unable to cover code in Catch block.thanks in advance
DeleteDMl class
public class DeleteDml {
public void accountInserting(){
//fetching old records with name kanus
List<Account> accounts =[SELECT id,name from Account WHERE name='kanus'];
try{
delete accounts;
}catch(Exception e){
system.debug('records are not deleted');
}
}
}
testclass
@istest
public class DeleteDML_test {
public static testMethod Account insertingAccount(){
test.startTest();
Account a = new Account();
a.Name='kanus';
insert a;
DeleteDml d = new DeleteDml();
d.accountInserting();
Test.stopTest();
return a;
}
public static testMethod void Method2(){
Test.startTest();
Account a1=insertingAccount();
Account a2=[SELECT id,name FROM Account WHERE name='kanus123'];
try{
delete a2;
}catch(Exception e){
system.debug('no record with name kanus123');
}
test.stopTest();
DeleteDml d1 = new DeleteDml();
d1.accountInserting();
}
}
public class DeleteDml {
public void accountInserting(){
//fetching old records with name kanus
List<Account> accounts =[SELECT id,name from Account WHERE name='kanus'];
try{
delete accounts;
}catch(Exception e){
system.debug('records are not deleted');
}
}
}
testclass
@istest
public class DeleteDML_test {
public static testMethod Account insertingAccount(){
test.startTest();
Account a = new Account();
a.Name='kanus';
insert a;
DeleteDml d = new DeleteDml();
d.accountInserting();
Test.stopTest();
return a;
}
public static testMethod void Method2(){
Test.startTest();
Account a1=insertingAccount();
Account a2=[SELECT id,name FROM Account WHERE name='kanus123'];
try{
delete a2;
}catch(Exception e){
system.debug('no record with name kanus123');
}
test.stopTest();
DeleteDml d1 = new DeleteDml();
d1.accountInserting();
}
}
In the above scenario a case was linked with an account. Hence its not possible to delete an account which has a related case.
therefore the catch block was executed returning 100% coverage.
All Answers
Please find the below modified test class which gives 100 percent code coverage.
@isTest
public class DeleteDML_test {
public static testmethod void accountInserting()
{
//account instance
account acc = new account();
acc.Name = 'kanus';
acc.BillingCity = 'hyderabad';
insert acc;
//case instance
case c = new case();
c.AccountId = acc.Id;
c.Subject = 'call';
insert c;
test.startTest();
DeleteDml DeleteAcc = new DeleteDml();
DeleteAcc.accountInserting();
test.stopTest();
}
}
Best Regards,
Manasa.G
}
In the above scenario a case was linked with an account. Hence its not possible to delete an account which has a related case.
therefore the catch block was executed returning 100% coverage.