You need to sign in to do that
Don't have an account?
Chitral Chadda
when i check to see which lines are not covered in trigger
line 5 to end is not covered , but i have performed this in test class .
Test class to limit case insertion
I have a trigger which would limit no. of case created by a single user in a particular month, if case creation exceeds more than 99 (for particular owner id) than it would throw error ..
i wrote a test class for this but got only 12% coverage.
here is the trigger
trigger caseInsertion on Case (before insert) { case_insertions__c settings =case_insertions__c.getInstance('maximum'); Decimal maxvalue = settings.maximumCount__c; map<id,integer> mapcount = new map<id,Integer>(); set<id> owid = new set<id>(); for(case t: trigger.new) { if(t.accountid!=null) { owid.add(t.ownerid); } mapcount.put(t.ownerid,0); } map<id,user> usermap = new map<id,user>([select id, Name from user where id in : owid]); for(aggregateResult ar: [ select ownerid, count(id) from case where ownerid in :owid and CreatedDate=THIS_MONTH group by ownerid]) { mapcount.put((id)ar.get('ownerid'),(Integer)ar.get('expr0')); } for(case t:trigger.new) { mapcount.put(t.ownerid,mapcount.get(t.ownerid)+1); if(mapcount.get(t.ownerid)> maxvalue) { t.addError('Cant create more cases'); } } }
@isTest public class TestcaseInsertion{ public static testMethod void TestcaseLimitInsertion() { case_settings__c settings= new case_settings__c(); settings.Name='max'; settings.Max_Number__c=99; insert settings; Profile p = [select id from profile where Name='System Administrator']; user u1 = new user(alias='abc',email='abc@mail.com',emailencodingkey='UTF-8', lastname='Testing1', languagelocalekey='en_US',localesidkey='en_US',timezonesidkey='America/Los_Angeles', username='adminTas@testorg.com', profileid = p.id); insert u1; Account acc = new Account(); acc.Name='Chadda'; acc.ownerid=u1.id; insert acc; list<case> addcase = new list<case>(); //Date Tdate = System.Today(); for(Integer i = 0; i < 110; i++) { Case a = new Case (Origin='Phone',Status='New',Subject='hello' + i + 'number',ownerid=u1.id,Accountid = acc.id); addcase.add(a); } try{ insert addcase; } catch(Exception e) { System.debug('An error happened, as predicted!'); } } }
when i check to see which lines are not covered in trigger
line 5 to end is not covered , but i have performed this in test class .
What you should do is, create 99 cases in your test class and then try to create one more case which should be rejected per your trigger.
Now, test the exception as shown in the link below:
https://developer.salesforce.com/forums/ForumsMain?id=906F00000008xGuIAI using System.AssertEquals
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CHITRALPACKAGE.caseInsertion: execution of BeforeInsert
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.CHITRALPACKAGE.caseInsertion: line 4, column 1: []