You need to sign in to do that
Don't have an account?
bakum
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!
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!
Message Edited by EJW on 11-07-2007 09:18 AM
"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;
}
}