You need to sign in to do that
Don't have an account?
randheer practise
Iam getting 95% of code coverage but iam unable to cover the catch block in my test class to the below upsert class,can anyone help me
public class UpsertDml {
public list<Account> acc{get;set;}
//default constructor1
public UpsertDml(){
acc=new list<Account>();
}
public void upsertAccount()
{
Account a = new Account(Name='krishna');
try{
insert a;
}catch(Exception e){
system.debug('Record '+a.Name +' inserted successfully');
}
List<Account> accounts=[SELECT id,name FROM Account WHERE name='kanusupdated'];
If(accounts.size() > 0){
try{
for(Account a1:accounts)
{
if(a1.name=='kanusupdated')
{
Account a2=new Account();
a2.id=a1.id;
a2.name='kanus updated';
acc.add(a2);
}
}
update acc;
system.debug('records were updated successfully===='+acc);
}catch(DMLException e){
system.debug('records were not updated====='+e);
}
}
}
}
testclass.
@isTest
public class upsertDml_Test {
//to cover first Exception
public static testMethod void main1Method(){
Account a = new Account();
a.Name='krihna';
a.BillingCity='khhgkg';
insert a;
upsertDml sc = new upsertDml();
sc.upsertAccount();
}
public static testmethod list<Account> recordCreation(){
List<Account> ac = new List<Account>();
Account a = new Account();
a.Name = 'kanusupdated';
ac.add(a);
return ac;
}
public static testmethod void P_main(){
Test.startTest();
list<Account> acc1 = recordCreation();
insert acc1;
upsertDml sc = new upsertDml();
sc.acc = recordCreation();
system.debug('values in sc.acc ============='+sc.acc);
sc.upsertAccount();
Test.stopTest();
}
}
public list<Account> acc{get;set;}
//default constructor1
public UpsertDml(){
acc=new list<Account>();
}
public void upsertAccount()
{
Account a = new Account(Name='krishna');
try{
insert a;
}catch(Exception e){
system.debug('Record '+a.Name +' inserted successfully');
}
List<Account> accounts=[SELECT id,name FROM Account WHERE name='kanusupdated'];
If(accounts.size() > 0){
try{
for(Account a1:accounts)
{
if(a1.name=='kanusupdated')
{
Account a2=new Account();
a2.id=a1.id;
a2.name='kanus updated';
acc.add(a2);
}
}
update acc;
system.debug('records were updated successfully===='+acc);
}catch(DMLException e){
system.debug('records were not updated====='+e);
}
}
}
}
testclass.
@isTest
public class upsertDml_Test {
//to cover first Exception
public static testMethod void main1Method(){
Account a = new Account();
a.Name='krihna';
a.BillingCity='khhgkg';
insert a;
upsertDml sc = new upsertDml();
sc.upsertAccount();
}
public static testmethod list<Account> recordCreation(){
List<Account> ac = new List<Account>();
Account a = new Account();
a.Name = 'kanusupdated';
ac.add(a);
return ac;
}
public static testmethod void P_main(){
Test.startTest();
list<Account> acc1 = recordCreation();
insert acc1;
upsertDml sc = new upsertDml();
sc.acc = recordCreation();
system.debug('values in sc.acc ============='+sc.acc);
sc.upsertAccount();
Test.stopTest();
}
}
Try this and rework your class and let me know if it works for you.