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());}
}
@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];
}
}