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
IT Admin DeloitteIT Admin Deloitte 

ApexPages.Message in Test Class

Hi All,

I've got this code in a class:
 
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO, 'Salvataggio Effettuato');
ApexPages.addMessage(myMsg);
return null;

I would like to cover this lines in my test class.

Any idea on how to do it?

Thankyou!
daniel_hdaniel_h
You can retrieve page messages in your test class. So after doing whatever you expect would cause the message to be added, you can check for it. This code is from https://developer.salesforce.com/forums/?id=906F00000008yOqIAI.
 
//cause message to be added to page
List<Apexpages.Message> msgs = ApexPages.getMessages();
boolean b = false;
for(Apexpages.Message msg:msgs){
    if (msg.getDetail().contains('Salvataggio Effettuato')) b = true;
}
system.assert(b);