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
sgottreusgottreu 

Writing Test Methods for Extension

I've been looking at all the examples for writing Test Methods and I keep coming across problems trying to write one for my class.  I'm enclosing the code below.  I would appreciate any help that can be provided on how to write test methods for this.

 

 

public class QuoteExtension {
    Integer LineCount = 0;
    
    public Opportunity Truck { get; private set; }  
    Double Truck_Subtotal = 0;
    
    public Opportunity Accessories { get; private set; }  
    Double Accessories_Subtotal = 0;
    
    public Opportunity CasedHole { get; private set; }  
    Double CasedHole_Subtotal = 0;

    public Opportunity OpenHole { get; private set; }  
    Double OpenHole_Subtotal = 0;
    
    public void Increment() {
        LineCount++;
    }

     public static testMethod void testQuoteExtenstion() {

        
    }
    
    
    public QuoteExtension(ApexPages.StandardController stdController) {
        ID id = ApexPages.CurrentPage().getParameters().get('id');
        Integer i = 0;
        
        /***** Begin Truck Section *****/
        
        Truck = [SELECT o.Id, o.Name, o.Amount, (SELECT op.Quantity, op.UnitPrice, op.TotalPrice,
            op.PricebookEntry.Name, op.OpportunityId, op.PricebookEntry.ProductCode,
            op.PricebookEntry.Product2.Family
            FROM OpportunityLineItems op WHERE op.PricebookEntry.Product2.Family = 'Truck')
            FROM Opportunity o where Id = :id];
        
            for(i=0;i<Truck.opportunityLineItems.size();i++) {
                Truck_Subtotal += Truck.opportunityLineItems[i].TotalPrice;
            }


        i = 0;
        /***** End Truck Section *****/
        
        /***** Begin Accessories Section *****/
        
        Accessories = [SELECT o.Id, o.Name, o.Amount, (SELECT op.Quantity, op.UnitPrice, op.TotalPrice,
            op.PricebookEntry.Name, op.OpportunityId, op.PricebookEntry.ProductCode,
            op.PricebookEntry.Product2.Family
            FROM OpportunityLineItems op WHERE op.PricebookEntry.Product2.Family = 'Accessories')
            FROM Opportunity o where Id = :id];
            
        for(i=0;i<Accessories.opportunityLineItems.size();i++) {
            Accessories_Subtotal += Accessories.opportunityLineItems[i].TotalPrice;
        }
        i = 0;
        
       /***** End Accessories Section *****/
        
        /***** Begin CasedHole Section *****/
        
        CasedHole = [SELECT o.Id, o.Name, o.Amount, (SELECT op.Quantity, op.UnitPrice, op.TotalPrice,
            op.PricebookEntry.Name, op.OpportunityId, op.PricebookEntry.ProductCode,
            op.PricebookEntry.Product2.Family
            FROM OpportunityLineItems op WHERE op.PricebookEntry.Product2.Family = 'Cased Hole')
            FROM Opportunity o where Id = :id];
        
        for(i=0;i<CasedHole.opportunityLineItems.size();i++) {
            CasedHole_Subtotal += CasedHole.opportunityLineItems[i].TotalPrice;
        }
        i = 0;
        /***** End CasedHole Section *****/

        /***** Begin OpenHole Section *****/
        
        OpenHole = [SELECT o.Id, o.Name, o.Amount, (SELECT op.Quantity, op.UnitPrice, op.TotalPrice,
            op.PricebookEntry.Name, op.OpportunityId, op.PricebookEntry.ProductCode,
            op.PricebookEntry.Product2.Family
            FROM OpportunityLineItems op WHERE op.PricebookEntry.Product2.Family = 'Open Hole')
            FROM Opportunity o where Id = :id];
        
        for(i=0;i<OpenHole.opportunityLineItems.size();i++) {
            OpenHole_Subtotal += OpenHole.opportunityLineItems[i].TotalPrice;
        }
        i = 0;
        /***** End OpenHole Section *****/
    }

    public Double Gettruck_subtotal() {
        return Truck_Subtotal;
    }
    public Double Getaccessories_subtotal() {
        return Accessories_Subtotal;
    }
    public Double Getcasedhole_subtotal() {
        return CasedHole_Subtotal;
    }
    public Double Getopenhole_subtotal() {
        return OpenHole_Subtotal;
    }

    public String Gettruck_style() {
        if(Truck_Subtotal == 0) {
            return 'style="display:none;"';
        } else {
            return '';
        }
    }    
    public String Getaccessories_style() {
        if(Accessories_Subtotal == 0) {
            return 'style="display:none;"';
        } else {
            return '';
        }
    }
    public String Getcasedhole_style() {
        if(CasedHole_Subtotal == 0) {
            return 'style="display:none;"';
        } else {
            return '';
        }
    }
    public String Getopenhole_style() {
        if(OpenHole_Subtotal == 0) {
            return 'style="display:none;"';
        } else {
            return '';
        }
    }
    
    public Integer Getlinecount() {
        Increment();
        return LineCount;
    }
}
Best Answer chosen by Admin (Salesforce Developers) 
sgottreusgottreu

So it took some more and trial and error but I modified my code and was able to get to over 90% code coverage. I have abbreviated the code and I'm posting in hopes that others will be able to benefit from it.

 

public class QuoteExtension {
    
    public Opportunity Truck { get; private set; }  
    Double Truck_Subtotal = 0;
    String Truck_Style = '';
    
    public QuoteExtension(ApexPages.StandardController stdController) {
        ID id = ApexPages.CurrentPage().getParameters().get('id');
        
        Integer i = 0;
        
        /***** Begin Truck Section *****/
        
        Truck = [SELECT o.Id, o.Name, o.Amount, (SELECT op.Quantity, op.UnitPrice, op.TotalPrice,
            op.PricebookEntry.Name, op.OpportunityId, op.PricebookEntry.ProductCode,
            op.PricebookEntry.Product2.Family
            FROM OpportunityLineItems op WHERE op.PricebookEntry.Product2.Family = 'Truck')
            FROM Opportunity o where Id = :id];
        
            for(i=0;i<Truck.opportunityLineItems.size();i++) {
                Truck_Subtotal += Truck.opportunityLineItems[i].TotalPrice;
            }

        i = 0;
       
       if(Truck_Subtotal == 0) {
            Truck_Style = 'style="display:none;"';
        }
        /***** End Truck Section *****/

    }

    public Double Gettruck_subtotal() {
        return Truck_Subtotal;
    }

    public String Gettruck_style() {
        return Truck_Style;
    }    
   
    
    public static testMethod void testQuoteExtenstion() {

        // Insert test Opportunity
        
        Opportunity testOppty = new opportunity (Name = 'Force Monkey 4x4',
                StageName = 'Prospect', Amount = 2000, CloseDate = System.today() );
        
        insert testOppty;
        
        // Insert test Truck OpportunityLineItem
        
        OpportunityLineItem testTruckOLI = new OpportunityLineItem (Quantity = 5,
        UnitPrice = 100, OpportunityId = testOppty.id, PricebookEntryId = '01u80000002XFfKAAW');
        
        insert testTruckOLI;
       
        // Instantiate VisualForce Page
        
        PageReference pg = Page.yourVFpageName; Test.setCurrentPage(pg); ApexPages.currentPage().getParameters().put('id', testOppty.id);
 
        // Instantiate custom QuoteExtension controller
       ApexPages.StandardController stc = new ApexPages.StandardController(testCasedHoleOLI);
        QuoteExtension qe = new QuoteExtension(stc);
        
        // Unit Test: getTruck_Subtotal & getTruck_Style
        
        Double iTruck = qe.getTruck_Subtotal();
        System.assertEquals (iTruck, 500); // when inserting truck OLI above, make sure they = 500
       
       String sTruck = qe.Gettruck_style();
        System.assertEquals (sTruck, '');
        

    
    }
    
}

All Answers

JPSeaburyJPSeabury

Well, I wrote a really long reply to this, but my session timed out before I hit POST, so naturally I lost it all. This reply will be ... shorter.  =)

 

As you've probably picked up from reading the docs, the goal of test methods is to execute every line of your Apex code (or at least 75% of it), and validate that the results returned are as expected.  There's a bit more to it than that, but that short summary will get you started with the basic unit tests you need to build.

 

I usually start by writing out the unit tests for my testMethod as comments: 

 

public static testMethod void testQuoteExtenstion() {

 

// Insert test Opportunity

// Insert test OpportunityLineItems

 

// Instantiate VisualForce Page

// Instantiate custom QuoteExtension controller

 

// Execute / validate getter methods }

 

 

 

That gives you the frame work for building your test code.  Then, start filling in the code:

 

 

public static testMethod void testQuoteExtenstion() {

 

// Insert test Opportunity

Opportunity testOppty = new opportunity (Name = 'Force Monkey 4x4', Date = System.today(),

Stage = 'Prospect', Amount = 5000 );

insert testOppty;

 

// Insert test Truck OpportunityLineItems

OpportunityLineItems testTruckOLI = new OpportunityLineItems (Quantity = 5, UnitPrice = 100,

TotalPrice = 500, OpportunityId = testOppty.id, ... PricebookEntry.Product2.Family = 'Truck');

insert OpportunityLineItems;

 

// Insert test Accessories OLI

:

: Do as above, for each of your OpportunityLineItems (Accessories, Cased Hole, Open Hole

:

 

// Instantiate VisualForce Page

  PageReference pg = Page.yourVFpageName; Test.setCurrentPage(pg); ApexPages.currentPage().getParameters().put('id', testOppty.id);

 

// Instantiate custom QuoteExtension controller

QuoteExtension qe = new QuoteExtension();

 

// Unit Test: getTruck_Subtotal

Double iTruck = qe.getTruck_Subtotal();

System.assertEquals (iTruck, 500); // when inserting truck OLI above, make sure they = 500

 

// Unit Test: getAccessories_Subtotal

:

: }

 

 

 

Message Edited by JPSeabury on 05-06-2009 08:51 PM
sgottreusgottreu

So I updated the test method that JPSeabury graciously submitted. However whenever I try to save the class I receive the following error msg.

 

Invalid type: OpportunityLineItems at line 147 column 49

 

Any advice on what is happening?

 

----------------------------------------------------------

public static testMethod void testQuoteExtenstion() {

        // Insert test Opportunity
        
        Opportunity testOppty = new opportunity (Name = 'Force Monkey 4x4', Date = System.today(),
                Stage = 'Prospect', Amount = 2000 );
        
        insert testOppty;
        
        // Insert test Truck OpportunityLineItems
        
        OpportunityLineItems testTruckOLI = new OpportunityLineItems (Quantity = 5,
        UnitPrice = 100, TotalPrice = 500, PricebookEntry.Name = 'Truck Item',
        PricebookEntry.ProductCode = '123456', OpportunityId = testOppty.id,
        PricebookEntry.Product2.Family = 'Truck');
        
        insert OpportunityLineItems;
        
        // Insert test Accessories OpportunityLineItems
                OpportunityLineItems testAccessoriesOLI = new OpportunityLineItems (Quantity = 5,
        UnitPrice = 100, TotalPrice = 500, PricebookEntry.Name = 'Accessories Item',
        PricebookEntry.ProductCode = '456DEF', OpportunityId = testOppty.id,
        PricebookEntry.Product2.Family = 'Accessories');
       
        insert OpportunityLineItems;
   
        // Insert test OpenHole OpportunityLineItems
       
        OpportunityLineItems testOpenHoleOLI = new OpportunityLineItems (Quantity = 5,
        UnitPrice = 100, TotalPrice = 500, PricebookEntry.Name = 'OpenHole Item',
        PricebookEntry.ProductCode = '789GHI', OpportunityId = testOppty.id,
        PricebookEntry.Product2.Family = 'Open Hole');
       
        insert OpportunityLineItems;
   
        // Insert test CasedHole OpportunityLineItems
       
        OpportunityLineItems testCasedHoleOLI = new OpportunityLineItems (Quantity = 5,
        UnitPrice = 100, TotalPrice = 500, PricebookEntry.Name = 'CasedHole Item',
        PricebookEntry.ProductCode = '012JKL', OpportunityId = testOppty.id,
        PricebookEntry.Product2.Family = 'Cased Hole');
        
        insert OpportunityLineItems;
        
        // Instantiate VisualForce Page
        
        PageReference pg = Page.yourVFpageName; Test.setCurrentPage(pg);

                ApexPages.currentPage().getParameters().put('id', testOppty.id);
 
        // Instantiate custom QuoteExtension controller
        
        QuoteExtension qe = new QuoteExtension();
        
        // Unit Test: getTruck_Subtotal
        
        Double iTruck = qe.getTruck_Subtotal();
        System.assertEquals (iTruck, 500);
        
        // Unit Test: getAccessories_Subtotal
        
        Double iAccessories = qe.getAccessories_Subtotal();
        System.assertEquals (iAccessories, 500);
    
        // Unit Test: getOpenHole_Subtotal
        
        Double iOpenHole = qe.getOpenHole_Subtotal();
        System.assertEquals (iOpenHole, 500);
    
        // Unit Test: getCasedHole_Subtotal
        
        Double iCasedHole = qe.getCasedHole_Subtotal();
        System.assertEquals (iCasedHole, 500); 

}

sgottreusgottreu

So it took some more and trial and error but I modified my code and was able to get to over 90% code coverage. I have abbreviated the code and I'm posting in hopes that others will be able to benefit from it.

 

public class QuoteExtension {
    
    public Opportunity Truck { get; private set; }  
    Double Truck_Subtotal = 0;
    String Truck_Style = '';
    
    public QuoteExtension(ApexPages.StandardController stdController) {
        ID id = ApexPages.CurrentPage().getParameters().get('id');
        
        Integer i = 0;
        
        /***** Begin Truck Section *****/
        
        Truck = [SELECT o.Id, o.Name, o.Amount, (SELECT op.Quantity, op.UnitPrice, op.TotalPrice,
            op.PricebookEntry.Name, op.OpportunityId, op.PricebookEntry.ProductCode,
            op.PricebookEntry.Product2.Family
            FROM OpportunityLineItems op WHERE op.PricebookEntry.Product2.Family = 'Truck')
            FROM Opportunity o where Id = :id];
        
            for(i=0;i<Truck.opportunityLineItems.size();i++) {
                Truck_Subtotal += Truck.opportunityLineItems[i].TotalPrice;
            }

        i = 0;
       
       if(Truck_Subtotal == 0) {
            Truck_Style = 'style="display:none;"';
        }
        /***** End Truck Section *****/

    }

    public Double Gettruck_subtotal() {
        return Truck_Subtotal;
    }

    public String Gettruck_style() {
        return Truck_Style;
    }    
   
    
    public static testMethod void testQuoteExtenstion() {

        // Insert test Opportunity
        
        Opportunity testOppty = new opportunity (Name = 'Force Monkey 4x4',
                StageName = 'Prospect', Amount = 2000, CloseDate = System.today() );
        
        insert testOppty;
        
        // Insert test Truck OpportunityLineItem
        
        OpportunityLineItem testTruckOLI = new OpportunityLineItem (Quantity = 5,
        UnitPrice = 100, OpportunityId = testOppty.id, PricebookEntryId = '01u80000002XFfKAAW');
        
        insert testTruckOLI;
       
        // Instantiate VisualForce Page
        
        PageReference pg = Page.yourVFpageName; Test.setCurrentPage(pg); ApexPages.currentPage().getParameters().put('id', testOppty.id);
 
        // Instantiate custom QuoteExtension controller
       ApexPages.StandardController stc = new ApexPages.StandardController(testCasedHoleOLI);
        QuoteExtension qe = new QuoteExtension(stc);
        
        // Unit Test: getTruck_Subtotal & getTruck_Style
        
        Double iTruck = qe.getTruck_Subtotal();
        System.assertEquals (iTruck, 500); // when inserting truck OLI above, make sure they = 500
       
       String sTruck = qe.Gettruck_style();
        System.assertEquals (sTruck, '');
        

    
    }
    
}

This was selected as the best answer