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
JohanLiljegrenJohanLiljegren 

Different code coverage in Sandbox and Production

Hi all,

I've written one of my first Apex classes, with a webservice enabled function that I call from an S-Control, that does a bit of calculations, updates some fields and then returns.
Problem is, I get 100% code coverage in Sandbox, but when I go to deploy it to Production I get only 48%. Do anyone have any suggestions on why that could be and what I could change to get more coverage in Production?

Code:
global class Calculate_Budget_Expenses
{
    WebService static Boolean CalculateBudgetExpenses( String ID )
    {
     ID budgetID = ID;
     
        Double ProgramExpensesToDate = 0;
        Double PlannedExpenses = 0;
        Double AdditionalExpensesNotCommitted = 0;
        Double QuarterBudgetConverted = 0;

        for ( Campaign tempCampaign : [ select Id,
convertCurrency(Total_Program_Expenses__c),
convertCurrency(Program_Budget__c),
convertCurrency(Expenses_Not_Committed__c)
from Campaign
where Marketing_Budget__c = :ID ] ) { ProgramExpensesToDate += tempCampaign.Total_Program_Expenses__c; PlannedExpenses += tempCampaign.Program_Budget__c; AdditionalExpensesNotCommitted += tempCampaign.Expenses_Not_Committed__c; } for ( Marketing_Budget__c tempBudget : [ SELECT convertCurrency(Quarter_Budget__c)
FROM Marketing_Budget__c
WHERE Id = :budgetID ] ) { QuarterBudgetConverted += tempBudget.Quarter_Budget__c; } Marketing_Budget__c Budget = [ SELECT Program_Expenses_To_Date__c,
Planned_Expenses__c,
Additional_Expenses_Not_Committed__c
from Marketing_Budget__c
where ID = :BudgetID ];

List<Marketing_Budget__c> Budgets = new List<Marketing_Budget__c>();
Budgets.Add(Budget);

Budget.Program_Expenses_To_Date__c = ProgramExpensesToDate;
Budget.Planned_Expenses__c = PlannedExpenses;
Budget.Additional_Expenses_Not_Committed__c = AdditionalExpensesNotCommitted;
Budget.Quarter_Budget_Converted__c = QuarterBudgetConverted;

Update Budgets;

return true;
}
static testMethod void test() {
Campaign camp = [ select Marketing_Budget__c
from Campaign
where Marketing_Budget__c != null limit 1 ];

Marketing_Budget__c budget = [ select Id
from Marketing_Budget__c
where Id = :camp.Marketing_Budget__c ];

CalculateBudgetExpenses(budget.Id);
}
}

 

JonPJonP
Your test methods depend on data in the organization, so you probably have a more robust data set in Sandbox than in Production.

A best practice is to create all your test data within your test method, and tailor any queries to get that data by Id, so your tests will be identical regardless of what data are in a particular organization.

Note that records inserted in testMethods are not committed to the database, so you don't have to worry about impacting production data.

Jon
JohanLiljegrenJohanLiljegren
Hey JonP, thanks for the feedback.
I will look into changing my approach to test data.

Now that I look at it more closely, the error log from the deployment actually shows a null pointer exception. That's probably why I'm not getting full coverage. =)

Any idea why this happens?

Code:
<-- snip -->

20080514191009.731:Class.Calculate_Budget_Expenses.CalculateBudgetExpenses: line 16, column 13: Double AdditionalExpensesNotCommitted <= Add
20080514191009.731:Class.Calculate_Budget_Expenses.CalculateBudgetExpenses: line 14, column 13: Double ProgramExpensesToDate <= Add
20080514191009.731:Class.Calculate_Budget_Expenses.CalculateBudgetExpenses: line 15, column 13: Double PlannedExpenses <= Add
System.NullPointerException: Attempt to de-reference a null object

Class.Calculate_Budget_Expenses.CalculateBudgetExpenses: line 15, column 32
Class.Calculate_Budget_Expenses.test: line 43, column 9

<-- snip -->

 

JonPJonP
Is it possible one of your currency fields in Campaign is blank/null?

If you run your test from the Force.com IDE, you can double-click on the error message to go directly to the line/col where the NullPointerException was thrown.