function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
vibrationvibration 

test coverage is only 50% for following code

public class New2 {

    public  Invoice_Statement__c accc {get; set;}
    public  Line_Items__c accc1 {get; set;}
    public New2(){
    accc =new Invoice_Statement__c();
    accc1 =new Line_Items__c();
    }
        
    public PageReference save() {         
           insert(accc); 
             accc1.Invoice_Statement__c=accc.id;                    
           insert(accc1); 
      return (new ApexPages.StandardController(accc)).view();   
       }  

 

test coverage=50%

 

my test class

 

@isTest
private class invtest {
static testMethod void invtest1() {
New2 invo = new New2();
    Invoice_Statement__c acccc = new Invoice_Statement__c(); 
         acccc.Description__c='desci';
         acccc.Status__c='open'; 
       
                                          
         insert acccc;
      
         
         Line_Items__c acccc1 = new Line_Items__c();
         acccc1.Unit_Price__c=3;
         acccc1.Units_Sold__c=4;
         acccc1.Invoice_Statement__c=acccc.id;
                                      
         insert acccc1;
        
  
          
       
         }
}
Best Answer chosen by Admin (Salesforce Developers) 
Jon Mountjoy_Jon Mountjoy_

Hi vibration

 

I suggest you work through the Force.com Workbook (it looks like you've made a start?), which you can find here:

http://developer.force.com/workbooks as well as An Introduction to Test Methods and the Apex Developer Guide, to get the basics.

 

At some point in your test you are going to want to invoke:

 

New2.save();

 

And check it's return value, and perhaps whether the line items were added (using "assert" and queries).

 

Jon

All Answers

Jon Mountjoy_Jon Mountjoy_

Yep - looks like it is.

 

Not sure what your question is.

 

Do you know that when you run your tests in the web browser, and it says "50%", that you can click on that and see a coloured depiction of your source that that indicates which lines aren't tested?

 

In this case, it doesn't look like your test method calls "save" at all.

 

PS. Your test method also doesn't test anything, but I'm hoping that's because you were trying to figure out the code coverage 

vibrationvibration

Yes . i am new in salesforce. code coverage is 50% save part is not tested. plz tell the solution of program

Jon Mountjoy_Jon Mountjoy_

Hi vibration

 

I suggest you work through the Force.com Workbook (it looks like you've made a start?), which you can find here:

http://developer.force.com/workbooks as well as An Introduction to Test Methods and the Apex Developer Guide, to get the basics.

 

At some point in your test you are going to want to invoke:

 

New2.save();

 

And check it's return value, and perhaps whether the line items were added (using "assert" and queries).

 

Jon

This was selected as the best answer
vibrationvibration

Thank u very much . 90% code is coverage.

 

but return statement is not covered.

 

public class New1 {
 2   
 3 1   public Invoice_Statement__c accc {get; set;}
 4   
 5 1   public New1() {
 6   
 7   
 8   
 9   
 10 1   accc =new Invoice_Statement__c();
 11   
 12   
 13    }
 14   
 15 1   public PageReference save() {
 16 1   try {
 17 1   upsert(accc);
 18 1   } catch(System.DMLException e) {
 19 1   ApexPages.addMessages(e);
 20 1   return null;
 21   
 22    }
 23   
 24 0   return (new ApexPages.StandardController(accc)).view();
 25    }
 26   }

 

Jon Mountjoy_Jon Mountjoy_

So let's think about it.

 

Line 24 is not being executed right?

 

What will cause that to happen?  What will cause execution to stop before Line 24?

 

Well, the save() method would have to terminate. 

 

What other routes are there for the save() method to terminate?

 

Aha - Line 20.  It has a return right?  AND, it's not red - which means it's being executed.

 

So your code is stopping on Line 20. 

 

So you need another test, with better data, so that the upsert doesn't fail. 

 

If you expected the return of that save to be null, you should have probably done something like:

 

PageReference rs = mynew1.save();

assertEquals(null, rs);   // I expect it to be null

 

Then of course, you should also try and write code that makes it succeed and NOT return null.

 

ie.

 

// some other statements inserting/modifying record

PageReference rs2 = mynew1.save();

assert( rs2 != null);  

//perhaps even more, checing the return value.

 

vibrationvibration

System.AssertException: Assertion Failed.

 

 

@isTest
private class invtest2 {

static testMethod void invtest1() {   

    Invoice_Statement__c acccc = new Invoice_Statement__c();     
     acccc.Description__c='desci';
     acccc.Status__c='open';  
           
     insert acccc;
          // acccc= [select Description__c from Invoice_Statement__c where id =:acccc.id ]; 
      // System.assertEquals('desci',acccc.Description__c); 
    
          
      New1 invo = new New1();
     
   
                                                     
       invo.save(); 
      PageReference rs = invo.save();
      System.assert(rs != null);
      
             
       

         }

}

Jon Mountjoy_Jon Mountjoy_

Yes, your assertion failed because it is equal to null.  Just like I said.  Please read what I said above, and also my first post where I show you some resources.