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
Valentin F.Valentin F. 

Testing getter setter

I'm not able to test getters/setters neither understand how it works to test them.
public with sharing class ALSearchController 
{
public class Location{
        @AuraEnabled 
        public String icon{get;set;} 
        @AuraEnabled 
        public String title{get;set;} 
        @AuraEnabled 
        public GeoLocation location{get;set;} 
        @AuraEnabled 
        public String description{get;set;} 
        @AuraEnabled 
        public String id{get;set;} 
        @AuraEnabled 
        public String type{get;set;} 
        @AuraEnabled 
        public String status{get;set;} 
    }
}
I tried 
Location loc = new Location();
String icon = loc.icon;
icon = 'test';
System.assertEquals(icon, 'test');
but I got an error for the = sign : Unexpected token '=' even if the Location object belong to ALSearchController. 

 
Best Answer chosen by Valentin F.
Ajay K DubediAjay K Dubedi
Hi Valentin,
Please try the below code and let me know if this works for you. If still need modifications do let me know.

ApexClass: 
 
public with sharing class ALSearchController 
{
public class Location{
        @AuraEnabled 
        public String icon{get;set;} 
        @AuraEnabled 
        public String title{get;set;} 
        @AuraEnabled 
        public String description{get;set;} 
        @AuraEnabled 
        public String id{get;set;} 
        @AuraEnabled 
        public String type{get;set;} 
        @AuraEnabled 
        public String status{get;set;} 
    }
}

TestClass:
 
@isTest
private class ALSearchController _Test {
    @isTest
    static void getSet() {
        ALSearchController.Location loc = new ALSearchController.Location();
        String icon = loc.icon;
        icon = 'test';
        System.assertEquals(icon, 'test');
    }
}

Please go through the below link for inner class:
https://www.jitendrazaa.com/blog/java/nested-class-and-its-necessity-with-example/

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi

All Answers

Ravi Dutt SharmaRavi Dutt Sharma
Try testing like below:
Location loc = new Location();
loc.setIcon('test');
System.assertEquals(loc.getIcon(), 'test');

 
Ajay K DubediAjay K Dubedi
Hi Valentin,
Please try the below code and let me know if this works for you. If still need modifications do let me know.

ApexClass: 
 
public with sharing class ALSearchController 
{
public class Location{
        @AuraEnabled 
        public String icon{get;set;} 
        @AuraEnabled 
        public String title{get;set;} 
        @AuraEnabled 
        public String description{get;set;} 
        @AuraEnabled 
        public String id{get;set;} 
        @AuraEnabled 
        public String type{get;set;} 
        @AuraEnabled 
        public String status{get;set;} 
    }
}

TestClass:
 
@isTest
private class ALSearchController _Test {
    @isTest
    static void getSet() {
        ALSearchController.Location loc = new ALSearchController.Location();
        String icon = loc.icon;
        icon = 'test';
        System.assertEquals(icon, 'test');
    }
}

Please go through the below link for inner class:
https://www.jitendrazaa.com/blog/java/nested-class-and-its-necessity-with-example/

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
This was selected as the best answer
Valentin F.Valentin F.
Your way is not working Ravi. I think it needed the outside method.
Thank you for answering by the way.
Valentin F.Valentin F.
Hey Ajay,
Thank you for your answer and your link, this is working !