-7

I have written Apex Code and i am getting 92% code coverage. I want 100% code coverage.This is my apex class

/** * RA,25/11/2016 * apex class to handle Entity Subscriptions */ public class FeedPostHandler {

public static boolean follow(Map<Id, SObject> mapSObject, Set<Id> subscriberIds) {

    /**
     * prepare the list of EntitySubscriptions to insert
     */
    List<EntitySubscription> lstSubscription = new List<EntitySubscription>();

    // get unique ids for subscribers in case a group is passed as parameter
    Set<Id> allMemberIds = new Set<Id>();

    String grpKeyPrefix = CollaborationGroup.getsObjectType().getDescribe().getKeyPrefix();
    String userKeyPrefix = '005'; // no need to describe since user is a standard object
    // separate group ids from input params
    Set<Id> groupIds = new Set<Id>();
    for(Id sId :subscriberIds) {
        if(((String)sId).startsWith(grpKeyPrefix)) {
            groupIds.add(sId);
        }
        if(((String)sId).startsWith(userKeyPrefix)) {
            allMemberIds.add(sId);
        }
    }

    // query all collaboration group members
    for(CollaborationGroupMember member: [Select id, MemberId from CollaborationGroupMember 
        where CollaborationGroupId in :groupIds]) {

        allMemberIds.add(member.MemberId);
    }

    /**
     * exclude existing subscriptions before inserting
     */
    Set<String> setSubscriptions = new Set<String>();
    for(EntitySubscription es: [Select parentId, subscriberId from EntitySubscription 
        where parentId in :mapSObject.keyset() and subscriberId in :allMemberIds
        Limit 1000]) {

        setSubscriptions.add(es.parentId+''+es.subscriberId);
    }

    for(Id recordId: mapSObject.keyset()) {
        for(Id memberId: allMemberIds) {
            if(!setSubscriptions.contains(recordId+''+memberId)) {
                lstSubscription.add(new EntitySubscription(parentId=recordId, subscriberId=memberId));
            }
        }
    }

    if(lstSubscription.size() > 0){
        insert lstSubscription;
    }

    // final statement to return true if all goes well.
    return true;
}

}

These lines are not covered

         groupIds.add(sId);               
       allMemberIds.add(member.MemberId);

My test class is @isTest public with sharing class FeedPostHandlerTest { public static testMethod void TestFeedPostHandler() {

    //Test: global static EntitySubscription[] copyFollowers(SObject objectToCopyFrom, SObject[] objectsToFollow)
    User[] users = [select id from User where isactive = true and usertype = 'standard'];

    Account acct1 = new Account(name='acct1');
    insert acct1;
    Account acct2 = new Account(name='acct2');
    insert acct2;


    EntitySubscription es0 = new EntitySubscription(parentId=acct1.id,subscriberId=users[0].id);
    EntitySubscription es1 = new EntitySubscription(parentId=acct1.id,subscriberId=users[1].id);
    EntitySubscription es2 = new EntitySubscription(parentId=acct2.id,subscriberId=users[1].id);

    List<EntitySubscription> lstSubscription = new List<EntitySubscription>();
    lstSubscription.add(es0);
    lstSubscription.add(es1);
    lstSubscription.add(es2);
    insert lstSubscription;

    Set<Id> set1 = new Set<Id>();
    set1.add(acct1.id);
    set1.add(acct2.id);

    Map<Id, SObject> m = new Map<Id, SObject>();
    m.put(lstSubscription[0].id, lstSubscription[0]);
    m.put(lstSubscription[1].id, lstSubscription[1]);
    m.put(lstSubscription[2].id, lstSubscription[2]);

    FeedPostHandler.follow(m, set1);
}
public static testMethod void TestFeedPostHandlerf() {

    //Test: global static EntitySubscription[] copyFollowers(SObject objectToCopyFrom, SObject[] objectsToFollow)
    User[] users = [select id from User where isactive = true and usertype = 'standard'];

    Account acct1 = new Account(name='acct1');
    insert acct1;
    Account acct2 = new Account(name='acct2');
    insert acct2;
    EntitySubscription es0 = new EntitySubscription(parentId=acct1.id,subscriberId=users[0].id);
    insert es0;
    EntitySubscription es1 = new EntitySubscription(parentId=acct1.id,subscriberId=users[1].id);
    insert es1;
    EntitySubscription es2 = new EntitySubscription(parentId=acct2.id,subscriberId=users[1].id);
    insert es2;

    List<EntitySubscription> lstSubscription = new List<EntitySubscription>();
    lstSubscription.add(es0);
    lstSubscription.add(es1);
    lstSubscription.add(es2);

    Set<Id> set1 = new Set<Id>();
    set1.add(users[0].id);
    set1.add(users[1].id);
    System.assertNotEquals(set1,null);

    Map<Id, SObject> m = new Map<Id, SObject>();
    m.put(acct1.id, acct1);
    m.put(acct2.id, acct2);

    FeedPostHandler.follow(m, set1);
}

}

closed as off-topic by Eric, Adrian Larson, Vigneshwaran G, Brian Mansfield, Lance Shi Dec 7 '16 at 23:04

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on problems in code you've written must describe the specific problem and include valid code to reproduce it. For help writing short, self-contained syntactically-valid examples, see: SSCCE.org" – Eric, Adrian Larson, Vigneshwaran G, Brian Mansfield, Lance Shi
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Rahul, as many others have suggested, please add other relevant code directly to your question with an edit. We try to have all relevant parts of the information in the question, so that answers can focus on the solution alone. – Samuel De Rycke Dec 7 '16 at 14:58
1

When writing tests the goal is to write test methods per functional scenario that your code needs to support. Preparing your test you have to set up the correct data, variables and conditions so that your test execution will go into the specific logical scenario you are trying to test.

Looking at the code you're not getting covered you probably didn't set editmode to false. Beyond that you want to make sure the lstmastertmp variable will contain data.

0

In your test class prepare the test data first. Try to insert some records of (TValue_Field__c) object so when this (

List<TValue_Field__c> lstTvaueFlds = [Select Id, 
                                              Name, 
                                              Datatype__c,
                                              TValue_Field_API_Name__c
                                              from TValue_Field__c where Name != null
                                              order by sort_order__c];

) query get executes you get some data (records) and this (if(lstTvalueWrapper.size() > 0)) condition evaluates to true and your test covers those lines.

  • Hi Amjad, I have tried like this. i am able to cover this line but its not increasing the %level of code. – Sidharth Dec 7 '16 at 11:43
  • TValue_Field__c TValField1 = new TValue_Field__c(); TValField1.Name = 'Term'; TValField1.Datatype__c = 'Number'; TValField1.Sort_Order__c = 2; lstTValField.add(TValField1); insert lstTValField; TValField1 = [Select Id, Name, Datatype__c, TValue_Field_API_Name__c from TValue_Field__c where Name != null order by sort_order__c Limit 1]; – Sidharth Dec 7 '16 at 11:43
  • 1
    This is the only way to cover the lines. Try to insert some original case scenarios so that all those if get evaluates to true – Amjad khan Dec 7 '16 at 11:48
  • i have add original case scenario but not increasing the %. is there need of assert statements? – Sidharth Dec 7 '16 at 12:05
  • It is a good practice to write asserts but asserts not play any role in coverage – Amjad khan Dec 7 '16 at 12:07

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