You need to sign in to do that
Don't have an account?
Create a test class for trigger
Hello,
I have developed a trigger on Attachments. My idea is to update a case field when a new attachment is insert on a case. It works OK, not perfect.
I can't deploy it to production because I'm not able to create a test class for it... I tried but I'm only starting with Apex now. The code is below, can someone help me build a test class for it?
trigger NewAttachment on Attachment (before insert) {List<Case> CasesToUpdate = new List<Case>();for (Attachment t: Trigger.new){try{Case c = new Case(Id = t.ParentId);c.HasAttachments__c = true;CasesToUpdate.add(c);}catch (Exception e) {}}update CasesToUpdate;}//end
Thanks!
Something like the following should do it. Haven't compiled this but its based on one of my working tests so shouldn't be too far off.
static testMethod void testAttachments() { Case cse=new Case(); insert cse; Attachment attach=new Attachment(); attach.Name='Unit Test Attachment'; Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body'); attach.body=bodyBlob; attach.parentId=cse.id;
insert attach;
List<Attachment> attachments=[select id, name from Attachment where parent.id=:cse.id]; System.assertEquals(1, attachments.size()); }
All Answers
you'll just need to create a Attachment in the test class and then run the test class....
private class testClass
{
static testMethod void myUnitTest()
{
Attachment at = new Attachment();
// initialize the compulsory fields
insert at;
}
}
Something like the following should do it. Haven't compiled this but its based on one of my working tests so shouldn't be too far off.
static testMethod void testAttachments() { Case cse=new Case(); insert cse; Attachment attach=new Attachment(); attach.Name='Unit Test Attachment'; Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body'); attach.body=bodyBlob; attach.parentId=cse.id;
insert attach;
List<Attachment> attachments=[select id, name from Attachment where parent.id=:cse.id]; System.assertEquals(1, attachments.size()); }
Thank you bob.
It achieved 88% test coverage.
I have a similar problem getting my test class to work for the following trigger:
trigger ProductCalculations on Product2 (after insert, after update) {
Set<ID> statesIds = new Set<Id>{};
Map<ID, State_Activity_Management__c> statesMap = new Map<ID, State_Activity_Management__c>();
for (Product2 product:trigger.new){
statesIds.add(product.State_Activity_Management__c);
}
SAMTotals.UpdateTotals(statesIds);
}
public class SAMTotals {
public static void UpdateTotals(Set<ID> stateIds)
{
// Get list of SAM
List<State_Activity_Management__c> statelist = [SELECT Id,
Total_Annualized_Premium__c,
Total_Annualized_Premium_Non_Partnership__c,
Total_Annualized_Premium_Partnership__c,
Total_Claims__c,
Total_Policies_Inforce__c
FROM State_Activity_Management__c WHERE Id=:stateIds];
Map<Id, State_Activity_Management__c> mapStates = new Map<Id, State_Activity_Management__c>();
List<State_Activity_Management__c> toupdate = new List<State_Activity_Management__c>();
// Add Id and state object to map for easy reference
for (State_Activity_Management__c s:statelist)
{
mapStates.put(s.Id, s);
}
// Query all the totals
AggregateResult[] sumResults = [SELECT State_Activity_Management__c,
SUM(Annualized_Premium__c) premium,
SUM(Annualized_Premium_Non_Partnership__c) nonpartnership,
SUM(Annualized_Premium_Partnership__c) premiumpartnership,
SUM(Policies_Inforce__c) inforce,
SUM(Total_Claims__c) claims
FROM Product2 WHERE State_Activity_Management__c=:stateIds GROUP BY State_Activity_Management__c];
for (AggregateResult ar : sumResults)
{
State_Activity_Management__c s = mapStates.get((ID)ar.get('State_Activity_Management__c'));
// Update totals
s.Total_Annualized_Premium__c = (Double)ar.get('premium');
s.Total_Annualized_Premium_Non_Partnership__c = (Double)ar.get('nonpartnership');
s.Total_Annualized_Premium_Partnership__c = (Double)ar.get('premiumpartnership');
s.Total_Claims__c = (Decimal)ar.get('claims');
s.Total_Policies_Inforce__c = (Decimal)ar.get('inforce');
toupdate.add(s);
}
if (toupdate!=null){
update(toupdate);
}
}
}
My test class is as follows, but is not getting me any coverage.
@isTest
public with sharing class testSAMTotals
{
static testMethod void UpdateTotals ()
{
State_Activity_Management__c st = new State_Activity_Management__c(Name = 'testOhio');
insert st;
State_Activity_Management__c state = [SELECT Id, Name FROM State_Activity_Management__c where Id =:st.Id];
system.debug('statename id ' + state.Id);
List<Product2> prodList = new List<Product2>();
for(Integer i = 0;i<200; i++){
Product2 prod = new Product2(name='test' +i,State_Activity_Management__c=state.Id);
prodList.add(prod);
}
insert prodList;
}
}
I am not getting any coverage. Any help would greatly be appreciated as I am relatively new to apex.
Thank You