-1

Please help me test the method because I'm at a dead end. It does not return anything, but sets values ​​to two custom fields subject to several conditions.

Half of the method is covered by tests, and I just have to test the part from 'for (AccountContactRelation acr: con.AccountContactRelations)' to 'break;'. However i cannot incorrect change the values 'Contact.AccountContactRelations' or 'AccountContactRelation.isDirect', as they are 'not writeable'.

I also do not really understand, how to compare the output value of 'Relations__c', because this is not the result of the whole method, and verification needs to be done directly in the middle of the code.

For clarity, I added a screenshot of the coverage.

    public void showDirect(List<Contact> cons){ 
    try {
        for(Contact con: cons){             
            for (AccountContactRelation acr: con.AccountContactRelations) {         
                if (acr!=null) {

                    if (acr.isDirect) con.Relations__c = 'Direct';
                    else con.Relations__c = 'Indirect';

                } else System.debug('AccountContactRelation is null in showDirect()');

                //Necessary for a timely interruption of the cycle and the correct recording of roles on all contacts.
                if (con.Relations__c!=null && con.Relations__c!='') break;     
            }  
        }
    } catch(NullPointerException e) {System.debug('The following exception has occurred: ' + e.getMessage());}
} 

enter image description here

@isTest
public class RelatedContactsControllerTest {

static RelatedContactsController rcc = new RelatedContactsController();
static List<Contact> contacts = rcc.ContactsWithRoles;


@isTest static void testNullContacts(){
    contacts = null;
    rcc.addRoles(contacts);
    rcc.showDirect(contacts);
}


@isTest static void testCutContacts(){      
    contacts = [SELECT Id, Name, Account.Name, Title, Email, Phone FROM Contact LIMIT 1000]; 
}

}

2
  • 2
    Post your test class and code for test data creation, other wise it's very hard to help
    – sdandamud1
    Oct 10, 2019 at 21:08
  • @sdandamud1 added a test class, but this is unlikely to help much - I did it on the Trailhead guide, and there was only the most basic example.
    – anabiozzze
    Oct 11, 2019 at 7:07

1 Answer 1

0

I think you need to create two different Contact records - one which is directly related to the Account and another where the Contact is indirect in your test code. I would recommend using a TestSetup method so that you can use this data in multiple methods if needed.

You then need to update those contacts in your test class (I assume this runs when a Contact is updated not created) so that your trigger is iterated and preferably test for both positive and negative outcomes in your Test Class using Assertions. It won't matter that you directly update those fields, it will be sufficient if that particular code is referenced when the Contact is updated.

Good luck :)

TestSetup Documentation: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_testsetup_using.htm

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