0

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();
}
}
3
  • Instead of using bold for method names, consider using code formatting, with (```) backticks. It looks much cleaner, and is easier to identify as code. Consider: Controller.Method(parameter) against Controller.Method(parameter). You can read more about markdown in the help section. Commented Dec 13, 2016 at 14:59
  • @battery.cord fine I will use that next time I ask questions. what about solution?did you know Commented Dec 13, 2016 at 15:02
  • 2
    Sankar -- Community is at its greatest assistance when you take the advice from @battery.cord re: code formatting by using the edit link. Your code is not indented at all and many community members will just bypass this question.
    – cropredy
    Commented Dec 14, 2016 at 2:34

1 Answer 1

1

The problem lies in this line:

PartsList.add(new SiteParts(OLI.Product__r.Name,''));

You are creating a new SiteParts object by calling it's constructor and the second parameter in your case is just an empty string. Then you say, that you can not cover this if-condition:

if(!String.isBlank(SNum.serial)){

Let's look into Apex String methods:

isBlank(inputString)

Returns true if the specified String is white space, empty (''), or null; otherwise, returns false.

So when you make this check, isBlank() return true, because it's an empty string. The exclamation mark turns this true into a false. Therefore you will never get into this if-condition


I'll also give you some tips for writing test classes:

  • Use Test.startTest() and Test.stopTest() methods
  • It might be useful to move the setup part into another method
  • Make sure to test EVERY POSSIBLE SCENARIO! You can and should NOT test everything in one test method(or even worse: get coverage only)

And, probably the most important: USE ASSERTS WHEN WRITING TEST CLASSES!
Test classes are called test classes because they should actually test something and not only provide coverage, otherwise we would call them coverage classes. A test class without assert is useless, doesn't test anything and should not even be called a test class.

If, after this, some part of your code is still not covered ask yourself "Do I even need this code? When is it used?" and remove it.

1
  • I knew its not covered because of ! condition but in visualforce page its changed dynamically to some value hence if condition works that I can't make in test class .I want to know that how to do that. Commented Dec 14, 2016 at 7:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .