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
Chitral ChaddaChitral Chadda 

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 .
SonamSonam (Salesforce Developers) 
Yes, thats correct because your trigger doesn't really cover the condition of not creating the case.

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
Chitral ChaddaChitral Chadda
yes, i tried to insert to insert 99 cases first , when i tried to insert one more . the test method failed and i got error:
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: []
SonamSonam (Salesforce Developers) 
You need to catch this DML exception in your trigger code - Check the following doc with the sample code snippet on the same page below:http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_exception_methods.htm
Chitral ChaddaChitral Chadda
Same error  i get
SonamSonam (Salesforce Developers) 
Can you pls share the code for your trigger where you have included the try and catch block together with the test class piece where youare testing the exception.