+ Start a Discussion
Pankaj IRTPankaj IRT 

Test Cases And Test Class

Hi Everyone ,

Please Suggest me best covarage logic for this

This is my class

public with sharing class CreateQuoteController {

    public List<Product2> packageProductsList = new List<Product2>();
    public List<ProductWrapper> searchResultsWithWrapper {get;set;}
    public List<QuoteLineItem> quoteItemsList {get;set;} 
    public Quote_Package__c quotePackage {get;set;}
    public CreateQuoteController (){
        //Get record id of Quote Package
        ID quotePackageId = ApexPages.currentPage().getParameters().get('id');
        //Read Package Details based on Record ID
        quotePackage = [Select Id, Name, Package__c, Physchem_Subpackage__c, Quote__c, Quote__r.CurrencyISOCode from Quote_Package__c where Id =:quotePackageId ];
        //Fetch All Products matching with Package
        packageProductsList = [Select Id, Name, ProductCode, Description, Package__c, Physchem_Subpackage__c , CIPAC__c,
            EC__c, OECD__c, OPPTS__c from Product2 where isActive = true and Package__c = :quotePackage.Package__c ];
        if(packageProductsList != null){
            //A list of ProductWrapper class that wraps Product2 class to enable checkbox selection
            searchResultsWithWrapper = new List<ProductWrapper>();
            if(quotePackage.Physchem_Subpackage__c != null && quotePackage.Physchem_Subpackage__c != ''){
                //Add only those products in Search Result where Product Physchem subpackage matches with that selected in Quote Package
                for(Product2 product:packageProductsList ){
                    String productPhysChemSubpackage = product.Physchem_Subpackage__c;
                    if(productPhysChemSubpackage  != null && productPhysChemSubpackage  != '' && productPhysChemSubpackage.contains(quotePackage.Physchem_Subpackage__c)){
                        searchResultsWithWrapper.add(new ProductWrapper(product));
                //Add All products in Search Result                
                for(Product2 product:packageProductsList ){
                    searchResultsWithWrapper.add(new ProductWrapper(product));

    public PageReference proceedToConfigure() 
        Map<String, String> productIdMap = new Map<String, String>();
        Map<String, Decimal> productIdPriceMap = new Map<String, Decimal>();
        Map<String, String> productIdPriceBookEntryIdMap = new Map<String, String>();
        for(ProductWrapper productWrapper : searchResultsWithWrapper){
                    productIdMap.put(productWrapper.product.Id, productWrapper.product.Id);
                //Fetch Standard Price book entries of Selected product to later add in Quote Line Item
                List<PriceBookEntry> priceBookEntries = [Select Id, Product2Id,Product2.CIPAC__c,
                Product2.EC__c, Product2.OECD__c,Product2.OPPTS__c, UnitPrice, CurrencyIsoCode
                from PriceBookEntry where Pricebook2.IsStandard = true 
                and Product2.isActive = true  and Product2Id in :productIdMap.keySet()
                and CurrencyIsoCode = :quotePackage.Quote__r.CurrencyISOCode];
                //Create a Map of Product Id and PriceBookEntry Id for later use
                if(priceBookEntries != null){
                    for(PriceBookEntry priceBookEntry : priceBookEntries ){
                        productIdPriceMap.put(priceBookEntry.Product2Id , priceBookEntry.UnitPrice);
                        productIdPriceBookEntryIdMap.put(priceBookEntry.Product2Id , priceBookEntry.Id);
        quoteItemsList = new List<QuoteLineItem>();
        for(ProductWrapper productWrapper : searchResultsWithWrapper){
                //For Every product selected, create a Quote Line Item to show for Configuration
                    QuoteLineItem quoteLineItem = new QuoteLineItem();
                    quoteLineItem.Product2Id = productWrapper.product.Id;
                    quoteLineItem.Quote_Package__c = quotePackage.Id;
                    quoteLineItem.QuoteId = quotePackage.Quote__c;
                    quoteLineItem.Quantity = 1;
                    quoteLineItem.Description = productWrapper.product.Description;
                    quoteLineItem.UnitPrice = productIdPriceMap.get(productWrapper.product.Id);
                    quoteLineItem.PricebookEntryId = productIdPriceBookEntryIdMap.get(productWrapper.product.Id);
                    quoteLineItem.Product2 = productWrapper.product;
        PageReference pageRef = new PageReference('/apex/configureQuoteLineItems');
        return pageRef;
    public PageReference addItemsToPackage() 
            PriceBook2 standardPriceBook = [Select Id from PriceBook2 where IsStandard = true];
            Quote quote = new Quote(Id=quoteItemsList[0].QuoteId);
            quote.Pricebook2Id = standardPriceBook.Id;
            update quote;
             //insert configured Quote line items, so that they appear against Quote Package as well as Quote
            insert quoteItemsList;
            return new PageReference('/'+quoteItemsList[0].Quote_Package__c);
        }catch(DMLException e){
            return null;
    public PageReference cancel() 
        return new PageReference('/'+quotePackage.Id);
    public PageReference backToSelectProducts() 
        return new PageReference('/apex/selectProductsPage?id='+quotePackage.Id);
     public class ProductWrapper{
         public Product2 product{get;set;}
         public boolean selected{get;set;}
         public ProductWrapper(Product2 p){
             product= p;
             selected = false;
Becka DBecka D
Hi Pankaj -

Can you share with us what you have written so far? We would love to help edit & fix what you have.
Pankaj IRTPankaj IRT
public class CreateQuoteControllerTest {
        private  static void testmethod1(){
            Quote_Package__c qp1 = new Quote_Package__c();
                qp1.Id = ApexPages.currentPage().getParameters().get('id');
                qp1.Name= qp1.Name;
                qp1.Physchem_Subpackage__c= qp1.Physchem_Subpackage__c;
            insert qp1;
            Package__c pk =new Package__c();
            insert pk;
            SBU__c sbu = new SBU__c();
            insert sbu;
            Opportunity opp = new Opportunity();
                opp.Id= opp.id;
                opp.Name= 'Test';
            insert opp;
            Quote q = new Quote();
                q.Pricebook2Id= q.Pricebook2Id;
                q.Contracting_SBU__c= sbu.Id;
            insert q;
            Pricebook2 pb = new Pricebook2();
            insert pb;
            PricebookEntry pbe = new PricebookEntry();
                pbe.UseStandardPrice = false;
                pbe.Id= pbe.Id;
            insert pbe;
            Product2 p2 = new Product2();
                p2.Package__c= pk.Id;
                p2.CurrencyIsoCode= p2.CurrencyIsoCode;
            insert p2;
            QuoteLineItem ql = new QuoteLineItem();
                ql.QuoteId= q.Id;
                ql.Quantity= 3.0;
            insert ql;
       CreateQuoteController cqc = new CreateQuoteController();
       PageReference pageRef = cqc.addItemsToPackage();
       PageReference pageRef1 = cqc.backToSelectProducts();
       PageReference pageRef2 = cqc.cancel();

Pankaj IRTPankaj IRT
above code give me 0% coverage after that I changed it and this new code give me 38% so plz help to recover it atleast 75%

new code is ....

public class CreateQuoteControllerTest {
        private  static void testmethod1(){
        Quote_Package__c qp = new Quote_Package__c(Name='Acute 6 pack',Package__c='a004B000000xLrVQAU',Physchem_Subpackage__c='MICRO EMULSION (ME)',Quote__c='0Q04B00000000u1SAA');
        insert qp;
        Quote_Package__c qpid = [SELECT Id from Quote_Package__c where Package__c='a004B000000xLrVQAU'];
        Product2 p2 = new Product2(Name='ORAL TOXICITY - RAT',ProductCode='401',CurrencyIsoCode ='USD',Package__c='a004B000000xLrVQAU');
            insert p2;
        Product2 p2id = [SELECT Id from Product2 where Package__c='a004B000000xLrVQAU'];
        List<Product2> lp2 = [select Id,Name,ProductCode,Physchem_Subpackage__c from Product2];
        PriceBook2 pb2 = new PriceBook2();
            pb2.Name= 'TestName';
        insert pb2;
        QuoteLineItem qli = new QuoteLineItem(QuoteId='0Q04B00000000s0SAA',PricebookEntryId='01u4B000000UI5nQAG',Quantity=1,UnitPrice=831,Product2Id =p2id.Id);
        insert qli;
            PageReference pageReference = Page.configureQuoteLineItems;

        CreateQuoteController cqc = new CreateQuoteController();