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
SUMAN KUMARI 70SUMAN KUMARI 70 

how to refer pagereference method in my apex class

Hi I am new to apex and I have a requirement to write a test class for my apex class.
Please refer the class below:-
public with sharing class auth {
    
    public String lang { get; set; }
    public auth() {
        String langCode = ApexPages.currentPage().getParameters().get('language');
        if (langCode == 'EN' || langCode == 'ES') {
            lang = langCode;
        }
        else {
            lang = 'EN';
        }
    }
    
    public PageReference changeLanguage() {
        lang = (lang.equalsIgnoreCase('EN')) ? 'ES' : 'EN';
        return null;
    }
}

Please do help me out with this.
Thanks a lot in advance.
Best Answer chosen by SUMAN KUMARI 70
Abhishek BansalAbhishek Bansal
Hi Suman,

Please find the code below:
@isTest 
public class AuthTest 
{
	static testMethod void testMethod1() 
	{
		
		Test.StartTest(); 

			ApexPages.currentPage().getParameters().put('language', 'EN');
			auth  testCntl = new auth();
			testCntl.changeLanguage();
        
             ApexPages.currentPage().getParameters().put('language', 'TEST');
		     testCntl = new auth();

		Test.StopTest();
	}
}

Thanks,
Abhishek Bansal.

All Answers

Abhishek BansalAbhishek Bansal
Hi Suman,

PageReference is just a return type for the method. This can be called similar to the other methods of your class. Please find the test class for your controller class below:
@isTest 
public class AuthTest 
{
	static testMethod void testMethod1() 
	{
		
		Test.StartTest(); 

			ApexPages.currentPage().getParameters().put('language', 'EN');
			auth  testCntl = new auth();
			testCntl.changeLanguage();

		Test.StopTest();
	}
}

Please take care of the syntax errors and let me know if you face any issues with this.

Thanks,
Abhishek Bansal.
SUMAN KUMARI 70SUMAN KUMARI 70
Hi Abhishek,

Thanks a lot , the code is working for me but it is covering only 88% and is not covering the else part. Can you please let me know how to cover that.
 
Abhishek BansalAbhishek Bansal
Hi Suman,

Please find the code below:
@isTest 
public class AuthTest 
{
	static testMethod void testMethod1() 
	{
		
		Test.StartTest(); 

			ApexPages.currentPage().getParameters().put('language', 'EN');
			auth  testCntl = new auth();
			testCntl.changeLanguage();
        
             ApexPages.currentPage().getParameters().put('language', 'TEST');
		     testCntl = new auth();

		Test.StopTest();
	}
}

Thanks,
Abhishek Bansal.
This was selected as the best answer
SUMAN KUMARI 70SUMAN KUMARI 70
Thanks a lot Abhishek, 
Code is working fine