Happy Business Starts Here

How do I write a Test class for class which extends DefaultFieldValue Plugin for Cancellation Quote

Highlighted
Zuora Support

How do I write a Test class for class which extends DefaultFieldValue Plugin for Cancellation Quote

When trying to deploy "Default Values" class which extends "DefaultFieldValue Plugin"  to set the default values on Cancellation Quote, deployment failed as there need to be test classes created to allow the minimum code coverage which is 75%

 

 

KC for reference to populate default values on Cancellation Quote - https://knowledgecenter.zuora.com/CA_Commerce/I_Development_Resources/C_Component_Library/B_Global_C...






If you found my answer helpful, please give me a kudo ↑
Help others find answers faster by accepting my post as a solution √

2 REPLIES 2
Zuora Support

Re: How do I write a Test class for class which extends DefaultFieldValue Plugin for Cancellation Qu

This can be acheived with the below code snippet .

 

**Please note that this Test class can just be used a sample code for reference to meet use cases**

 

@isTest  //(SeeAllData=true)

public with sharing class Z_DefaultValueForCancelQuoteTest {

 

  @isTest

   private static void testNewSubNewAccount(){

   Account acc = makeAccount();

    Opportunity opp = makeOpportunity(acc);

    Contact c = makeContact(acc, 'United States');

    zqu__Quote__c quote = makeQuote(acc, opp);

   

   Z_DefaultFieldsForCancelQuote controller = new Z_DefaultFieldsForCancelQuote();

      PageReference createQuote = Page.zqu__ZQCancellation;

      createQuote.getParameters().put('oppId', opp.Id);

      createQuote.getParameters().put('quoteType', 'Cancellation');

      createQuote.getParameters().put('stepNumber', '2');

      Test.setCurrentPage(createQuote);

      //start test

      Test.startTest();

         controller.populateDefaultFieldValue(quote, new zqu.PropertyComponentController.ParentController());

      Test.stopTest();

   }

  

 /**

   * Create a test Contact

   */

  public static Contact makeContact(Account a, String country) {

    Contact c = new Contact();

    c.MailingCountry = country;

    c.AccountId = a.Id;

    c.LastName = 'Manojna';

    insert c;

    return c;

  }

   

 

  /**

  * Create a quote

  */

  public static zqu__Quote__c makeQuote(Account acc, Opportunity testOpp) {

    zqu__Quote__c testQuote = new zqu__Quote__c();

 

    testQuote.Name = 'Test Quote';

    testQuote.zqu__Account__c = acc.Id;

    testQuote.zqu__Opportunity__c = testOpp.Id;

    testQuote.zqu__Currency__c = 'USD';

    testQuote.zqu__Status__c = 'New';

    testQuote.zqu__SubscriptionTermStartDate__c = Date.today();

    testQuote.zqu__startdate__c =  Date.today();

    testQuote.zqu__InitialTerm__c = 12;

    testQuote.zqu__SubscriptionTermEndDate__c = Date.today().addMonths(12);

    testQuote.zqu__SubscriptionType__c ='New Subscription';

    testQuote.zqu__RenewalTerm__c = 12;

 

    insert testQuote;

    return testQuote;

  }

 

  /**

  * Create an Opportunity

  */

  public static Opportunity makeOpportunity(Account testAcc) {

 

    Opportunity testOpp = new Opportunity();

 

    testOpp.Name = 'Test Opportunity';

    testOpp.StageName = 'Test';

 

    testOpp.AccountId = testAcc.Id;

 

    insert testOpp;

    return testOpp;

  }

 

  /**

  * Create an Account

  */

  public static Account makeAccount() {

 

    Account testAcc = new Account();

    testAcc.Name = 'Test Account';

    testAcc.BillingState = 'California';

    testAcc.BillingStreet = 'Test';

    testAcc.BillingCity = 'San Mateo';

    testAcc.BillingCountry = 'United States';

    testAcc.BillingPostalCode = '94403';

 

    insert testAcc;

    return testAcc;

  }

 

}






If you found my answer helpful, please give me a kudo ↑
Help others find answers faster by accepting my post as a solution √

Honor Student

Re: How do I write a Test class for class which extends DefaultFieldValue Plugin for Cancellation Qu

Facing the same issue.

In my controller "super.populateDefaultFieldValue(record, pcc);" putting this under, "!Test.isRunningTest()" let's me bypass the error, but eventually the code coverage falls.

Need a proper fix to it.