Earlier this is working with 3 but due to few modifications to this class, Now it is not working. What would be the problem if i change like

System.assertEquals(lAccrSaleConShare.size(), 2);
  • 2
    This is the whole point of a test method. According to the person that wrote the test, you should be getting 3. This means any code that calls this method after your changes, will get a different result. Is that a bad thing? Only you can answer that. – Nick Cook Sep 9 '16 at 8:42
  • Yeah that will not be good. But is is working fine in devfull org. I getting error in UAT. Same code in both orgs. Not even single line difference. – user24737 Sep 9 '16 at 9:18
  • 1
    This is why testing magic numbers is brittle, and also why using assertion messages is very important. – Adrian Larson Sep 9 '16 at 11:34
  • How can i query list size which is in test class? I just want to check how many records are fetching the list. i want to checkout this: lAccrSaleConShare = [SELECT Id, ParentId FROM Accreditation_Sales_Contact__Share WHERE ParentId =: accrSaleCon.Id]; – user24737 Sep 11 '16 at 3:26

Don't follow the example set by this code.

  • The syntax is system.assertEquals(expected_value, actual_value, message).
    • In the assertions above, expected_value and actual_value are flipped.
    • While the message argument is optional, it is highly recommended and might have helped you here.
  • When using magic numbers in your unit tests, you can give them more meaning by making them constants. Most of my test classes have a line static final Integer RECORD_COUNT = Limits.getLimitQueries() + 1, which you could use in place of 3.
  • You aren't even testing the setManualShareAfterInsert method. You're testing a trigger somewhere, which you haven't shared with us.
  • Same goes for the setManualShareAfterUpdate method.

I can't tell you how to get your unit test working. I can't even tell if your code should work as advertised. But you might have some luck by working on Separation Of Concerns. You could break out a few discrete tasks and verify they work as expected, then build on that. The key to unit testing is to test small units of code.

I have found trying to build on top of code that tries to do dozens of things in one method is just not tenable. Here's a start for how I would refactor:

public static Set<Id> pluckIds(String field, List<SObject> records)
    Set<Id> ids = new Set<Id>();
    for (SObject record : records)
    return ids;
public static Map<Id, Accreditation__c> getAccreditations(List<Accreditation_Sales_Contact__c>  records)
    return new Map<Id, Accreditation>([
        SELECT Id, Name, Account__r.Partner_Community_User_Role_ID__c
        FROM Accreditation__c
        WHERE Id IN :pluckIds('Accreditation__c', records)
    // if you query the Account__r.Partner_Community_User_Role_ID__c field,
    // you don't even need to query Account at all, just reference this way

Anyway, that's probably not the answer you were looking for, but hopefully it will help you to some extent.

  • Even if i keep that line static final Integer RECORD_COUNT = Limits.getLimitQueries() + 1 it firing error : actual 2 expected 101. How to check this query list to check how many records are in list. I am not able to query as it is sharing Object and which id i need to pass? – user24737 Sep 9 '16 at 13:57
  • Again, keep in mind that expected_value and actual_value are reversed. It's expecting 3. I don't know why it would create 3 shares per record. It looks like it should only be creating one. And your unit test doesn't touch this method directly, only the trigger... – Adrian Larson Sep 9 '16 at 14:01

Your Answer

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