2

As i am not aware of test classes i am bit confusing how to write test classes. Can any one one please suggest me how to write test class for below class:

 public class genericSendanEmailClass {
 public static void sendEmailToAuthor(Map<id,List<Question_Answer__c>> sendingMap){
   for(id aid : sendingMap.keySet()){
    // Step 1: Create a new Email
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setSubject('Quesitons updated');
    string Subject =  '<br/>';
    system.debug('+++++++'+ sendingMap.keySet().size());
    for(Question_Answer__c bk : sendingMap.get(aid)){

     system.debug('====aid==='+ aid);
        String Recordlink = URL.getSalesforceBaseUrl().toExternalForm()+'/'+bk.id;               
         Subject = Subject +'<br/>'+  Recordlink;
         Subject = Subject +'<br/>'+'Question: <br/>'+ bk.Question__c;
         Subject = Subject + '<br/>'+'Answer: <br/>'+ bk.Answer__c + '<br/>'+'<br/>'; 

    }       
     list<id> LegalTeamId = New list<id>();
    List<String> mailAddresses = new List<String>();
     list<GroupMember>  LegalTeam  = [SELECT UserOrGroupId FROM GroupMember WHERE GroupId = '00GU0000002TUao'];
    for(GroupMember u : LegalTeam){
      LegalTeamId.add(u.UserOrGroupId);
    }
    List<user> Lteam = [Select email FROM user WHERE id IN : LegalTeamId];
    for(user Lt : Lteam){
      mailAddresses.add(Lt.email);               
    }
     system.debug('======='+ mailAddresses);

    String body = 'Dear Leagal <br/> Please review the FCPA changes for ' + Subject ;
    mail.setHtmlBody(body);
    mail.setToAddresses(mailAddresses);           
    Messaging.sendEmail(new Messaging.Email[] {mail});
}   
}
}

1 Answer 1

2

Some general tips :

  1. Make sure all the methods are called atleast once.
  2. Make sure all conditional loops/logic is reachable (it HAS to execute the inner code, not just reach the loop, most common mistake).
  3. Make sure you populate all the required fields of the objects being used.

Call the function sendEmailToAuthor(Map<id,List<Question_Answer__c>> with proper data, which is a map of ID's with list of Question_Answer__c. I donot have access to that custom object, but you'll have to setup some data for it first.

for eg,

@istest(SeeAllData=True) //so you dont accidentally take live data
private class testnamesomething()
{
 Question_Answer__c qa_test = new Question_Answer__c();
 list<Question_Answer__c> qa_L_test = new Question_Answer__c();
 for(integer i=1;i<5;i++)
 {
  qa_test.name='something'+i;
  qa_test.quantity=12+i;
  qa_test.email='[email protected]+i';
  qa_L_test.add(qa_test);
 }     
 insert qa_L_test; 

 sendEmailToAuthor(qa_test.id,qa_L_test);
}

The above code (if you properly setup the required fields) should call most, if not all, the functions/methods in your actual code. Just make SURE that you ar properly setting up the required objects (like Question_Answer__c etc).

Hope this helps!

EDIT : WARNING, PLEASE DONOT BLINDLY COPY+PASTE THE CODE I WROTE

I can practically guarantee it will not work, since I have no idea what the Question_Answer__c object is.

Please code responsibly.

You must log in to answer this question.