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
RajiiiRajiii 

Return statement not covered in test method

 

I have written a test method for webservice method.  Here i am facing code coverage problem.

 

The below image covered till the line of 'insert u' but i am not sure why it hasn't cover the line of return statement. Here no issues in Insert statement. The test method was passed successfully.

 

  public static ID createuser('testuser','lastname')

  {

    ---------------....

   ...........................

   insert u;  // Covered till this line

   return u.id;  //this line not covered

 }

The above method not works at return statement so that method declaration place also not covered.

 

My question is why the return statement is not covered even no issues in test method. Anybody pls help.

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Based on my experience, it sounds like the insert u dml statement failed, but a try-catch block further up the execution stack caught the error (thus causing the test method to still pass). Trace your code through its execution stack and make sure you're handling the error as you intended. 

All Answers

sfdcfoxsfdcfox

Based on my experience, it sounds like the insert u dml statement failed, but a try-catch block further up the execution stack caught the error (thus causing the test method to still pass). Trace your code through its execution stack and make sure you're handling the error as you intended. 

This was selected as the best answer
RajiiiRajiii

Thanks for your help,

I resolved this. I removed the try, catch statement from my class and again run the test method, it throws the error to my hand catch.