I wrote a trigger to prevent more than n incidents for a client(lookup to user) at any point of time.
trigger CountLimit on Incident__c (after insert,after update,after undelete) {
Set<Id> clientids = new Set<Id>();
Map<Id,Integer> clientmap = new Map<Id,Integer>();
Max_Incidents__c cs = Max_Incidents__c.getInstance('Platinum');
Integer maxincs = (Integer)cs.MaxIncidentsPerClient__c;
for(Incident__c inc:Trigger.new)
{
clientids.add(inc.Client__c);
}
Map<Id,User> usermap = new Map<Id,User>([select id,name from user where id in:clientids]);
for(AggregateResult result:[select count(id) sum, client__c client from Incident__c where client__C in:clientids group by client__C])
{
clientmap.put((Id)result.get('client'),(Integer)result.get('sum'));
}
for(Incident__c inc:Trigger.new)
{
if(clientmap.get(inc.Client__c)>maxincs)
{
inc.addError('You can\'t open more than '+maxincs+' incidents for '+ usermap.get(inc.Client__c));
}
}
And I wrote this test class-
@isTest
public class IncidentLimit_Test {
public static testmethod void testcountincident(){
User usr = CreateUser('Ben','Bentaleb');
List<Incident__c> inclist = new List<Incident__c>();
try{
System.runAs(usr){
Test.startTest();
for(integer i=1;i<=5;i++)
{
inclist.add(new Incident__c(client__c=usr.id,Description__c='incident'+i));
}
insert inclist;
Test.stopTest();
}
}
catch(exception e){
e.getmessage().contains('You can\'t open more than');
}
finally{
System.assertEquals(5, inclist.size());}
}
public static UserRole CreateUserRole(String name) {
UserRole ur = new UserRole();
ur.Name = name;
insert ur;
return ur;
}
public static User CreateUser(String firstname, String lastname) {
UserRole ur = CreateUserRole('Testing');
profile pr = [SELECT Id FROM Profile WHERE Name='Standard User'];
User usr = new User(FirstName = firstname,
LastName = lastname,
Username = firstname + '.' + lastname + '@testing.com',
IsActive = true,
UserRoleId = ur.Id,
Email = '[email protected]',
Alias = 'tusr',
TimeZoneSidKey = 'Europe/Berlin' ,
LocaleSidKey = 'en_US',
EmailEncodingKey = 'UTF-8',
ProfileId = pr.id,
LanguageLocaleKey = 'en_US'
);
insert usr;
return usr;
}
}
The issue I'm here facing is until I add the parameter (seealldata=true) into the annotation the code coverage remains 26% otherwise it's 93%. Please advise.
seealldata
annotation. Your test is creating a user and setting theClient__c
to that new user so your trigger should be ignoring all theIncident__c
records in your org because of this.seealldata
set tofalse
is because your trigger is running onbefore insert
which means theIncident__c
records have not been inserted into the database so your SOQL query returns no results.seeAllData=true
you cannot access the custom setting. So try creating the custom setting data in test class, remove theseeAllData
and check for coverage.Max_Incidents__c.MaxIncidentsPerClient__c
may not be set when usingseeAllData=false
, when it's true you will be using whatever value is set in your org. You test should be creating this custom settting.