public class democlass{

public string invoiceid{get;set;}

PUBLIC DEMOCLASS(apexpages.standardcontroller controller){

  invoice=[select id,name,net__c,Policy_Status__c,Amount__c,Period__c from invoice_policy__c where id=:invoiceid];

//some code here
  invoice=new invoice_policy__C();
public pagereference saveIn(){
    newinvoicepolicy=new invoice_policy__c();
//some code
insert newinvoicepolicy;
  p=new Pagereference('/'+newinvoicepolicy.Id);
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.standardcontroller controller;
        democlass policy = new democlass(controller);

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

  • I have the same issue. but my constructor is default constructor. public PhotoUploadController() { .................. } – user25200 Oct 5 '15 at 6:31

Add the below code before calling initializing the demoClass.

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

  • 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

You have a mistake in your test class


It needs to be


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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