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
sohail najmisohail najmi 

unit test class for getter setter return method apex salesforce

I am new to salesforce and writing unit-test for ServiceParameters class.

I have tried to get coverage from AccountName method but faild.


Following are the Method:

public without sharing class ServiceParameters {
    
    public Id OrderId {get;set;}
    public Id PackageTypeId{ get; set; }
    public Id ShipperId{ get; set; }
    public Id ShipFromId{ get; set; }
---
---
--
    public Custom_Object_c ShipFromAddress {set;get;}
    public Custom_Object_c ShipperAddress {set;get;}
    public Order CurrentOrder {get;set;}
    public Account AccountDetail {set;get;}

    public String AccountName {
        get {
            return this.AccountDetail != null && String.isNotEmpty(this.AccountDetail.Name) ? this.AccountDetail.Name : '';
        }    
    }
    public String AccountPhoneNo {
        get {
            return this.AccountDetail != null && String.isNotEmpty(this.AccountDetail.Phone) ? this.AccountDetail.Phone : '';
        }    
    }

    public String ShipFromPhoneNo {
        get {
            return this.ShipFromAddress != null && String.isNotEmpty(this.ShipFromAddress.ShipGuruApp__Phone_Number__c) ? this.ShipFromAddress.ShipGuruApp__Phone_Number__c : '';
        }    
    }

    public String ShipperName {
        get {
            return this.ShipperAddress != null && String.isNotEmpty(this.ShipperAddress.Name) ? this.ShipperAddress.Name : '';
        }    
    }


.................. ...................................................

So far i have done.

@isTest
private  class ServiceParametersTest {
    @isTest static void testServiceParametersTest() {
             ServiceParameters SP = new ServiceParameters();.
            Account AccountDetail = new Account(Name = 'john');
       String Namess = SP.AccountName(AccountDetail.Name);    // showing error message (Method does not exist or incorrect signature: void AccountName(String) )



How to start the unit test ....
If anyone helps ... THanks in Advance...
Suraj Tripathi 47Suraj Tripathi 47

Hi Please find the solution.

@isTest
public class ServiceParametersTest{

public static testMethod void dataTest(){
Account a = new Account();
    a.Name = 'Test Account';
    a.Custom2__c = '000093';
    a.Business_Type__c = 'Consultant';
    insert a;

    Product2 p = new Product2();
    p.Name = ' Test Product ';
    p.Description='Test Product Entry 1';
    p.productCode = 'ABC';
    p.isActive = true;
    insert p;
    
    

    Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];
    
    PricebookEntry standardPrice = new PricebookEntry();
    standardPrice.Pricebook2Id = standardPb.Id;
    standardPrice.Product2Id = p.Id;
    standardPrice.UnitPrice = 1;
    standardPrice.IsActive = true;
    standardPrice.UseStandardPrice = false;
    insert standardPrice ;
    
    //Test Order Insert
    
    Order o = new Order();
    o.Name = 'Test Order ';
    o.Status = 'Draft';
    o.EffectiveDate = system.today();
    o.EndDate = system.today() + 4;
    o.AccountId = a.id;
    o.Pricebook2Id =  standardPb.Id ;
    
    insert o;
	
	
  Test.startTest();
  ServiceParameters.OrderId =o.id;
ServiceParameters.PackageTypeId ='1234';
ServiceParameters.ShipperId ='111';
ServiceParameters.ShipFromId ='111';

Custom_Object_c  obj=new Custom_Object_c ();
obj.Name='Test';//insert required field
insert obj;
 


ServiceParameters.ShipFromAddress   =obj;
ServiceParameters.ShipperAddress   ='111';
ServiceParameters.CurrentOrder  =o;


ServiceParameters.AccountDetail  =a;

String An=ServiceParameters.AccountName;
String Ap=ServiceParameters.AccountPhoneNo;
String As=ServiceParameters.ShipFromPhoneNo;
String Aship=ServiceParameters.ShipperName;
  
  Test.stopTest();
 
}
}

Please let me know it is working or not.

Please mark it as the Best If  it helps you

Thank You