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
pramod pallewadpramod pallewad 

how to cover apexpages.hasmessage in test class

  public boolean hasMessages {
        get {
             return ApexPages.hasMessages();
        }
Ajay K DubediAjay K Dubedi
Hi Pramod,

Here is an example for you i created this one which return apexpages.hasMessages();

VF Page:
<apex:page standardController="Opportunity" extensions="Opportunity_Message">
    <apex:pageMessage strength="2" severity="INFO" summary="Closed Date Is Near" rendered="{!opportunity_date}"></apex:pageMessage>
    <apex:detail />
    <apex:pageMessage strength="2" severity="INFO" summary="Closed Date Is Near" rendered="{!opportunity_date}"></apex:pageMessage>
</apex:page>
Controller Class:
public class Opportunity_Message {
    public boolean opportunity_date { get; set; }
    public Opportunity_Message(ApexPages.StandardController controller) 
    {
        String test = controller.getid();
        Opportunity opp=[Select Id, CloseDate From Opportunity where Id=:test ];
        if(opp.CloseDate>=Date.today() && opp.CloseDate<=Date.today().addDays(5)) {
            opportunity_date = true;
        }
        else{
            opportunity_date = false;
        }
    }
     public boolean hasMessages {
        get {
             return ApexPages.hasMessages();
        }
}
Test_Class:
@isTest
public class Opportunity_Message_Test 
{
    public TestMethod Static Void Opportunity_Message_Method()
    {
        Opportunity op = New Opportunity();
        op.Name = 'Hello';
        op.StageName = 'Prospecting';
        op.CloseDate = Date.today();
        insert op;
        Opportunity opp = New Opportunity();
        opp.Name = 'Hello';
        opp.StageName = 'Prospecting';
        opp.CloseDate = Date.today().addDays(10);
        insert opp;
        Test.startTest();
        ApexPages.currentPage().getParameters().put('abc', String.valueOf(op.Id));
        ApexPages.StandardController sc = new  ApexPages.StandardController(op);
        Opportunity_Message st = new Opportunity_Message(sc);
        ApexPages.currentPage().getParameters().put('abc', String.valueOf(opp.Id));
        ApexPages.StandardController scc = new  ApexPages.StandardController(opp);
        Opportunity_Message st1 = new Opportunity_Message(scc);
        st1.hasMessages();
        System.assertEquals(True,apexpages.hasMessages());
        Test.stopTest();
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com