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
rockForcerockForce 

test class code

I want the test class code. Can anyone suggest me with the test class code for the apex code.

 

 

public with sharing class TextMessage {

List<text__c> appTextMessage = [select text_message__c FROM text__c ];
Map<ID, Contact> contactMap = new Map<ID, Contact>([select id, firstname from contact]);
List<String> displayMessage = new List<String>();

public List<String> messageTemplate()
{  
  
  for (text__c textMessage : appTextMessage)
     {
    String text = textMessage.text_message__c;
  if(text !=null  ){
    Set<ID> keyId = contactMap.keySet();
    for(ID c :keyId){
      displayMessage.add(contactMap.get(c).firstname + ' ' + text + '\n');
      System.debug(displayMessage);
    }
  }
      }
    
  return displayMessage;
}

}

 Thanks in advance.

kritinkritin

@isTest

 

private class TestTextMessage {

 

static testmethod void TestmessageTemplate(){

test.startTest();

 

Account acc=new Account();

acc.Name = 'My Account';

 

insert acc;

 

Contact con=new Contact();

con.AccountId=acc.id;

con.firstname='Robin';

con.LastName='Hood';

insert con;

 

List<String> sResult=TextMessage.messageTemplate();

 

system.assertNotEqulas(sResult,null);

 

 

test.stopTest();

 

}

 

}

Ankit AroraAnkit Arora

Here you go :

 

public with sharing class TextMessage {

List<text__c> appTextMessage = [select text_message__c FROM text__c limit 1];
Map<ID, Contact> contactMap = new Map<ID, Contact>([select id, firstname from contact limit 1]);
List<String> displayMessage = new List<String>();

public List<String> messageTemplate()
{  
  
  for (text__c textMessage : appTextMessage)
     {
    String text = textMessage.name;
  if(text !=null  ){
    Set<ID> keyId = contactMap.keySet();
    for(ID c :keyId){
      displayMessage.add(contactMap.get(c).firstname + ' ' + text + '\n');
      System.debug(displayMessage);
    }
  }
      }
    
  return displayMessage;
}

@isTest

private static void myTest()
{
    text__c acc = new text__c(Name = 'test' , text_message__c = 'test') ;
    insert acc ;
    contact con = new contact (lastName = 'test') ;
    insert con ;
    TextMessage controller = new TextMessage() ;
    List<String> lst = controller.messageTemplate() ;
}

}

 

 

But let me tell you one thing, the logic implemented is not correct as you are using for in for and if there are 1K records in both object then you will get too many script statements for sure. For now the test will work. I will suggest you to refactor your code or just apply limits in the query.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Ankit AroraAnkit Arora

Cross post, but please do as I suggested. Else you will face error when there are too many records in organization.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Devendra NataniDevendra Natani
static testmethod void TestTextMessage()
{
   text__c t = new text__c(text_message__c ='test message',name='test');
   insert t;

   Contact c = new Contact(lastname = 'test lastname', firstname ='test firstname');
   insert c;

   Test.startTest();
    TextMessage tm = new TextMessage();
    system.assert(tm.messageTemplate.size() >=0 ); 
   Test.stopTest(); 
}

 Please let me know if there is any issue.

Ankit AroraAnkit Arora

Welcome Devendra, you are also included in the list of cross post :)

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page