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
prachi barahateprachi barahate 

test class for a trigger having count soql

I had created a trigger on a custom object 'Job_application__c' as follows:
Trigger code:
trigger chk_no_of_appn_appliedTODAY on Job_Application__c (before insert,before update) 
{
    Integer n=[select count() from Job_Application__c where createddate=TODAY and CreatedById =:UserInfo.getUserId()];

    for(Job_Application__c j:Trigger.new)
    {
        if((j.createddate==date.today())&&(j.CreatedById==UserInfo.getUserId()))
        n+=1;-----------------1
        
           if(n>5)
            j.addError('you have exceeded today limit');------------------------2
    }

And had written test class like as :
Test Class :
@isTest
public class t2 
{
    static testmethod void check()
    {
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard Employee'];

        User u2 = new User(Alias = 'Sailee', Email='Sailee@testorg.com',EmailEncodingKey='UTF-8', 
                            LastName='Testing', LanguageLocaleKey='en_US', 
                            LocaleSidKey='en_US', ProfileId = p.Id,
                          TimeZoneSidKey='America/Los_Angeles', UserName='Sailee@testorg.com');
        insert u2 ;
        
          
            
           System.runAs(u2)
           {  
               Position__c pos= new Position__c(Name='vb_4');
               insert pos;
           
               Candidate__C c= new Candidate__C(First_name__c='Prachci');
               insert c;
               for(integer i=0;i<5;i++)
               {
                    Job_Application__c  j =new Job_Application__c();
                    j.position__C=pos.Id;
                    j.Candidate__c=c.Id;                    
                    insert j;
                   
               }
               try
               {    
                   Test.StartTest(); 
                    
                   job_Application__c  j1 =new Job_Application__c(position__C=pos.Id,Candidate__c=c.Id);
                   insert j1;                      
                   Test.StopTest();
               }
               catch(DMLException e)
               {
                   System.assert(e.getMessage().contains('you have exceeded today limit'));
               }
               
           }

    }
}
This class is giving 71% coverage and highlighting line marked---1  and --2  as not traced in results.
Please me in modifying code to achieve 100% coverage.
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Prachi barahate, Please mark it as best answer if it resolves your issue.

Thanks
Rahul Kumar