You need to sign in to do that
Don't have an account?
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.
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)" ?
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)