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
Debbie61Debbie61 

Need Test help for ApexPages.Message generated by Query Exception

I am at 63% for testing my class and the piece SFC is hi-lighting as not being tested is the section that catches a query exception and creates ApexPages.Message

 

 

catch (QueryException qe) { ApexPages.Message myMsg = new ApexPages.Message (ApexPages.severity.ERROR, 'Error retrieving login username'); ApexPages.addMessage(myMsg); return null; }

 

I am not sure why SFC does not think I am testing as my test method looks like this:

 

 

static testMethod void throwQE() { try{ ...some code here... } catch (QueryException qe) { ApexPages.Message myMsg = new ApexPages.Message (ApexPages.severity.ERROR, 'Error retrieving login username'); ApexPages.addMessage(myMsg); System.assert(true); }

 

I did verify that a query exception is being thrown.

 

Any thoughts? Appreciate the help.

Message Edited by Debbie61 on 03-31-2010 02:43 PM
MiddhaMiddha

2 things to check here:

 

1. QueryException is thrown (you have checked that)

2. An Exception is being thrown and not an error.

 

Have your tried  "catch (Exception e)" ?

Debbie61Debbie61

Thanks for the response. I tried your suggestion but it didn't make a difference. The code is still being hi-lighted as not being tested.

 

Looking in the Debug Log, it confirms that the path is being tested and an exception of type QueryException is being thrown.

 

8:14:20.675|SOQL_EXECUTE_BEGIN|[39,29]|Aggregations:0| select Id from User where Username = 'pickles' 8:14:20.679|SOQL_EXECUTE_END|[39,29]|Rows:0|Duration:4 8:14:20.679|EXCEPTION_THROWN|[39,29]|System.QueryException: List has no rows for assignment to SObject 8:14:20.679|METHOD_ENTRY|[50,17]|ApexPages.addMessage(ApexPages.Message) 8:14:20.679|VF_PAGE_MESSAGE|Error retrieving login username 8:14:20.679|METHOD_EXIT|[50,17]|addMessage(ApexPages.Message) 8:14:20.679|METHOD_ENTRY|[52,13]|System.assert(Boolean) 8:14:20.679|METHOD_EXIT|[52,13]|assert(Boolean)