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
Jerome Almejas 33Jerome Almejas 33 

How to write test class for the code below?

Can you please help me create test class for this code below?

public class CSS_NavigateReturnsPortal {
   
    @AuraEnabled
    public static String getURL(Id orderID) {
        Order tempOrder = [SELECT OrderNumber, CSS_Contact__r.Email FROM Order WHERE Id = :orderId];
        String strURL = '{"order_number":"' + tempOrder.OrderNumber + '","email":"' + tempOrder.CSS_Contact__r.Email + '"}';
        Blob tempBlob = Blob.valueOf(strURL);
        return EncodingUtil.base64Encode(tempBlob);
    }
   
}
Best Answer chosen by Jerome Almejas 33
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Jerome,

You can write the test class as below. This will cover 100% of the code.
 
@isTest 
public class CSS_NavigateReturnsPortalTest {  

   testMethod static void insertorder(){   
 Account a = new Account();
    a.Name = 'Test Account';
    insert a;
Contact c = new Contact();
       c.lastname='sample';
       c.Email='sai@sample.com';
    // Insert Product
    Product2 p = new Product2();
    p.Name = ' Test Product ';
    p.Description='Test Product Entry 1';
    p.productCode = 'ABC';
    p.isActive = true;
    insert p;
    

    Id pricebookId = Test.getStandardPricebookId();
    
    // Insert PricebookEntry

    PricebookEntry standardPrice = new PricebookEntry();
    standardPrice.Pricebook2Id = pricebookId;
    standardPrice.Product2Id = p.Id;
    standardPrice.UnitPrice = 1;
    standardPrice.IsActive = true;
    standardPrice.UseStandardPrice = false;
    insert standardPrice ;
    
    // Insert Order
    
    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 =  pricebookId ;
    
    insert o;
       
      String url= CSS_NavigateReturnsPortal.getURL(o.id);
 }
}

If this solution helps, Please mark it as best answer.

Thanks,
 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Jerome,

You can write the test class as below. This will cover 100% of the code.
 
@isTest 
public class CSS_NavigateReturnsPortalTest {  

   testMethod static void insertorder(){   
 Account a = new Account();
    a.Name = 'Test Account';
    insert a;
Contact c = new Contact();
       c.lastname='sample';
       c.Email='sai@sample.com';
    // Insert Product
    Product2 p = new Product2();
    p.Name = ' Test Product ';
    p.Description='Test Product Entry 1';
    p.productCode = 'ABC';
    p.isActive = true;
    insert p;
    

    Id pricebookId = Test.getStandardPricebookId();
    
    // Insert PricebookEntry

    PricebookEntry standardPrice = new PricebookEntry();
    standardPrice.Pricebook2Id = pricebookId;
    standardPrice.Product2Id = p.Id;
    standardPrice.UnitPrice = 1;
    standardPrice.IsActive = true;
    standardPrice.UseStandardPrice = false;
    insert standardPrice ;
    
    // Insert Order
    
    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 =  pricebookId ;
    
    insert o;
       
      String url= CSS_NavigateReturnsPortal.getURL(o.id);
 }
}

If this solution helps, Please mark it as best answer.

Thanks,
 
This was selected as the best answer
CharuDuttCharuDutt
Hii Jerome
Try Below Test Class 100% Code Coverage
@isTest 
public class CSS_NavigateReturnsPortalTest {  

   testMethod static void insertorder(){   
 Account a = new Account();
    a.Name = 'Test Account';
    insert a;
Contact c = new Contact();
       c.lastname='sample';
       c.Email='tes123@testsample.com';
   
    Product2 p = new Product2();
    p.Name = ' Test Product ';
    p.Description='Test Product Entry 1';
    p.productCode = 'ABC';
    p.isActive = true;
    insert p;
    

    Id pricebookId = Test.getStandardPricebookId();
    
    

    PricebookEntry standardPrice = new PricebookEntry();
    standardPrice.Pricebook2Id = pricebookId;
    standardPrice.Product2Id = p.Id;
    standardPrice.UnitPrice = 1;
    standardPrice.IsActive = true;
    standardPrice.UseStandardPrice = false;
    insert standardPrice ;
    
    
    
    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 =  pricebookId ;
    
    insert o;
       
    CSS_NavigateReturnsPortal.getURL(o.id);
 }
}
Please Mark It As Best Answer If It Helps
Thank You!