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
DcoderDcoder 

Different test coverage in eclipse and salesforce app

Hi,

 

I have a class in sandbox and have it exported in a project in eclipse. When I run test for it in eclipse, it gives me 90% test coverage for it but when I run test in salesforce app front end it gives 75%. I have noticed that while running test in salesforce app it is not including the exception block as executed whereas in eclipse it does. I have put code in test method of that class to have the exception block executed.

 

to give you a brief about testmethod:

 

static testmethod void test1(){

 

try

{

.........test code for testing most part of the class

 

.......test code to generate exception so it can be caught in exception block below

 

}

 

catch(exception e)

{

....this exception block is empty in testmethod but has statements in class

}

 

} // end testmethod

 

 

Please help me out what can be possible cause of this issue and how it can be resolved.

 

Thanks!

 

mikefitzmikefitz

I don't think it's in your best interest to do a try catch within your test class because you always want your test class to blow up if you run into an exception.

 

About your real question, I know from my experience the IDE tests more robustly than just simply running a test within your browser but I would be interested also to hear from Salesforce the differences. Hopefully someone within inside knowledge will respond.

 

Good luck.

DcoderDcoder

Hi ,

 

Thanks for your reply!

 

I have put the try/catch block to test cover my exception block in main class. I didn't know of any other way to test cover it. If you are aware of anyone please share it.

 

Yes, I would like someone from salesforce developer support replying on it as it is bit tricky.

 

thanks!

mikefitzmikefitz

You could extend the Custom Exception class and Throw an Error.

I don't have time right away to follow back up if you have more questions but basically you set a variable that is only set to true in your test class and when it's true you throw an exception to get caught in your handler. It's pretty simple if you can get the syntax correct.

 

 

private boolean isTest = false;/*Throw Custom Exception*/
public class MyException extends Exception{} /*CustomException*/ 
public static DoSomething(){
if (isTest){throw new MyException('TEST ERROR HANDLER');}
...rest of code
}

static testmethod void test1(){
	DoSomething c1 = new DoSomething();
	c1.isTest = true;
	.........test code for testing most part of the class
} // end testmethod
 



 

 

 

Kirill_YunussovKirill_Yunussov

I have run into a weird issue as well, where all of a sudden, MyEclipse says that I have 100% coverage for a ton of different classes, while the test classes are in reality at 0 to 20%.   Some strange bug.  

 

When I test the classes through Salesforce user interface, then it shows true coverage.

 

Anyone knows what's going on?

uptime_andrewuptime_andrew

I have some subtle differences in coverage too.  If anyone has discovered anything regarding the reasoning, I'd be interested to hear.

communitycommunity

Go to Setup--> Develop --> Apex Test Execution --> View Test History --> Click 'Clear Test Results' 

 

That would clear all the dump that Salesforce stores in the cache.  Now 'Run ALL Tests' again. That would give exact coverage.