0

My Apex Class:

@AuraEnabled
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(){

    Test.startTest();
    QCT_Claim__c testClaim = QCT_NoteInfoCtrl.getClaimInfo(null);
    Test.stopTest();

    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.

3

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;
try
{
    QCT_NoteInfoCtrl.getClaimInfo(null);
}
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. – John Thompson Feb 20 '17 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 ^^' – Varun Kirtiman Abhishek Molla Feb 20 '17 at 16:31
  • @varun you cannot hit that line. As for the error, you have to create the corresponding data. – Adrian Larson Feb 20 '17 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 '17 at 18:34
  • @AdrianLarson okay, thanks for the explanation :). I will try to explain to the one who wrote the code. – Varun Kirtiman Abhishek Molla Feb 21 '17 at 6:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.