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
iyappan kandasamy 4iyappan kandasamy 4 

Private method

This is the original code
-------------------------------
private String addressValidation(Contact con, boolean showApexErroMsg){
            boolean errorExist = false;
            String addrs_Error_Message = '';
            String errorMsgFullString = '';
   
            if (!AddressService.isValidationResultValid(con.Other_Address_Validation_Status__c, con.Other_Address_Validation_Timestamp__c))
            {
                addrs_Error_Message += ' Ship To Address, ';
                errorExist = true;
            }

        if (!AddressService.isValidationResultValid(con.Mailing_Address_Validation_Status__c, con.Mailing_Address_Validation_Timestamp__c))
        {
            addrs_Error_Message += ' Sold To Address, ';
            errorExist = true;
        }

        if (!AddressService.isValidationResultValid(con.Other_Address2_Validation_Status__c, con.Other_Address2_Validation_Timestamp__c))
        {
            addrs_Error_Message += ' Bill To Address ';
            errorExist = true;
        }

        if(String.isNotBlank(addrs_Error_Message)){
            errorMsgFullString = con.name+' has an non-validated '+addrs_Error_Message+'. Please go to the Contact page for '+con.name +' and validate the address.';
            if(showApexErroMsg)
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,errorMsgFullString));

        }
        system.debug('----addressValidation Result ----'+errorExist);
        return errorMsgFullString;
    }

kindly need test class for the above code.
Please do the needful as it is urgent
Thanks in advance
Raj VakatiRaj Vakati


You can do it like below two ways 

option 1 : Use TestVisible annotation


Use the TestVisible annotation to allow test methods to access private or protected members of another class outside the test class. These members include methods, member variables, and inner classes. This annotation enables a more permissive access level for running tests only. This annotation doesn’t change the visibility of members if accessed by non-test classes.

Change your method as below 
 
@TestVisible
private String addressValidation(Contact con, boolean showApexErroMsg){
            boolean errorExist = false;
            String addrs_Error_Message = '';
            String errorMsgFullString = '';
   
            if (!AddressService.isValidationResultValid(con.Other_Address_Validation_Status__c, con.Other_Address_Validation_Timestamp__c))
            {
                addrs_Error_Message += ' Ship To Address, ';
                errorExist = true;
            }

        if (!AddressService.isValidationResultValid(con.Mailing_Address_Validation_Status__c, con.Mailing_Address_Validation_Timestamp__c))
        {
            addrs_Error_Message += ' Sold To Address, ';
            errorExist = true;
        }

        if (!AddressService.isValidationResultValid(con.Other_Address2_Validation_Status__c, con.Other_Address2_Validation_Timestamp__c))
        {
            addrs_Error_Message += ' Bill To Address ';
            errorExist = true;
        }

        if(String.isNotBlank(addrs_Error_Message)){
            errorMsgFullString = con.name+' has an non-validated '+addrs_Error_Message+'. Please go to the Contact page for '+con.name +' and validate the address.';
            if(showApexErroMsg)
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,errorMsgFullString));

        }
        system.debug('----addressValidation Result ----'+errorExist);
        return errorMsgFullString;
    }
And now you can able to call this method in test class 



Option 2 : You can call the original method which calls this private method 
 
Raj VakatiRaj Vakati
Give me complete code pls 
Raj VakatiRaj Vakati
And its test class also
Raj VakatiRaj Vakati
Give me your class and test class .. 
Raj VakatiRaj Vakati
Do one thing 

Step1 : Change your method as below 
 

@TestVisible
private String addressValidation(Contact con, boolean showApexErroMsg){
            boolean errorExist = false;
            String addrs_Error_Message = '';
            String errorMsgFullString = '';
   
            if (!AddressService.isValidationResultValid(con.Other_Address_Validation_Status__c, con.Other_Address_Validation_Timestamp__c))
            {
                addrs_Error_Message += ' Ship To Address, ';
                errorExist = true;
            }

        if (!AddressService.isValidationResultValid(con.Mailing_Address_Validation_Status__c, con.Mailing_Address_Validation_Timestamp__c))
        {
            addrs_Error_Message += ' Sold To Address, ';
            errorExist = true;
        }

        if (!AddressService.isValidationResultValid(con.Other_Address2_Validation_Status__c, con.Other_Address2_Validation_Timestamp__c))
        {
            addrs_Error_Message += ' Bill To Address ';
            errorExist = true;
        }

        if(String.isNotBlank(addrs_Error_Message)){
            errorMsgFullString = con.name+' has an non-validated '+addrs_Error_Message+'. Please go to the Contact page for '+con.name +' and validate the address.';
            if(showApexErroMsg)
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,errorMsgFullString));

        }
        system.debug('----addressValidation Result ----'+errorExist);
        return errorMsgFullString;
    }

Invoke this method test class like below 

YOURCLASSNAME c= new YOURCLASSNAME();
c.addressValidation(CONTACTID,FALSE)
Raj VakatiRaj Vakati

You need to use ; at the end of the statement  addressValidation like below

c.addressValidation(CONTACTID,FALSE);
iyappan kandasamy 4iyappan kandasamy 4
Hi Raj
we have used your below 2 lines as :

"YOURCLASSNAME c= new YOURCLASSNAME();
c.addressValidation(CONTACTID,FALSE)"

In my test class i have created a class and then included the above 2 lines :
below is the code:(highlighted in bold )
-----------------------

public static testmethod void addressValidation_Test()
   {
       test.startTest();
       // order_header__c orderRec = [select Id, opportunity__c,PrePayment_Number__c,Bill_To_Company__c from order_header__c order by createddate desc limit 1];
         Contact con = [select Id, firstName, lastname from contact];
        PageReference pagRef = Page.CreateOrder;
        Test.setCurrentPage(pagRef); 
        
        ApexPages.StandardController sd = new ApexPages.StandardController(con);
        
        ApexPages.CurrentPage().getParameters().put('id', con.id);
       CreateNewOrderController controller = new CreateNewOrderController(sd);
       controller.addressValidation(ContactId, false);

   }

But getting error as :"variable does not exist " - ContactId


Kindly do the needful by giving your code please....Thanks in advance...
 
iyappan kandasamy 4iyappan kandasamy 4
Kindly do the needful...Its very urgent....Urgent help is needed....Thanks...