+ Start a Discussion
Ishan.AroraIshan.Arora 

Fetch Apex Message in text Class from Vf page

i want to test a contoller class using Apex Message passed by the controller on the Vf page 

But i am not able to get any result as pageMessages comes null 

Any ideas how i can do it ?
 

static testMethod void test_Member(){
        
        //start the test process
        Test.startTest();
        
        Test.setCurrentPage(Page.BankManagement_Login);
        
        TransactionController obj = new TransactionController();
        
        ApexPages.Message[] pageMessages = ApexPages.getMessages();
        //List<Apexpages.Message> pageMessages = ApexPages.getMessages();  
        
        Boolean check=false;

        obj.username='aneesh@gmail.com';
        obj.password='12';
        obj.login();
        System.debug('hello1');
        
        for(ApexPages.Message msg:pageMessages){
          
            if (msg.getDetail().contains('Username Or Password is not valid')) {
                
                check = true;
                
            }else{
                
                check = false;
            }
        }
        
        system.assert(check);

        //Stop the test process
        Test.stopTest();
    }
Best Answer chosen by Ishan.Arora
GulshanRajGulshanRaj
Hi Ishan,

I just shuffle your code slightly. Please try this:
static testMethod void test_Member(){
        
        //start the test process
        Test.startTest();
        
        Test.setCurrentPage(Page.BankManagement_Login);
        
        TransactionController obj = new TransactionController();
        
        Boolean check=false;

        obj.username='aneesh@gmail.com';
        obj.password='12';
        obj.login();
        System.debug('hello1');

        ApexPages.Message[] pageMessages = ApexPages.getMessages();
        for(ApexPages.Message msg:pageMessages){
          
            if (msg.getDetail().contains('Username Or Password is not valid')) {
                
                check = true;
                
            }else{
                
                check = false;
            }
        }
        
        system.assert(check);

        //Stop the test process
        Test.stopTest();
    }

Thanks
Gulshan Raj
 

All Answers

GulshanRajGulshanRaj
Hi Ishan,

I just shuffle your code slightly. Please try this:
static testMethod void test_Member(){
        
        //start the test process
        Test.startTest();
        
        Test.setCurrentPage(Page.BankManagement_Login);
        
        TransactionController obj = new TransactionController();
        
        Boolean check=false;

        obj.username='aneesh@gmail.com';
        obj.password='12';
        obj.login();
        System.debug('hello1');

        ApexPages.Message[] pageMessages = ApexPages.getMessages();
        for(ApexPages.Message msg:pageMessages){
          
            if (msg.getDetail().contains('Username Or Password is not valid')) {
                
                check = true;
                
            }else{
                
                check = false;
            }
        }
        
        system.assert(check);

        //Stop the test process
        Test.stopTest();
    }

Thanks
Gulshan Raj
 
This was selected as the best answer
Ishan.AroraIshan.Arora
Thanks Gulshan Raj