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
Sunay - KVP Bus SolnsSunay - KVP Bus Solns 

Need Help on Test class to get the code coverage

Hi,

 

I am trying to write the test method for the below class, but I am not able to cross the coverage of 65%. Can anybody help me on this as it is very urgent.

 

Please find the code below of both Apex class and the test class:

 

public class AddLineItems
{

public AddLineItems(ApexPages.StandardSetController controller)
{
op=[Select id,Name from Opportunity where id=:ApexPages.currentPage().getParameters().get('id')];

lineitem1= new App_Account_Opportunity_Link__c();
lineitem2= new App_Account_Opportunity_Link__c();
lineitem3= new App_Account_Opportunity_Link__c();
lineitem4= new App_Account_Opportunity_Link__c();
lineitem5= new App_Account_Opportunity_Link__c();
lineitem6= new App_Account_Opportunity_Link__c();
lineitem7= new App_Account_Opportunity_Link__c();
lineitem8= new App_Account_Opportunity_Link__c();
lineitem9= new App_Account_Opportunity_Link__c();
lineitem10= new App_Account_Opportunity_Link__c();

}

public App_Account_Opportunity_Link__c lineitem1{get;set;}
public App_Account_Opportunity_Link__c lineitem2{get;set;}
public App_Account_Opportunity_Link__c lineitem3{get;set;}
public App_Account_Opportunity_Link__c lineitem4{get;set;}
public App_Account_Opportunity_Link__c lineitem5{get;set;}
public App_Account_Opportunity_Link__c lineitem6{get;set;}
public App_Account_Opportunity_Link__c lineitem7{get;set;}
public App_Account_Opportunity_Link__c lineitem8{get;set;}
public App_Account_Opportunity_Link__c lineitem9{get;set;}
public App_Account_Opportunity_Link__c lineitem10{get;set;}

public Opportunity op{get;set;}



public AddLineItems()
{
}


Public PageReference savedata()
{

if(lineitem1.App_Account__c!= null)
{
lineitem1.Opportunity__c= op.Id;
insert lineitem1;
}

if(lineitem2.App_Account__c!= null)
{
lineitem2.Opportunity__c=op.Id;
insert lineitem2;
}

if(lineitem3.App_Account__c!= null)
{
lineitem3.Opportunity__c=op.Id;
insert lineitem3;
}

if(lineitem4.App_Account__c!= null)
{
lineitem4.Opportunity__c=op.Id;
insert lineitem4;
}

if(lineitem5.App_Account__c!= null)
{
lineitem5.Opportunity__c=op.Id;
insert lineitem5;
}

if(lineitem6.App_Account__c!= null)
{
lineitem6.Opportunity__c=op.Id;
insert lineitem6;
}

if(lineitem7.App_Account__c!= null)
{
lineitem7.Opportunity__c=op.Id;
insert lineitem7;
}

if(lineitem8.App_Account__c!= null)
{
lineitem8.Opportunity__c=op.Id;
insert lineitem8;
}

if(lineitem9.App_Account__c!= null)
{
lineitem9.Opportunity__c=op.Id;
insert lineitem9;
}

if(lineitem10.App_Account__c!= null)
{
lineitem10.Opportunity__c=op.Id;
insert lineitem10;
}



PageReference oppty=new PageReference('/' + op.Id);

oppty.setRedirect(true);

return oppty;
}


}

 

Test Class:

@istest
private class TestMassAddLineItems
{
static testmethod void MassAddLineItems()
{
Id userId =UserInfo.getUserId();

User us =[Select id from User where id=:userId ];

Opportunity o=new Opportunity();
o.Name='Test Opportunity';
o.StageName='Hotel Loading Only';
o.CloseDate=date.Today();
o.Client_Services_Manager__c=us.id;
insert o;


App_Account__c a = new App_Account__c();
a.Name='Test App Account';
insert a;


App_Account_Opportunity_Link__c aaol=new App_Account_Opportunity_Link__c();
aaol.Opportunity__c=o.id;
aaol.App_Account__c=null;
insert aaol;

List<Opportunity> opp = [Select id from Opportunity where id = :o.Id];


ApexPages.StandardSetcontroller sc1 = New ApexPages.StandardSetController(opp);
System.CurrentPageReference().getParameters().put('Id',o.Id);

AddLineItems li1=new AddLineItems();
AddLineItems li=new AddLineItems(sc1);

li.savedata();

aaol.App_Account__c = a.Id;
update aaol;

li.savedata();



}

}

sfdcfoxsfdcfox

I'd worry more about Bulkifying your code before you worry about coverage; this would make your test code 10x smaller than it would be otherwise.

Sunay - KVP Bus SolnsSunay - KVP Bus Solns

Hi,

 

I tried bulkifying but the functionality did not work as per my requirement. Can you help me on that!!!