0

I have written my first trigger and have attempted to write my test class but it is passing but it is saying it has zero coverage. Can anyone clear it up for me and tell me where im going wrong.

SharingClass

trigger ScrumTeamSharing on agf__ADM_Scrum_Team__c(after insert) {
if (trigger.isInsert) {
    agf__ADM_Scrum_Team__Share TeamShare;

    for(agf__ADM_Scrum_Team__c ADM : Trigger.new){
        TeamShare = new agf__ADM_Scrum_Team__Share();
        String IdTeam = ADM.Id;
        String assAcc = ADM.Account_ID__c;

        List<Account> alist = [SELECT Id, Name FROM Account WHERE Id=:assAcc];

        System.debug('a' + alist);

        List<Contact> clist = [SELECT Id, Name FROM Contact WHERE Contact.AccountId IN :alist];

        System.debug('c' + clist);

        // Set ID of record being shared
        TeamShare.ParentId = ADM.Id;

        // Set ID of user or group being granted access

        // Set Access Level
        TeamShare.AccessLevel = 'Read';

        //Parse contact Id into the sharing list
        Integer i = clist.size();
        while(i > 0) {
            TeamShare.UserOrGroupId = clist[i].Id;//add to the team reports
            i--;
                }
            }

                // Insert the sharing record and capture the save result. 
          // The false parameter allows for partial processing if multiple records passed 
          // into the operation.
          Database.SaveResult sr = Database.insert(TeamShare,false);

          // Process the save results.
          if(sr.isSuccess()){
             // Indicates success
             System.debug('Save Success');
          }
          else {
             // Get first save result error.
             Database.Error err = sr.getErrors()[0];

             // Check if the error is related to trival access level.
             // Access level must be more permissive than the object's default.
             // These sharing records are not required and thus an insert exception is acceptable. 
             if(err.getStatusCode() == StatusCode.FIELD_FILTER_VALIDATION_EXCEPTION  &&  
                      err.getMessage().contains('AccessLevel')){
                // Indicates success.
                System.debug('Success on mapping error');
             }
             else{
                // Indicates failure.
                System.debug('Failed to map error');
             }
        }   
    }
}

SharingClassTest

@isTest 
private class ScrumTeamSharingTest {

@isTest static void testTeamSharing() {
    //create contact for test
    List<Contact> users = new List<Contact>();
    Account a = new Account();
    a.Name = 'TestAcc';
    insert a;
    for (Integer j=0; j<5;j++) { //j number of contacts per account
        users.add(new Contact(firstname ='Test'+j,
                              lastname  ='Test'+j,
                              AccountId =a.Id));
    }
    insert users;
    agf__ADM_Scrum_Team__c t = new agf__ADM_Scrum_Team__c();
    t.Name='testTeam';
    t.Account_ID__c=a.Id;
    t.agf__Cloud__c='IT';
    insert t;
    System.debug('Inserted team & acct: ' + t.Name + ' ' + a.Name);
    }
}
3
  • 2
    Firstly, your trigger is not bulkified, And for your test class, try adding assert statements to see if the records are actually being inserted
    – Raul
    Nov 23, 2016 at 13:35
  • Your test class is not testing anything. It is just an execution script. So your test does not pass it just does not cause an error
    – Eric
    Nov 23, 2016 at 14:33
  • can you claify further like how would you make it test e.g. an example
    – Nebbyyy
    Nov 23, 2016 at 14:33

1 Answer 1

2

You have several things going on here. I recommend you go through the trailhead module on Apex which includes creating test classes. You'll first want to create a RunAs user that has the appropriate permissions to create your records. Add debug statements to your class to make certain your records are being created and that you have all the required fields.

First, WHO are you sharing these records with in your trigger? You need to create records for these users or Groups that you want to share the records with and the users who will create the records too!

You show in your code that you're inserting users (according to your variable naming) when in fact you're inserting contacts. Now if these contacts are community users, you need to also create the community users from the contacts that are associated with their owners. Refer to the [Object Reference][1] for more on the required fields and look at similar classes posted here that use those objects. All contacts require an email field in order to be inserted which is something I don't see in your test class.

You'll also want to use test.starttest() and test.stoptest() after which you assert your results. Your test class should look something like the following:

@isTest 
private class ScrumTeamSharingTest {

@isTest static void testTeamSharing() {

    //create your runAs user here by specifying their profile and role
    // query for the role and profile.id if needed
    User TstUsr = new User(required fields....);

    // the above user could be used to create all of your contacts and records and should be an admin


     System.RunAs(TstUsr){
     // you will also need to create other users and/or groups for your sharing
     // they can be used to create users and to share them with, so more than one user needs to be created

        System.RunAs(User1){
            //create User contacts for test
            List<Contact>userContacts = new List<Contact>();

            // Your org likely has other required fields for an Account beyond the name. Look for validation rules to see what they are.
            Account a = new Account();
            a.Name = 'TestAcc';
            insert a;

            // you may want to assign different owners depending on your intent to each of the users in your test
            // if these are the contacts being shared as they're not users
           ... create the accounts and contacts if you want them owned by User 1
           // can also run this without runAs if you assign the ownership explicity to the users        
           // insert the accounts

           for (Integer j=0; j<5;j++) { //j number of contacts per account
        userContacts.add(new Contact(firstname ='Test'+j,
                              lastname  ='Test'+j,
                              AccountId =a.Id));
           // a contact requires an email address. The above will not insert without errors                           
           }
           insert userContacts;

        } // end RunAs(User1) unless these are the contacts that will fire the trigger

        // if this is your team of users below, they need to be created before your test starts
        agf__ADM_Scrum_Team__c t = new agf__ADM_Scrum_Team__c();
        t.Name='testTeam';
        t.Account_ID__c=a.Id;
        t.agf__Cloud__c='IT';
        insert t;
        System.debug('Inserted team & acct: ' + t.Name + ' ' + a.Name);

        // create the list of real contacts that you intend to share
        test.starttest()

        // insert the contacts to be shared

        test.stoptest();

        // assert the ownership of the contacts has been changed to the new owners or groups

        } // end System.RunAs(TstUsr)
}

}


  [1]: https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_concepts.htm
0

You must log in to answer this question.

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