Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.
public class democlass{

public string invoiceid{get;set;}

PUBLIC DEMOCLASS(apexpages.standardcontroller controller){

invoiceid=apexpages.currentpage().getparameters().get('id');
**if(invoiceid!=null)
 {
  invoice=[select id,name,net__c,Policy_Status__c,Amount__c,Period__c from invoice_policy__c where id=:invoiceid];

//some code here
}**else{
  invoice=new invoice_policy__C();
}
}
public pagereference saveIn(){
    newinvoicepolicy=new invoice_policy__c();
   **if(invoiceid!=null){
//some code
}**else{
insert newinvoicepolicy;
  p=new Pagereference('/'+newinvoicepolicy.Id);
}
p.setredirect(true);
return p;
}

}

My Test class is

public static testmethod void policyTest(){

        Invoice_policy__c newinvoice = new invoice_policy__c(Name='Invoice',internal_alerts__c=true,American_express__c = true);
        insert newinvoice;
apexpages.currentpage().getparameters().put('invoiceid',newinvoice.id);
apexpages.standardcontroller controller;
        democlass policy = new democlass(controller);
        policy.saveIn();
}

Here I can't cover the invoiceid!=null part.wat should I do to achieve it

share|improve this question
add comment

2 Answers 2

Add the below code before calling initializing the demoClass.

Apexpages.standardcontroller controller = new Apexpages.standardcontroller(newinvoice); democlass policy = new democlass(controller); policy.saveIn();

share|improve this answer
    
nothing was changed....it's not covering the remaining part –  Eagerin Sf Jan 24 '13 at 10:21
    
which part are you not able to cover? the "If" part or the "else" part ? with the above lines, you will be able to cover "if" part of the code. To cover else part, you will have to mention apexpages.currentpage().getparameters().put('invoiceid',null); after and reinituialise the class. –  Shailesh Deshpande Jan 24 '13 at 10:29
    
if(invoiceid!=null){//code } This part –  Eagerin Sf Jan 24 '13 at 10:31
1  
There you are - change the parameter name that you are passing. apexpages.currentpage().getparameters().put('id',newinvoice.id); –  Shailesh Deshpande Jan 24 '13 at 10:42
    
it worked.Thanks alot. –  Eagerin Sf Jan 24 '13 at 10:46
add comment

You have a mistake in your test class

apexpages.currentpage().getparameters().put('invoiceid',newinvoice.id);  

It needs to be

apexpages.currentpage().getparameters().put('id',newinvoice.id);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.