function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Chris BettiniChris Bettini 

Needing help with Apex test class

This trigger creates an opposing relationship between contacts in a juntion object when an initial one is created.  eg. i create a link from Bob to Jon, and the trigger creates the link Jon to Bob.  It also creates a comma separated string of the contact ids on each of the respective contacts each time a junction record is created or deleted. I cant seem to get past 41% coverage and not having any luck with the string concatenation part in the test class.  
trigger teamLinkUpdate on TeamMemberLink__c (before insert, after insert, after delete) {
    
    if(Trigger.isAfter){
        if(Trigger.isInsert){
                try {
                    for (TeamMemberLink__c t : Trigger.new){
                        Contact c = [SELECT Id, TeamMemberLinks_List__c  FROM Contact WHERE Id = :t.ContactLink1__c];
                        
                        List<TeamMemberLink__c> l_co = [SELECT ContactLink2__c FROM TeamMemberLink__c WHERE ContactLink1__c = :c.Id];
                        String[] stringList = new String[0];
                        for(TeamMemberLink__c lstr : l_co) {
                            String strId = String.valueOf(lstr.ContactLink2__c);
                            stringList.add(strId);  
                        }
                       String result = String.join(stringList, ', ');                   
                       c.TeamMemberLinks_List__c  = result;
    
                        update c;
                    }
                } catch (Exception e) {
                    System.debug(e);
                }
            for (TeamMemberLink__c t : Trigger.new){
                        List<TeamMemberLink__c> tchk = [Select Id from TeamMemberLink__c where ContactLink2__c = :t.ContactLink1__c];
                        if(tchk.isEmpty()){
                        
                            List<TeamMemberLink__c> rel = new List<TeamMemberLink__c>();
                                TeamMemberLink__c tnew = new TeamMemberLink__c(ContactLink1__c = t.ContactLink2__c, ContactLink2__c = t.ContactLink1__c);
                            rel.add(tnew);
                            insert rel;
                           
                        }
                }
            }
        if(Trigger.isDelete){
                try {
                    for (TeamMemberLink__c t : Trigger.old){
                        Contact c = [SELECT Id, TeamMemberLinks_List__c  FROM Contact WHERE Id = :t.ContactLink1__c];
                        
                        List<TeamMemberLink__c> l_co = [SELECT ContactLink2__c FROM TeamMemberLink__c WHERE ContactLink1__c = :c.Id];
                        String[] stringList = new String[0];
                        for(TeamMemberLink__c lstr : l_co) {
                            String strId = String.valueOf(lstr.ContactLink2__c);
                            stringList.add(strId);  
                        }
                       String result = String.join(stringList, ', ');                   
                       c.TeamMemberLinks_List__c  = result;
    
                        update c;
                    }
                } catch (Exception e) {
                    System.debug(e);
                }
            
            }
    }
}
test class
@isTest
public class teamLinkUpdateTest {
	static testMethod void testUpdate()
    {	
              
        TeamMemberLink__c tm = new TeamMemberLink__c();
        tm.ContactLink1__c = '0034A00002lPY13QAG';
        tm.ContactLink2__c = '003G000002DpiytIAB';
        insert tm;           
              
             }
}


 
EranVEranV
1. Test classes don't have access to any data already stored in the database. You need to generate your own contacts in your test class from scratch, insert them and only then execute the test.
2. There are major design flaws in your trigger, which will almost surely fail your test class anyway (or worse, later in production). You need to remove all the SOQL queries you put inside loops (as they are guarenteed to exceed the 100 SOQL queries per transaction governor limit), and rediesign your entire code.
olaitan adesojiolaitan adesoji
I agree with EranV there. your code seems to be badly designed. Meanwhile the best way you can achieve code coverage is to call the Method you are trying to test and verifying that the changes you expect work or not. 
You also need to restructure that piece of code you are working o.