I have give here some important code where I struck to make coverage.I struck in SaveAssest() if condition under for loop. In getparts() partslist serial number is set as null but in visual force page we change that dynamically by calling that function and by changing its field but I can't able to do in test class.
If I call getparts() from test class partslist 2nd parameter is set as null so I can't able to cover that if condition in SaveAssest() which checks for not blank.How to resolve this.
Controller:
public class SitePartsUpdate{
public List<SiteParts> getParts(){
if(flag){
PartsList=new List<SiteParts>();
for(SalesOrderLineItems__c OLI:SalesOLI){
for(integer i=0;i<OLI.Quantity__c;i++){
PartsList.add(new SiteParts(OLI.Product__r.Name,''));
}
}
}
system.debug('!!!!!!!!!!!!!!'+PartsList);
return PartsList;
}
public pagereference SaveAssest(){
urlFlag= false;
system.debug('====================================='+PartsList);
SitePartsUpdate();
flag=false;
UpdateSN=new List<Inventory_Transaction__c>();
RepeatSN=new Set<String>();
for(SiteParts SNum :PartsList){
if(!String.isBlank(SNum.serial)){
system.debug('&&&&&&&&&&&&'+SNum.serial+'@@@@@@@@@@@@@');
if(SN.containsKey(SNum.serial)){
system.debug('>>>>>>>>>'+SN.get(SNum.serial).Product__c+'>>>>>>>>>'+SNum.productName+'>>>>>>>>>'+SN.get(SNum.serial).Name+'>>>>>>>>>'+SNum.serial);
if(SN.get(SNum.serial).Product__c==SNum.productName && SN.get(SNum.serial).Name==SNum.serial){
system.debug('~~~~~~~~~~~~~~~~'+SNum.serial+'@@@@@@@@@@@@@');
if(!RepeatSN.contains(SNum.serial)){
system.debug('@@@@@@@@'+SNum.serial+'@@@@@@@@@@@@@');
RepeatSN.add(SNum.serial);
SN.get(SNum.serial).Transaction_Type__c='Delivered';
SN.get(SNum.serial).SalesOrder__c=SalesId;
if(SNum.selected){
UpdateSN.add(SN.get(SNum.serial));
} else {
system.debug('SN ======'+SN.get(SNum.serial));
unSelectedSN.add(SN.get(SNum.serial));
}
system.debug('$$$$$$$$$'+SNum.serial+'@@@@@@@@@@@@@');
}else{ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Serial Number must be unique'));
return null;
}
}else{ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Wrong Serial Number'));
return null;
}
}
else{ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Wrong Serial Number'));
return null;
}
}else{ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Serial Number is mandatory'));
return null;
}
}
}
public class SiteParts{
public String productName{get; set;}
public String serial{get;set;}
public Boolean selected{get;set;}
public SiteParts(String Name, String SN){
productName=Name;
serial=SN;
}
}
}
Visualforce page:
<apex:page controller="SitePartsUpdate" action="{!SitePartsUpdate}">
<apex:form>
<apex:pageBlock>
<apex:pageBlock id="MPbid">
<apex:pageblockButtons >
<apex:commandButton value="Save" action="{!SaveAssest}" reRender="MPbid" />
<apex:commandButton value="Cancel" action="{!Cancel}"/>
</apex:pageblockButtons>
<apex:pageBlockTable value="{!Parts}" var="prt" id="Pbid">
<apex:column headerValue="Log Asset">
<apex:inputCheckbox value="{!prt.selected}" />
</apex:column>
<apex:column headerValue="Product Name">
<apex:outputText >{!prt.productName}</apex:outputText>
</apex:column>
<apex:column headerValue="Serial Number">
<apex:inputtext value="{!prt.serial}" styleClass="jq_req" />
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Test class:
@isTest(seeAlldata=true)
public class SitePartsUpdateTest{
static testMethod void SitePartsUpdateMethod(){
Apexpages.currentpage().getparameters().put('id',String.valueof(Sales.Id));
SitePartsUpdate Spu=new SitePartsUpdate();
Spu.SitePartsUpdate();
List<SitePartsUpdate.SiteParts> PartsList=new List<SitePartsUpdate.SiteParts>();
SitePartsUpdate.SiteParts sp=new SitePartsUpdate.SiteParts('Test','123');
PartsList.add(sp);
Spu.getParts();
Spu.SaveAssest();
Spu.Cancel();
}
}
code formatting
, with (```) backticks. It looks much cleaner, and is easier to identify as code. Consider: Controller.Method(parameter) againstController.Method(parameter)
. You can read more about markdown in the help section.