+ Start a Discussion
Marry SteinMarry Stein 

Help for testclass

Hey guys, 
i am going crazy on this testclass. In my opionion it should be very simple but it does not work. 

This is the necessary part of my class. 
public class CreateCustomOpporutnityQuote {
    // create necessary attributes 
    public Opportunity oppo {get; set;}
    public Id opportunityId {get; set;}
    public PageReference pdfQuote;

    public ID parentId {get; set;}
    // create sections  
    public List <OpportunityLineItem> sectionFlat {get;set;}
    public List <OpportunityLineItem> sectionEmployerBranding {get;set;}
    public List <OpportunityLineItem> sectionPerformance {get;set;}
    public List <OpportunityLineItem> sectionService {get;set;}
    public List <OpportunityLineItem> sectionSonstiges {get;set;}
    // variables for customization 
    public String subject {get; set;}
    public String firsttext {get; set;}
    public String secondtext {get; set;}
    public String image {get; set;}
    public Date qDate {get; set;} // Enddatum für Angebot
    // pagebreaks between sections 
    public Boolean pageBreak {get; set;}
    public Boolean pageBreak1 {get; set;}
    public Boolean pageBreak2 {get; set;}
    public Boolean pageBreak3 {get; set;}

    public CreateCustomOpporutnityQuote(ApexPages.StandardController stdController ){
        this.oppo = (Opportunity)stdController.getRecord();
        this.opportunityId = ApexPages.currentPage().getParameters().get('Id');
    }
    //create pdf sections related to prodcut quote category
    public void createPdfSections(){
        List<OpportunityLineItem> oliList = [Select Id, Name, ListPrice, TotalPrice,  Quantity,  Product2.CustomQuoteCategory__c,      
                                            ON_Produktbeschreibung_detailliert__c, Produkt__c, CustomQuoteQuantity__c,Custom_Quote_List_Price__c,
                                            Product2.family, Product2.domain__c, Product2.size__c, Product2.Subname__c,SumListPrice__c
                                            FROM OpportunityLineItem
                                            WHERE OpportunityId =:opportunityId Limit 50];
        for(OpportunityLineItem oli : oliList){
            switch on oli.Product2.CustomQuoteCategory__c { // Custom
                when 'BAS' {
                    sectionFlat.add(oli);
                    System.debug('Wenn Category gleich BAS, dann Abschnitt Basis');
                }
                when 'EMP' {
                    sectionEmployerBranding.add(oli);  
                    System.debug('Wenn Category EMP, dann Abschnitt Employer Brand');
                }
                when 'PER'{
                    sectionPerformance.add(oli);  
                    System.debug('Wenn Category PER, dann Abschnitt Performane');
                }
                when 'SER'{
                    sectionService.add(oli);  
                    System.debug('Wenn Category SER, dann Abschnitt Service');
                }
                when else {
                    sectionSonstiges.add(oli);
                    System.debug('Wenn nicht zugeordnet, dann Abschnitt Sonstiges');
                }
            }
        }
    }
so i created the following / testmethode  (i think the data setup is not necassary at this point) 
 
@isTest static void testGetOliProd(){

            Opportunity opp = [SELECT Id FROM Opportunity LIMIT 1]; 
            ApexPages.StandardController sc = new ApexPages.StandardController(opp);
            CreateCustomOpporutnityQuote tstCtrl = new CreateCustomOpporutnityQuote(sc);
            tstCtrl.opportunityId = opp.Id;
            List<OpportunityLineItem> testList = [Select Id, OpportunityId, Product2.CustomQuoteCategory__c FROM OpportunityLineItem WHERE OpportunityId =: opp.id];
            System.assertEquals(testList.size(), 5);
            List<OpportunityLineItem> bas = new List <OpportunityLineItem>();
            List<OpportunityLineItem> emp = new List <OpportunityLineItem>();
            List<OpportunityLineItem> perv = new List <OpportunityLineItem>();
            List<OpportunityLineItem> serv = new List <OpportunityLineItem>();
            List<OpportunityLineItem> sons = new List <OpportunityLineItem>();
            Test.startTest();
            tstCtrl.createPdfSections();
            bas = tstCtrl.sectionFlat; // at this point i get an null pointer exception and i don't know why :( 
       
            Test.stopTest();
        }

I would appreciate some advices . Thanks for your help and your time guys ! 
 
 
Soyab HussainSoyab Hussain
Hi Marry,
First, you need to insert opportunity records in the code than your test class will be covered.
 
@testSetup static void methodName() {
      Opportunity testOpportunity = new Opportunity(
            StageName = 'Sourcing Demand',
            CloseDate = '2016-01-01',
            Account = '0015000000tVJRS',
            Name = 'Test Opportunity Triggers',
            TimeZoneSidKey = 'America/Denver',
            LocaleSidKey = 'en_US',
            EmailEncodingKey  = 'UTF-8',
            LanguageLocaleKey = 'en_US'
        );
        insert testOpportunity;
}

Regards,
Soyab
Marry SteinMarry Stein
Hi Soyab,

i did that. I have created all necessary objects in my testSetup method :) 
Soyab HussainSoyab Hussain
So can you please share your all code, 
Then i can help you
Marry SteinMarry Stein
Here we go :) 
 
@isTest
public class TestCreateCustomOpportunityQuote {
    
    @isTest private static void TestCreateCustomOpportunityQuote(){
   
         Opportunity opp = new Opportunity ();
         opp.name = 'test';
         opp.StageName = '40 - Demo Termin vereinbart';
         opp.CloseDate = System.today();
         insert opp;
        
         PageReference testPage = Page.CustomizeOpportunityQuote;
         testPage.getParameters().put('id', opp.id);
         Test.setCurrentPage(testPage);
         ApexPages.StandardController sc = new ApexPages.StandardController(opp);
         CreateCustomOpporutnityQuote tstCtrl = new CreateCustomOpporutnityQuote(sc);
         tstCtrl.opportunityId = testPage.getParameters().get(opp.id);
        
         tstCtrl.image = 'test';
         tstCtrl.secondtext = 'test';
         tstCtrl.subject = 'test';
         tstCtrl.firsttext = 'test';
         tstCtrl.pageBreak = false;
         tstCtrl.pageBreak1 = false;
         tstCtrl.pageBreak2 = false;
         tstCtrl.pageBreak3 = false;
         tstCtrl.qDate = System.today();
     } 
        
        @testSetup static void Setup(){
            Id pricebookId = Test.getStandardPricebookId();
            
            Product2 pro = new Product2();
            pro.name = 'test';
            pro.Produktbeschreibung_detailliert__c = 'test';
            pro.Family = 'Anzeigen Flat'; 
            pro.IsActive = true;
            insert pro;
            
            Product2 pro1 = new Product2();
            pro1.name = 'test1';
            pro1.Produktbeschreibung_detailliert__c = 'test';
            pro1.Family = 'Exklusiv Logo'; 
            pro1.IsActive = true;
            insert pro1;
            
            Product2 pro2 = new Product2();
            pro2.name = 'test2';
            pro2.Produktbeschreibung_detailliert__c = 'test';
            pro2.Family = 'Top Job'; 
            pro2.IsActive = true;
            insert pro2;
            
            Product2 pro3 = new Product2();
            pro3.name = 'test3';
            pro3.Produktbeschreibung_detailliert__c = 'test';
            pro3.Family = 'Dienstleistungen'; 
            pro3.IsActive = true;
            insert pro3;
            
            Product2 pro4 = new Product2();
            pro4.name = 'test4';
            pro4.Produktbeschreibung_detailliert__c = 'test';
            pro4.Family = 'Sonstiges'; 
            pro4.IsActive = true;
            insert pro4;
                        
            PricebookEntry pEntry = new PricebookEntry (); 
            pEntry.Product2Id = pro.id;
            pEntry.UseStandardPrice = false;
            pEntry.UnitPrice = 600; 
            pEntry.Pricebook2Id = pricebookId;
            pEntry.IsActive = true;
            insert pEntry; 
            
            PricebookEntry pEntry1 = new PricebookEntry (); 
            pEntry1.Product2Id = pro1.id;
            pEntry1.UseStandardPrice = false;
            pEntry1.UnitPrice = 600; 
            pEntry1.Pricebook2Id = pricebookId;
            pEntry1.IsActive = true;
            insert pEntry1;
            
            PricebookEntry pEntry2 = new PricebookEntry (); 
            pEntry2.Product2Id = pro2.id;
            pEntry2.UseStandardPrice = false;
            pEntry2.UnitPrice = 600; 
            pEntry2.Pricebook2Id = pricebookId;
            pEntry2.IsActive = true;
            insert pEntry2;
            
            PricebookEntry pEntry3 = new PricebookEntry (); 
            pEntry3.Product2Id = pro3.id;
            pEntry3.UseStandardPrice = false;
            pEntry3.UnitPrice = 600; 
            pEntry3.Pricebook2Id = pricebookId;
            pEntry3.IsActive = true;
            insert pEntry3;
            
            PricebookEntry pEntry4 = new PricebookEntry (); 
            pEntry4.Product2Id = pro4.id;
            pEntry4.UseStandardPrice = false;
            pEntry4.UnitPrice = 600; 
            pEntry4.Pricebook2Id = pricebookId;
            pEntry4.IsActive = true;
            insert pEntry4;
        
            Account acc = new Account();
            acc.name = 'testAcc';
            acc.BillingStreet = 'testStreet';
            acc.BillingCity = 'München';
            acc.BillingPostalCode = '80331';
            acc.BillingCountry = 'Deutschland';
            acc.RecordTypeId = '012D0000000BZ8L';
            insert acc;
            
            Account acc1 = new Account();
            acc1.name = 'testAcc1';
            acc1.BillingStreet = 'testStreet';
            acc1.BillingCity = 'Brüssel';
            acc1.BillingPostalCode = '020203';
            acc1.BillingCountry = 'Belgien';
            acc1.ON_UID_Nummer__c = 'U2323232';
            acc1.RecordTypeId = '012D0000000BZ8L';
            insert acc1;
            
            // Formelfeld --> query
            Account tstAcc = [Select ON_Steuerart__c FROM Account WHERE name = 'testAcc1'];
            system.assertEquals('Nicht steuerbar (EU mit UStID-Nr.)', tstAcc.ON_Steuerart__c );

            
            Contact con = new Contact();
            con.AccountId = acc.id;
            con.FirstName = 'böser';
            con.LastName = 'Moritz';
            con.Salutation = 'Herr';
            con.Briefanrede__c = 'Sehr geehrter';
            con.Entscheider__c = true;
            insert con;
            
            Contact con1 = new Contact();
            con1.AccountId = acc1.id;
            con1.FirstName = 'netter';
            con1.LastName = 'Moritz';
            con1.Salutation = 'Herr';
            con1.Briefanrede__c = 'Sehr geehrter';
            con1.Entscheider__c = true;
            insert con1;
            
            
            Opportunity opp = new Opportunity ();
            opp.AccountId = acc.Id; 
            opp.Name = 'test';
            opp.StageName = '40 - Demo Termin vereinbart';
            opp.Override_Region__c = 'München';
            opp.CloseDate = System.today();
            opp.z_Entscheider__c = con.id;
            opp.z_Vertragsbeginn__c = system.today();
            opp.z_Enddatum__c = system.today().addDays(360);
            opp.Type = 'Bestandskundengeschäft'; 
            opp.ON_Kein_Rabatt_ausweisen__c = false; 
            insert opp;
            
            system.assertEquals('test', opp.Name);

            
            Opportunity opp1 = new Opportunity ();
            opp1.AccountId = acc.Id; 
            opp1.Name = 'test1';
            opp1.StageName = '40 - Demo Termin vereinbart';
            opp1.Override_Region__c = 'München';
            opp1.CloseDate = System.today();
            opp1.z_Entscheider__c = con.id;
            opp1.z_Vertragsbeginn__c = system.today();
            opp1.z_Enddatum__c = system.today().addDays(360);
            opp1.Type = 'Bestandskundengeschäft'; 
            opp1.ON_Kein_Rabatt_ausweisen__c = true; 
            insert opp1;
            
            Opportunity opp2 = new Opportunity ();
            opp2.AccountId = acc1.Id; 
            opp2.Name = 'test2';
            opp2.StageName = '40 - Demo Termin vereinbart';
            opp2.Override_Region__c = 'München';
            opp2.CloseDate = System.today();
            opp2.z_Entscheider__c = con1.id;
            opp2.z_Vertragsbeginn__c = system.today();
            opp2.z_Enddatum__c = system.today().addDays(360);
            opp2.Type = 'Bestandskundengeschäft'; 
            opp2.ON_Kein_Rabatt_ausweisen__c = true; 
            insert opp2;
            
            OpportunityLineItem oli = new OpportunityLineItem ();
            oli.TotalPrice = 500;
            oli.Produkt__c = 'test';
            oli.Quantity = 4;
            oli.OpportunityId = opp.Id;
            oli.Product2Id = pro.id;
            oli.ON_Produktbeschreibung_detailliert__c = 'test';                            
            oli.PricebookEntryId = pEntry.id;
            insert oli;
            
            OpportunityLineItem oli1 = new OpportunityLineItem ();
            oli1.TotalPrice = 600;
            oli1.Produkt__c = 'test1';
            oli1.Quantity = 4;
            oli1.OpportunityId = opp.Id;
            oli1.Product2Id = pro1.id;
            oli1.ON_Produktbeschreibung_detailliert__c = 'test';                            
            oli1.PricebookEntryId = pEntry1.id;
            insert oli1;
            
            OpportunityLineItem oli2 = new OpportunityLineItem ();
            oli2.TotalPrice = 1000;
            oli2.Produkt__c = 'test2'; 
            oli2.Quantity = 4;
            oli2.OpportunityId = opp.Id;
            oli2.Product2Id = pro2.id;
            oli2.ON_Produktbeschreibung_detailliert__c = 'test';                            
            oli2.PricebookEntryId = pEntry2.id;
            insert oli2;
            
            OpportunityLineItem oli3 = new OpportunityLineItem ();
            oli3.TotalPrice = 1000;
            oli3.Produkt__c = 'test2'; 
            oli3.Quantity = 4;
            oli3.OpportunityId = opp.Id;
            oli3.Product2Id = pro3.id;
            oli3.ON_Produktbeschreibung_detailliert__c = 'test';                            
            oli3.PricebookEntryId = pEntry3.id;
            insert oli3;
            
            OpportunityLineItem oli4 = new OpportunityLineItem ();
            oli4.TotalPrice = 1000;
            oli4.Produkt__c = 'test2'; 
            oli4.Quantity = 4;
            oli4.OpportunityId = opp.Id;
            oli4.Product2Id = pro4.id;
            oli4.ON_Produktbeschreibung_detailliert__c = 'test';                            
            oli4.PricebookEntryId = pEntry4.id;
            insert oli4;
    
        }
    
        // positiver Test
        @isTest static void testGetOliFlat(){

            Opportunity opp = [SELECT Id FROM Opportunity LIMIT 1]; 
            ApexPages.StandardController sc = new ApexPages.StandardController(opp);
            CreateCustomOpporutnityQuote tstCtrl = new CreateCustomOpporutnityQuote(sc);
            tstCtrl.opportunityId = opp.Id;
            List<OpportunityLineItem> testList = [Select Id, OpportunityId, Product2.CustomQuoteCategory__c FROM OpportunityLineItem WHERE OpportunityId =: opp.id];
            System.assertEquals(testList.size(), 5);
            List<OpportunityLineItem> bas = new List <OpportunityLineItem>();
            List<OpportunityLineItem> emp = new List <OpportunityLineItem>();
            List<OpportunityLineItem> perv = new List <OpportunityLineItem>();
            List<OpportunityLineItem> serv = new List <OpportunityLineItem>();
            List<OpportunityLineItem> sons = new List <OpportunityLineItem>();
            Test.startTest();
            tstCtrl.createPdfSections();
            bas = tstCtrl.sectionFlat;
            //emp = tstCtrl.sectionEmployerBranding;
            //perv = tstCtrl.sectionPerformance;
            // serv = tstCtrl.sectionService;
            // sons = tstCtrl.sectionSonstiges;
            Test.stopTest();
        }