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
bakumbakum 

Unit Testing Newbie...need just a little guidance

Hi, I've never written a unit test class before and I'm trying to deploy my first trigger.  The trigger itself works fine, the class I wrote to test the trigger works fine, but when I try to deploy Eclipse tells me I have no test coverage on my trigger!  Not true!  Hopefully I'm doing something stupid.

The background details...we have custom fields for Total Costs on our Opportunities and Line Items, so we can calculate margins and markups and such and make sure our salespeople are doing a good job.  The trigger is supposed to do this:
-whenever a line item is created, updated, or deleted
-find the opportunityID of the line item.
-run through every line items attached to that oportunity.
-total all the Total Costs fields on those line items, and shove that total in Total_Costs_Apex__c on the opportunity itself.

The code works fine.  I just can't deploy.

Here's the code:

trigger update_opportunity_costs on OpportunityLineItem (after delete, after insert, after update)
{
    double totalCost = 0.00;
   
    for (OpportunityLineItem oli : Trigger.new)
    {   
         //grab info on every line item attached to the opportunity from the triggering line item.
         for (OpportunityLineItem myOles : [SELECT Total_Costs__c FROM OpportunityLineItem WHERE OpportunityID=:oli.OpportunityId])
         {
          //total the costs of these line items
          totalCost += myOles.Total_Costs__c;
         }
         Opportunity oppty = new Opportunity(Id=oli.OpportunityId);
         //update the opportunity Total Costs Apex field with the total costs of all line items.
         oppty.Total_Costs_Apex__c = totalCost;
         //update the opportunity
         update oppty;  
    }
   
}

~~~~~~
and here's the unit test class I wrote:
public class opportunity_costs_update_test {

    static testMethod void myTest()
    {
   
      Pricebook2 stdPb = [select Id from Pricebook2 where name='Standard' limit 1];   
      PricebookEntry pbe = [select p.Id From PricebookEntry p where p.Name = 'SLA: Silver' and p.Pricebook2Id = :stdPb.Id];

      Opportunity oppty = new Opportunity();
      oppty.name = 'Unit Test Opportunity.';
      oppty.ForecastCategory = 'Pipeline';
      oppty.CloseDate = Date.newInstance(2020,10,10);
      oppty.StageName = 'Proposal/Price Quote';
      oppty.CurrencyIsoCode = 'USD';
      oppty.Pricebook2ID = stdPb.Id;
      insert oppty;
       
      OpportunityLineItem oli = new OpportunityLineItem();
      oli.OpportunityId = oppty.Id;
      oli.Costs_Other__c = 32000.00;
      oli.TotalPrice = 70000.00;
      oli.Quantity = 2;
      oli.PricebookEntryId = pbe.Id;
      insert oli;
    }
}

Any help?  Thanks in advance!
EJWEJW
Are you deploying your unit test class with the trigger?  If not, then that's the problem.  The deployed code must include your unit tests.

Message Edited by EJW on 11-07-2007 09:18 AM

bakumbakum
THanks for the reply.  I'm a little confused though.  When I put the test code inside the trigger I get this error:

"Only top-level class methods can be declared static."  There must be something wrong with my syntax, no?


~~~~~~~~~~~~

trigger update_opportunity_costs on OpportunityLineItem (after delete, after insert, after update)
{

    for (OpportunityLineItem oli : Trigger.new)
    {
         double totalCost = 0.00;
                    
         //grab info on every line item attached to the opportunity from the triggering line item.
         for (OpportunityLineItem myOles : [SELECT Total_Costs__c FROM OpportunityLineItem WHERE OpportunityID=:oli.OpportunityId])
         {
          //total the costs of these line items
          totalCost += myOles.Total_Costs__c;
         }
         Opportunity oppty = new Opportunity(Id=oli.OpportunityId);
         //update the opportunity Total Costs field with the total costs of all line items.
         oppty.Total_Costs__c = totalCost;
         //update the opportunity
         update oppty;  
    }
   
    static testMethod void myTest()
    {
      //grab the standard pricebook ID
      Pricebook2 stdPb = [select Id from Pricebook2 where name='Standard' limit 1];
      //grab the product ID of a product in the price book.   
      PricebookEntry pbe = [select p.Id From PricebookEntry p where p.Name = 'SLA: Silver' and p.Pricebook2Id = :stdPb.Id];
     
      //create a new opportunity with the above Pricebook
      Opportunity oppty = new Opportunity();
      oppty.name = 'Unit Test Opportunity.';
      oppty.ForecastCategory = 'Pipeline';
      oppty.CloseDate = Date.newInstance(2020,10,10);
      oppty.StageName = 'Proposal/Price Quote';
      oppty.CurrencyIsoCode = 'USD';
      oppty.Pricebook2ID = stdPb.Id;
      insert oppty;
     
      //add a line item to our test opportunity
      OpportunityLineItem oli = new OpportunityLineItem();
      oli.OpportunityId = oppty.Id;
      oli.Costs_Other__c = 35000.00;
      oli.TotalPrice = 70000.00;
      oli.Quantity = 1;
      oli.PricebookEntryId = pbe.Id;
 
      insert oli;
     
    }


   
}
EJWEJW
Keep the code for the unit tests in a separate class and then when you use the Eclipse deployment wizard you need to deploy both the class containing your unit tests and the trigger at the same time.

bakumbakum
ah.  that is exactly what I tried the first time.  seperate trigger and test class files.  both deployed at the same time.  No joy.
bakumbakum
silly me...my products were slightly different so the unit test didn't work when deployed.  fixed that little problem and it all works.  Thanks for the help.