+ Start a Discussion
alxhdezalxhdez 

how can I complete my test class? (68% covered)I'm trying the part of wrapper class

Class Controller:
public with sharing class controllerquoteVisual {
    
    public String title {get;set;}
    public Quote quote {get;set;}
    public List<quoteLineItem> qliList {get;set;}
    //public List<Product2> products {get; set;}
    
    //Capturar Nombre de familia y lista de productos
    public Map<String,List<ObjectWrapper>> mapFamily {get;set;}
    //public Map<String,List<QuoteLineItem>> mapFamilyqli {get;set;}
    //Captutar Nombre de familia y recuento de Productos que se mostrarán
    public Map<String, Integer> FamilyCountMap{get;set;}
	//Capturar Lista de nombres de familia de productos 
    public List<String> FamilyList {get;set;}
    //ProductList o products ya con los productos que se requieren mostrar
    
    public controllerquoteVisual(){
        
        Id id = ApexPages.currentPage().getParameters().get('id');
        
        //Obtener quote mediante el Id que arroja la visualforce desde quote
        quote = [SELECT Id, Name, TotalPrice, GrandTotal, ExpirationDate, AccountId, Total_Hours__c, OpportunityId, QuoteNumber   
                FROM quote WHERE Id=:id LIMIT 1];
        
        Opportunity opportunity = [SELECT Name FROM Opportunity WHERE Id=:quote.OpportunityId LIMIT 1];
        
        title = opportunity.Name.split('\\|')[0] + opportunity.Name.split('\\|')[1];
        
        Apexpages.currentPage().getHeaders().put('content-disposition', 'inline; filename='+title+ ' - ' +quote.QuoteNumber );
        
        //Obtener lista de quoteLineItems de la actual quote
        qliList = [SELECT Id, Product2Id, Quantity  
                    FROM quoteLineItem WHERE quoteId=:Id];
       
        ///////////////////////////////////////////////
        //Separar productos por tipo de familia 
        mapFamily = new Map<String, List<ObjectWrapper>>(); 
        FamilyCountMap = new Map<String, Integer>();
        FamilyList = new List<String>();
        
        List<quoteLineItem> finalqliList = new List<QuoteLineItem>();
        finalqliList = qliList;
        
        //Agrupar por nombre de familia y preparar los map
        for(QuoteLineItem qq: finalqliList){
            Product2 famObj = [SELECT Id, Name, Family, Description
                   FROM Product2 WHERE Id=:qq.Product2Id];
            if(famObj.Family == null){
                famObj.Family= 'Sin clasificación';
            }
            
            //List<QuoteLineItem> qq = qliList;
            List<ObjectWrapper> proList = new List<ObjectWrapper>();
            //Verificar si el map ya contiene el mismo nombre por familia
            if(mapFamily.containsKey(famObj.Family)){
            	//Recuperar lista de productos existentes
            	proList = mapFamily.get(famObj.Family);
            	//Meter el nuevo producto a la lista
                proList.add(new ObjectWrapper(qq, famObj));
                    
                mapFamily.put(famObj.Family, proList);
                    
                //Almacenar filas por nombre de familia
                FamilyCountMap.put(famObj.Family, proList.size());
            }
            else{
            	//crear nuevo map del nombre de familia //.fammily,name,etc
                //proList.add(famObj);
                proList.add(new ObjectWrapper(qq, famObj));
                mapFamily.put(famObj.Family, proList); 
                    
                //Almacenar filas por nombre de familia
                FamilyCountMap.put(famObj.Family, proList.size());
            }
        	FamilyList = new List<String>(mapFamily.keySet());
    	}
        
    }
    
    public class ObjectWrapper{
        //Campos QuoteLineItems
        //Public Id QliId{get;set;}
        Public Decimal Quantity{get;set;}
        
        //Campos Producto
        Public String Name{get;set;}
        Public String Family{get;set;}
        Public String Description{get;set;}
        
        public ObjectWrapper(QuoteLineItem ql, Product2 pr){
            //this.QliId = ql.Id;
            this.Quantity = ql.Quantity;
            
            this.Name = pr.Name;
            this.Family = pr.Family;
            this.Description = pr.Description;
        }  
    }
}

Test Class (68%, wrapper class is missing)
@isTest
public class TestControllerquoteVisual{
	@isTest
    static void testControllerVFP(){
        
        Pricebook2 pb = new Pricebook2(Name = 'Standard Price Book', 
                                       Description = 'Price Book Products', IsActive = true );
        insert pb;
        
        Product2 prod = new Product2(Name = 'Test Product', Description = 'Descripción Test', 
                                     Family = 'Salesforce Service Cloud', IsActive = true);
        insert prod;
        
        List<Pricebook2> standardPbList = [select id, name, isActive from Pricebook2 
                                           where IsStandard = true ];
 
    	List<PricebookEntry> listPriceBook = new List<PricebookEntry>();
     	for(Pricebook2 p : standardPbList ){
      		PricebookEntry pbe = New PricebookEntry ();
      		pbe = new PricebookEntry(Pricebook2Id = p.Id, Product2Id = prod.Id, 
                                     UnitPrice = 10000, IsActive = true, UseStandardPrice = false);
       		listPriceBook.add(pbe);
     	}
        insert listPriceBook;
        
        Opportunity opp = new Opportunity(Name = 'Test Opportunity', 
                                          StageName = 'Discovery', Product_Families__c='Salesforce Service Cloud' ,CloseDate = system.today());
        insert opp;
        
        Quote quttest = new Quote (Name = 'Quote Test' , OpportunityId = opp.id , Pricebook2Id = pb.id );
        insert quttest;
        
        List<QuoteLineItem> listval = new List<QuoteLineItem>();
        for(PricebookEntry pricebook : listPriceBook){
            QuoteLineItem qutlineitemtest = new QuoteLineItem ();
            qutlineitemtest = new QuoteLineItem(QuoteId = quttest.id, Quantity = 3.00,UnitPrice = 12, PricebookEntryId = pricebook.id);
            
            listval.add(qutlineitemtest);
        }
        insert listval;    
            
        QuoteLineItem qlitem = new QuoteLineItem(Quantity = 3.00, UnitPrice = 12 );
        
        
        Test.startTest();
            ApexPages.currentPage().getParameters().put('id', String.valueOf(quttest.Id));
            controllerquoteVisual controllerVfp= new controllerquoteVisual();
            //controllerVfp = new controllerquoteVisual();
        
            controllerquoteVisual.ObjectWrapper wrapper = new controllerquoteVisual.ObjectWrapper(qlitem, prod);
            
            wrapper.Quantity = 3.00;
                
            wrapper.Name = prod.Name;
            wrapper.Description = prod.Description;
            wrapper.Family = prod.Family;
        
        Test.stopTest();
        
    }

}

 
Best Answer chosen by alxhdez
Maharajan CMaharajan C
Hi alxhdez,

Please try the below test class you will get 97%:
 
@isTest
public class TestControllerquoteVisual{
    @isTest
    static void testControllerVFP(){
        
        Pricebook2 pb = new Pricebook2(Name = 'Standard Price Book', 
                                       Description = 'Price Book Products', IsActive = true );
        insert pb;
        
        Product2 prod = new Product2(Name = 'Test Product', Description = 'Descripción Test', 
                                     Family = 'Salesforce Service Cloud', IsActive = true);
        insert prod;
        
        List<Pricebook2> standardPbList = [select id, name, isActive from Pricebook2 
                                           where IsStandard = true ];
        
        Id standardPBId = Test.getStandardPricebookId();
        
        PricebookEntry pbe = New PricebookEntry (Pricebook2Id = standardPBId, Product2Id = prod.Id, 
                                                 UnitPrice = 10000, IsActive = true, UseStandardPrice = false);
        insert pbe;
        
        Opportunity opp = new Opportunity(Name = 'Test \\| Opportunity', 
                                          StageName = 'Discovery' ,CloseDate = system.today());
        insert opp;
        
        Quote quttest = new Quote (Name = 'Quote Test' , OpportunityId = opp.id , Pricebook2Id = standardPBId );
        insert quttest;
        
        QuoteLineItem qlitem = new QuoteLineItem(QuoteId = quttest.id, Quantity = 3.00,UnitPrice = 12, PricebookEntryId = pbe.id,Product2Id = prod.Id);
        insert qlitem;
        
        QuoteLineItem qlitem1 = new QuoteLineItem(QuoteId = quttest.id, Quantity = 5.00,UnitPrice = 10, PricebookEntryId = pbe.id,Product2Id = prod.Id);
        insert qlitem1;
        
        Test.startTest();
        ApexPages.currentPage().getParameters().put('id', String.valueOf(quttest.Id));
        controllerquoteVisual controllerVfp= new controllerquoteVisual();
        
        controllerquoteVisual.ObjectWrapper wrapper = new controllerquoteVisual.ObjectWrapper(qlitem, prod);
        
        wrapper.Quantity = 3.00;
        
        wrapper.Name = prod.Name;
        wrapper.Description = prod.Description;
        wrapper.Family = prod.Family;
        
        Test.stopTest();
        
    }
    
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi alxhdez,

Please try the below test class you will get 97%:
 
@isTest
public class TestControllerquoteVisual{
    @isTest
    static void testControllerVFP(){
        
        Pricebook2 pb = new Pricebook2(Name = 'Standard Price Book', 
                                       Description = 'Price Book Products', IsActive = true );
        insert pb;
        
        Product2 prod = new Product2(Name = 'Test Product', Description = 'Descripción Test', 
                                     Family = 'Salesforce Service Cloud', IsActive = true);
        insert prod;
        
        List<Pricebook2> standardPbList = [select id, name, isActive from Pricebook2 
                                           where IsStandard = true ];
        
        Id standardPBId = Test.getStandardPricebookId();
        
        PricebookEntry pbe = New PricebookEntry (Pricebook2Id = standardPBId, Product2Id = prod.Id, 
                                                 UnitPrice = 10000, IsActive = true, UseStandardPrice = false);
        insert pbe;
        
        Opportunity opp = new Opportunity(Name = 'Test \\| Opportunity', 
                                          StageName = 'Discovery' ,CloseDate = system.today());
        insert opp;
        
        Quote quttest = new Quote (Name = 'Quote Test' , OpportunityId = opp.id , Pricebook2Id = standardPBId );
        insert quttest;
        
        QuoteLineItem qlitem = new QuoteLineItem(QuoteId = quttest.id, Quantity = 3.00,UnitPrice = 12, PricebookEntryId = pbe.id,Product2Id = prod.Id);
        insert qlitem;
        
        QuoteLineItem qlitem1 = new QuoteLineItem(QuoteId = quttest.id, Quantity = 5.00,UnitPrice = 10, PricebookEntryId = pbe.id,Product2Id = prod.Id);
        insert qlitem1;
        
        Test.startTest();
        ApexPages.currentPage().getParameters().put('id', String.valueOf(quttest.Id));
        controllerquoteVisual controllerVfp= new controllerquoteVisual();
        
        controllerquoteVisual.ObjectWrapper wrapper = new controllerquoteVisual.ObjectWrapper(qlitem, prod);
        
        wrapper.Quantity = 3.00;
        
        wrapper.Name = prod.Name;
        wrapper.Description = prod.Description;
        wrapper.Family = prod.Family;
        
        Test.stopTest();
        
    }
    
}

Thanks,
Maharajan.C
This was selected as the best answer
fdssvds cfdsfcdsfdssvds cfdsfcds
This is exactly the similar kind of formula that I applied here on this page (https://campingshout.com/best-solar-charger-for-camping/) I would like to suggest it to all of the new salesforce members.
ilu sharmailu sharma
Such great content thanks for sharing with us. keep it up.
 
Very good information posted :  techjustify  (http://techjustify.com) ( http://techjustify.com (http://techjustify.com/) )is a blogging and technology website about science, mobiles, gaming, entertainment gadgets. from analysis of the brand new laptops, games, mobiles, shows, and movies to the latest news about privacy, tech, VPN, environmental policy, and labor.