You need to sign in to do that
Don't have an account?
Thijs Faber
Apex trigger Test class
Hi there,
I got the feeling that I'm almost there. My test class shows a disappointing 0% coverage. What am I doing wrong?
This is my trigger (works in my sandbox, aside from an unintended modulation to my corporate currency <- solved with custom formula field):
This is my (under performing) test class:
The help so far has already been awesome, thanks again you all!
Thijs
I got the feeling that I'm almost there. My test class shows a disappointing 0% coverage. What am I doing wrong?
This is my trigger (works in my sandbox, aside from an unintended modulation to my corporate currency <- solved with custom formula field):
trigger TotalAmountTrigger on Account (after insert, after update, after delete, after undelete) { Set<Id> accountIds = new Set<Id>(); for(Account c: Trigger.isDelete? Trigger.old : Trigger.new){ if(c.GroupName__c != null) { accountIds.add(c.GroupName__c ); } } List<CustomerGroups__c> accList = new List<CustomerGroups__c>(); for(AggregateResult currentAggResult:[SELECT GroupName__c accId, SUM(Customer_Loan_Amount__c ) sumAmt FROM Account WHERE GroupName__c in:accountIds GROUP BY GroupName__c ]) { CustomerGroups__c acc = new CustomerGroups__c(); acc.Id = (Id)currentAggResult.get('accId'); acc.Customer_Group_OLB__c = (decimal)currentAggResult.get('sumAmt'); accList.add(acc); accountIds.remove((Id)currentAggResult.get('accId')); } for(Id currAccId : accountIds) { CustomerGroups__c acc = new CustomerGroups__c(); acc.Id = currAccId; acc.Customer_Group_OLB__c = null; accList.add(acc); } update accList; }
This is my (under performing) test class:
@isTest public class TestTrigger1 { static testMethod void testMethod1() { CustomerGroups__c p = new CustomerGroups__c (); p.Name='testgroup'; insert p; Account c = new Account(GroupName__c = p.id); c.Name='CustomerTest'; c.Date_of_Sale__c=date.today(); c.Invoice_No__c='123'; c.GroupName__c='Rotterdam Group'; c.CurrencyIsoCode ='ZAR'; insert c; Opportunity g = new Opportunity (AccountId = c.id); g.Name='OpptyTest'; g.CloseDate=date.today(); g.CurrencyIsoCode ='ZAR'; g.StageName='Closed Won'; insert g; OpportunityLineItem b = new OpportunityLineItem (OpportunityId = g.id); b.Owed_so_far__c=100; b.Paid_so_far__c=5; b.Quantity=1; b.TotalPrice=1000; insert b; Product2 f = new Product2 (Id = b.id); f.Name='ACE1 R1,080 9Month Credit'; f.CurrencyIsoCode='ZAR'; insert f; } }
The help so far has already been awesome, thanks again you all!
Thijs
Small changes
Simply try the below test class:
@isTest
public class TotalAmountTriggertestclass
{
static testmethod void testTotalAmountTrigger()
{
CustomerGroups__c p = new CustomerGroups__c ();
p.Name='testgroup';
insert p;
Account acc=new Account();
acc.name='test trigger';
acc.Date_of_Sale__c=date.today();
acc.Invoice_No__c='Test 123';
acc.GroupName__c=p.Id;
Insert acc;
Test.starttest();
insert acc;
Test.stoptest();
}
}
Can you please Let me know if it works or not and also If you face any problems!!!
If it works don't forget to mark this as a best answer!!!
Thanks,
Raj
All Answers
Small changes
Simply try the below test class:
@isTest
public class TotalAmountTriggertestclass
{
static testmethod void testTotalAmountTrigger()
{
CustomerGroups__c p = new CustomerGroups__c ();
p.Name='testgroup';
insert p;
Account acc=new Account();
acc.name='test trigger';
acc.Date_of_Sale__c=date.today();
acc.Invoice_No__c='Test 123';
acc.GroupName__c=p.Id;
Insert acc;
Test.starttest();
insert acc;
Test.stoptest();
}
}
Can you please Let me know if it works or not and also If you face any problems!!!
If it works don't forget to mark this as a best answer!!!
Thanks,
Raj
Have a great weekend!!!!
Add the Currency ISO code field also in the test class may be it will increase more coverage
acc.CurrencyIsoCode ='ZAR';
Thanks,
Raj
I want to write below trigger test class. Please help me out not getting 100% code coverage
trigger OpportunityTriggerUpdate on Opportunity (after update) {
Map <Id, Opportunity> mapOpportunity = new Map < Id, Opportunity >();
Map <Opportunity, String> Endcustomeracctype = new Map < Opportunity, String >();
List <Account> accList = new List < Account >();
List <Account> accountList = new List < Account >();
Opportunity opp = new Opportunity();
List <Id> accIdsList = new List <Id>();
for ( Opportunity oppty : trigger.new ) {
if(oppty.End_Customer__c != null){
System.debug('Print End Customer'+oppty.End_Customer__c);
accIdsList.add(oppty.End_Customer__c);
System.debug('Print Account..............'+oppty.StageName);
if(oppty.StageName == 'Contract Signed'){
System.debug('Print Account..............'+oppty.StageName);
for(Account acc : [Select Id,Name,Type from Account where ID IN : accIdsList]){
System.debug('Print Account.....'+ acc);
// acc.Type = ('Customer','Prospect');
if(acc.Type != null && acc.Type !='Partner' && acc.Type !='Competitor' && acc.Type !='Customer'){
System.debug('Print Account Type.........'+acc.Type);
acc.Type = 'Indirect Customer';
System.debug('Print Account Type.........'+acc.Type);
accList.add(acc);
System.debug('Print Account Type.........'+accList);
}
}
}
}
}
update accList;
System.debug('Update Account List...........'+accList);
}
Thanks !!