You need to sign in to do that
Don't have an account?

Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required
I'm a new developer with apex. I am the getting the error when i tried to create an apex class and trigger. Could any one can help me?
Apex Trigger:
trigger ExpenseTrigger on Expense__c (before insert, before update) {
ExpenseUtil.areExpensesAllowed(Trigger.new);
}
Apex Class:
public with sharing class ExpenseUtil {
static final Double MAX_EXPENSE_PER_DAY = 1000000.00;
public static void areExpensesAllowed(Expense__c[] expenses) {Double totalExpenses = 0;
String createdbyId = UserInfo.getUserId();
/* Adds the expenses created in previous requests for today */
for(Expense__c eq:[SELECT ExpenseInRs__c FROM Expense__c
WHERE Date__c = TODAY
AND Expense__c.createdById = :createdbyId AND ExpenseInRs__c != null]) {totalExpenses += eq.ExpenseInRs__c;
}
/* Totals the expenses in the request */
for (Expense__c e:expenses) {totalExpenses += e.ExpenseInRs__c;
if(totalExpenses > MAX_EXPENSE_PER_DAY)e.addError('Expense request exceeds daily limit: ' + MAX_EXPENSE_PER_DAY);}
}
static testMethod void areExpensesAllowed() {System.assert('Expense' == 'Expense');
}
}
Even tried with some comments displayed in the community but not working. Thanks in advance :smileyhappy:
Try this:
static testMethod void areExpensesAllowed() { Expense__c[] expense = new Expense__c[0]; for(integer i=0;i<10;i++) { expense.add(new Expense__c(ExpenseInRs__c = 10.00,Date__c = syatem.today())); } insert expense; System.assert(Expense[0].ExpenseInRs__c == 10.00); Expense[0].ExpenseInRs__c = 1100000.00; update expense; }
Hi lvivanin,
Thanks for your effort. But still the problem(red X appears in my trigger object) persists.
-Sankar
I have a trigger and a class as displayed earlier. While compiling itself i am got the below error
"Description-Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required
Resource- ExpenseTrigger.trigger
Path- ExpenseProject/src/triggers
Location- line 1
Type- Force.com code coverage warning
"
even removing static declrations the effect remains the same.
I tested it creating Expense object and fields similar to your code, it works fine and has 100% code coverage:
trigger ExpenseTrigger on Expense__c (before insert, before update) {
ExpenseUtil.areExpensesAllowed(Trigger.new);
}
public with sharing class ExpenseUtil {
static final Double MAX_EXPENSE_PER_DAY = 1000000.00;
public static void areExpensesAllowed(Expense__c[] expenses) {
Double totalExpenses = 0;
String createdbyId = UserInfo.getUserId();
/* Adds the expenses created in previous requests for today */
for(Expense__c eq : [SELECT ExpenseInRs__c FROM Expense__c WHERE Date__c = TODAY AND ExpenseInRs__c != null AND createdById = :createdbyId])
{
totalExpenses += eq.ExpenseInRs__c;
}
/* Totals the expenses in the request */
for (Expense__c e:expenses) {
totalExpenses += e.ExpenseInRs__c;
if(totalExpenses > MAX_EXPENSE_PER_DAY)e.addError('Expense request exceeds daily limit: ' + MAX_EXPENSE_PER_DAY);
}
}
static testMethod void areExpensesAllowed()
{
Expense__c[] expense = new Expense__c[0];
for(integer i=0;i<10;i++)
{
expense.add(new Expense__c(ExpenseInRs__c = 10.00,Date__c = system.today()));
}
insert expense;
System.assert(Expense[0].ExpenseInRs__c == 10.00);
Expense[0].ExpenseInRs__c = 1100000.00;
update expense;
}
}
hope it helps!
Hi guys,
thanks for your efforts. It worked fine for me but for different set of example. For mileage object it worked fine. It is not showing any compilation error. when i used the same logic for an Expense object it shows compilation error. Still wondering. :)
thanks
sankar