Sign up ×
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){

  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

share|improve this question
I have the same issue. but my constructor is default constructor. public PhotoUploadController() { .................. } – Chetna Agrawal Oct 5 at 6:31

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'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
There you are - change the parameter name that you are passing. apexpages.currentpage().getparameters().put('id',; – Shailesh Deshpande Jan 24 '13 at 10:42
it worked.Thanks alot. – Eagerin Sf Jan 24 '13 at 10:46

You have a mistake in your test class


It needs to be

share|improve this answer

Your Answer


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.