+ Start a Discussion
mahemahe 

Apex test case help

Hi, I was trying to write test case for below class and I was covered 40% only.

Can you please help me to cover for 75% . thanks.

 

public

withsharingclassConfirm_UnreceiveDeliverables {

 

List<

Deliverable__c> Confirm_unReceiveDels{get; set;}

string[] deliverableArray;

string tempDels{

get; set;}

 

public string oppId='';

 

publicConfirm_UnreceiveDeliverables() {

oppId= ApexPages.currentPage().getParameters().

get('id');

tempDels= ApexPages.currentPage().getParameters().

get('DeliverablesIds');

 

if(tempDels <> null){

deliverableArray = tempDels.split(

',');

getConfirm_unReceiveDels();

}

}

publicConfirm_UnreceiveDeliverables(ApexPages.StandardSetController controller) {

oppId= ApexPages.currentPage().getParameters().

get('id');

tempDels= ApexPages.currentPage().getParameters().

get('DeliverablesIds');

 

if(tempDels <> null){

deliverableArray = tempDels.split(

',');

getConfirm_unReceiveDels();

}

}

public List<Deliverable__c> getConfirm_unReceiveDels() {

 

if(Confirm_unReceiveDels == null) Confirm_unReceiveDels = [select id,IsLocked,Name,Amount_Paid__c,Balance__c,Deliverable_Number__c,Partial_Amount__c,Make_Partial_Payment__c,Notify_DS_Owner_for_PO_TroubleShoot_Del__c,Work_Approved_By__c,Approved_By_Date__c,Contractor_Invoice__c, Deliverable_Amount__c,due_date__c,Approval_Status__c,Deliverable_Description__c,Opportunity__c fromDeliverable__cwhere Id IN:deliverableArray ];//OR (Approval_Status__c =: 'Deliverable Approved for Payment')returnConfirm_unReceiveDels ;

}

publicvoidUnReceiveDeliverable(){

 

try{ List<Deliverable__c> unReceivedDels = new List<Deliverable__c>();

Integer[] k =

newInteger[]{};

Integer j = 0;

if(Confirm_unReceiveDels.size()>0){

 

for(Deliverable__cdel : Confirm_unReceiveDels){

del.Notify_DS_Owner_for_PO_TroubleShoot_Del__c =

true;

 

if(del.Notify_DS_Owner_for_PO_TroubleShoot_Del__c){

 

// create the new approval request to submitApproval.ProcessSubmitRequest req = newApproval.ProcessSubmitRequest();

req.setComments(

'Submitted for Unreceive deliverable/PO line.');

req.setObjectId(del.Id);

// submit the approval request for processingApproval.ProcessResult result = Approval.process(req);

 

// display if the reqeust was successful

System.debug(

'Submitted for approval successfully: '+result.isSuccess());

}

if(del.Notify_DS_Owner_for_PO_TroubleShoot_Del__c){

k.add(j);

del.Approval_Status__c =

'Not Completed';

del.Work_Approved_By__c=

null;

del.Approved_By_Date__c=

null;

del.Contractor_Invoice__c =

null;

del.Invoice_Date__c =

null;

 

if(del.Make_Partial_Payment__c){

del.Make_Partial_Payment__c=

false;

del.Partial_Amount__c=Decimal.valueOf(

'0');

}

del.Notify_DS_Owner_for_PO_TroubleShoot_Del__c =

false;

unReceivedDels.Add(del);

}

j++;

}

updateunReceivedDels;

 

//for(Integer i : k){//if(unReceiveDels[i].Notify_DS_Owner_for_PO_TroubleShoot_Del__c){//unReceiveDels.remove(i);//} //}if(unReceivedDels.size() >1)

ApexPages.addmessage(

new ApexPages.message(ApexPages.severity.confirm,'You have successfully UnReceived in Salesforce. Please click “Close” button to close this window or click “UnReceive another deliverable” button if you want to unreceive another deliverable.'));

 

if(unReceivedDels.size() == 1)

ApexPages.addmessage(

new ApexPages.message(ApexPages.severity.confirm,'You have successfully UnReceived in Salesforce. Please click “Close” button to close this window or click “UnReceive another deliverable” button if you want to unreceive another deliverable.'));

 

}

}

catch(Exception e){

ApexPages.addmessage(

new ApexPages.message(ApexPages.severity.ERROR ,'Following error occured while Receiving '+ ApexPages.currentPage().getParameters().get('id')+ e));

}

 

}

public

PageReference goBack()

{

string id = ApexPages.currentPage().getParameters().

get('id');

PageReference OppPage = Page.Un_Receive_Deliverables;

////PageReference('https://cs3.salesforce.com/apex/Un_Receive_Deliverables?&id=' + oppId);

OppPage.getParameters().put(

'id', id);

OppPage.setRedirect(

true);

 

returnOppPage;

 

}

publicPageReference CancelAction(){

string id = ApexPages.currentPage().getParameters().

get('id');

PageReference OppPage =

new PageReference('https://na3.salesforce.com/'+ oppId);

OppPage.setRedirect(

true);

 

returnOppPage;

}

 

 

}

 

 

GlynAGlynA

@mahe,

 

I have done two things.  I have refactored your controller extension using some techniques that make it easier to test, and I have written a test class that should cover 100% of the code.

 

First, the controller extension:

 

public with sharing class Confirm_UnreceiveDeliverables
{
    public Boolean throwException = false;

    public String oppId
    {
        get
        {
            if ( oppId == null )
            {
                oppId = ApexPages.currentPage().getParameters().get( 'id' );
            }
            return oppId;
        }
private set; } public List<String> deliverableArray { get { if ( deliverableArray == null ) { String tempDels = ApexPages.currentPage().getParameters().get( 'DeliverablesIds' ); if ( tempDels != null ) { deliverableArray = tempDels.split( ',' ); } } return deliverableArray; } private set; } public List<Deliverable__c> Confirm_unReceiveDels { get { if ( Confirm_unReceiveDels == null ) { Confirm_unReceiveDels = [ SELECT Id, IsLocked, Name, Amount_Paid__c, Balance__c, Deliverable_Number__c, Partial_Amount__c, Make_Partial_Payment__c,
Notify_DS_Owner_for_PO_TroubleShoot_Del__c, Work_Approved_By__c, Approved_By_Date__c, Contractor_Invoice__c,
Deliverable_Amount__c, Due_Date__c, Approval_Status__c, Deliverable_Description__c ,Opportunity__c FROM Deliverable__c WHERE Id IN :deliverableArray ]; } return Confirm_unReceiveDels; } private set; }
public List<Deliverable__c> getConfirm_unReceiveDels()
{
return Confirm_unReceiveDels;
}
public Confirm_UnreceiveDeliverables( ApexPages.StandardSetController controller ) { // nothing to do here } public Confirm_UnreceiveDeliverables() { this( (ApexPages.StandardSetController) null ); } public void UnReceiveDeliverable() { if ( Confirm_unReceiveDels.isEmpty() ) return; try { if ( Test.isRunningTest() && throwException ) throw new NullPointerException(); List<Deliverable__c> unReceivedDels = new List<Deliverable__c>(); for ( Deliverable__c del : Confirm_unReceiveDels ) { // create the new approval request to submit Approval.ProcessSubmitRequest req = newApproval.ProcessSubmitRequest(); req.setComments( 'Submitted for Unreceive deliverable/PO line.' ); req.setObjectId( del.Id ); // submit the approval request for processing Approval.ProcessResult result = Approval.process( req ); // display if the reqeust was successful System.debug( 'Submitted for approval successfully: '+ result.isSuccess() ); del.Approval_Status__c = 'Not Completed'; del.Work_Approved_By__c = null; del.Approved_By_Date__c = null; del.Contractor_Invoice__c = null; del.Invoice_Date__c = null; if ( del.Make_Partial_Payment__c ) del.Partial_Amount__c = 0; del.Make_Partial_Payment__c = false; unReceivedDels.add( del ); } update unReceivedDels; if ( !unReceivedDels.isEmpty() ) { ApexPages.addmessage( new ApexPages.message( ApexPages.Severity.CONFIRM, 'You have successfully UnReceived in Salesforce. Please click “Close” button to close this window or click “UnReceive another deliverable” button if you want to unreceive another deliverable.' ) ); } } catch ( Exception e ) { ApexPages.addmessage( new ApexPages.message(ApexPages.Severity.ERROR, 'Following error occured while Receiving ' + oppId + e ) ); } } public PageReference goBack() { PageReference OppPage = Page.Un_Receive_Deliverables; OppPage.getParameters().put( 'id', oppId ); OppPage.setRedirect( true ); return OppPage; } public PageReference CancelAction() { PageReference OppPage = new PageReference( '/' + oppId ); OppPage.setRedirect( true ); return OppPage; } }

 

Second, the test class:

 

@isTest
public class Confirm_UnreceiveDeliverablesTest
{
    public static testMethod void test()
    {
        Deliverable__c deliverable = new Deliverable__c
        (   //  initialize fields here
        );
        insert deliverable;

        Test.setCurrentPage( Page.Un_Receive_Deliverables );
        ApexPages.currentPage().getParameters().put( 'id', deliverable.Id );
        ApexPages.currentPage().getParameters().put( 'DeliverablesIds', deliverable.Id );

        Test.startTest();

        Confirm_UnreceiveDeliverables controller = new Confirm_UnreceiveDeliverables();
        controller.UnReceiveDeliverable();
        controller.throwException = true;
        controller.UnReceiveDeliverable();
        controller.goBack();
        controller.CancelAction();

        Test.stopTest();
    }
}

 

You'll have to provide the initialization of your Deliverable__c record in the test method.  I don't know how you want it to be initialized.

 

Let me know if this has any errors in it or if you have any questions about it.  I will be happy to help.  Good luck with your project!

 

If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them. Thanks!

 

-Glyn Anderson
Certified Salesforce Developer | Certified Salesforce Administrator

mahemahe

I am getting error : Save error: member variable not visible for assignment

 

publicString oppIds

{

get

{

if ( oppIds == null)

{

oppIds = ApexPages.currentPage().getParameters().

get( 'id');

}

returnoppIds;

}

 

}

 

 

and I know we have add set {} but even I am getting error. can you please advice

mahemahe

Hi Glyn,

 

I am getting error  that "

The method LIST<Deliverable__c> getConfirm_unReceiveDels() is referenced by Visualforce Page (Confirm_UnReceiveDeliverables) in salesforce.com.

Remove the usage and try again."

 

public

List<String> deliverableArray

{

get

{

if ( deliverableArray == null)

{

 

String tempDels = ApexPages.currentPage().getParameters().

get( 'DeliverablesIds');

 

if ( tempDels != null)

{

deliverableArray = tempDels.split(

',');

}

}

returndeliverableArray;

}

privateset;

}

GlynAGlynA

I made a couple of edits to my code above.  First, I added "private set;" to oppIDs, to fix that problem.  Second, I added the method "getConfirm_unReceiveDels" - I don't think this should be necessary, but let's see if it fixes the error.

 

-Glyn