My Apex Class:

public static QCT_Claim__c getClaimInfo(Id claimId){
    return [select Id, Claim_Status__c, Requested_amount__c, Amount_type__c, Treated_amount__c, Gap__c, Accept_Gap__c 
            from QCT_Claim__c
            where Id = :claimId
            limit 1];
    return null;

My Test Class code for the return null:

    static testMethod void getClaimInfo_Test_Null(){

    QCT_Claim__c testClaim = QCT_NoteInfoCtrl.getClaimInfo(null);

    System.assertEquals(null, testClaim);

How to test for the return null? Doing it this way is getting me the error: list has no rows for assignment to sObject.


1 Answer 1


You can't hit that line since you have an unconditional return before it. If you want to return a query result you're not sure exists, assign to a list.

List<QCT_Claim__c> candidates = [/*query*/];
return candidates.isEmpty() ? null : candidates[0];

If you leave the code as is (which you shouldn't; it's broken) then an exception is your expected behavior. You will not be able to cover that last line since it's impossible to hit. But you can verify "expected" behavior:

QueryException expectedException;
catch (QueryException q)
    expectedException = q;

system.assertNotEquals(null, expectedException,
    'Record should not be successfully queried');
  • 1
    That's a ternary operator... "This operator acts as a short-hand for if-then-else statements. Feb 20, 2017 at 16:15
  • The problem is, i cannot modify the Apex Class code myself...i have been given the job to do only the test class...as for a ternary operator...thanks for the explanation @JohnThompson, but i knew about it ^^' Feb 20, 2017 at 16:31
  • @varun you cannot hit that line. As for the error, you have to create the corresponding data.
    – Adrian Larson
    Feb 20, 2017 at 18:33
  • That exception is the expected behavior as the code is written. It's poorly written code. You should push to fix it.
    – Adrian Larson
    Feb 20, 2017 at 18:34
  • @AdrianLarson okay, thanks for the explanation :). I will try to explain to the one who wrote the code. Feb 21, 2017 at 6:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .