You need to sign in to do that
Don't have an account?
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;
}
}
Hi,
You can refer this link
http://abhithetechknight.blogspot.in/2013/10/salesforce-test-class-basics.html
@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:
Second, the test class:
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
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
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;
}
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