You need to sign in to do that
Don't have an account?
Need help on writting test class
hi for below class i started to write test class but i'm struck .. i'm not aware of how to pass the values in test class any one help me on this..
Class:
and my test class is,,,
Class:
public with sharing class EventTriggerHandller { /** * @Author * @return: * @parameters: List of event object, list of old event object * @description:This method will accept the set of new event values and old event values. * When event status is changed from Planned to Schedule, tasks would be created for Gala and donor events. * * */ public static void eventTaskCreation(list<Events__c> newEvent, Map<Id, Events__c> oldEvent){ system.debug('INside Handler'); Date eventStartDate; Date onedayBfrEvent; String userId = UserInfo.getUserId(); List<Task> taskList= new List<Task>(); DKMS_Util dkmsUtil= new DKMS_Util(); //getting teh record type map from DKMS_Util class method getAllRecordTypes() Map<id, RecordType> recType= dkmsUtil.getAllRecordTypes(); for(Events__C eventNew: newEvent){ system.debug('inside for'); if(eventNew.Start_Date_and_Time__c != null){ eventStartDate= date.newinstance(eventNew.Start_Date_and_Time__c.year(), eventNew.Start_Date_and_Time__c.month(), eventNew.Start_Date_and_Time__c.day()); onedayBfrEvent=eventStartDate.addDays(-1); system.debug('onedayBfrEvent-->'+onedayBfrEvent); system.debug('eventStartDate-->'+eventStartDate); } system.debug('recType-->'+recType); if((eventNew.status__C != oldEvent.get(eventNew.id).status__C) && (eventNew.Status__c =='Scheduled' && oldEvent.get(eventNew.id).status__C =='Planned')){ if(recType.get(eventNew.recordtypeid) != null){ if ((recType.get(eventNew.recordtypeid).Name == 'Gala Event' || recType.get(eventNew.recordtypeid).Name =='Donor Drive')){ system.debug('gala & donor -->'); Task evtTask = new Task(); evtTask.Subject = 'Inform the City Hall'; evtTask.status = 'Not Started'; if(recType.get(eventNew.recordtypeid).Name == 'Gala Event' && eventNew.Start_Date_and_Time__c != null){ evtTask.ActivityDate = eventStartDate.addDays(-14); // setting the due date for Gala }else if(recType.get(eventNew.recordtypeid).Name == 'Donor Drive' && eventNew.Start_Date_and_Time__c != null){ evtTask.ActivityDate = eventStartDate.addDays(-12); // setting the due date for Donor Drive } evtTask.IsReminderset= true; evtTask.ReminderDateTime = datetime.newInstance(onedayBfrEvent.year(), onedayBfrEvent.month(),onedayBfrEvent.day(), 09, 00,00); evtTask.whatId = eventNew.id; evtTask.ownerId = userId; // for now instead of drive manager, its being assigned to logged in user. taskList.add(evtTask); } if (recType.get(eventNew.recordtypeid).Name == 'Gala Event'){ system.debug('gala 10 -->'); Task evtTask = new Task(); evtTask.Subject = 'Call Catering Company'; evtTask.status = 'Not Started'; if(eventStartDate != null){ evtTask.ActivityDate = eventStartDate.addDays(-10); // setting the due date for Gala evtTask.ReminderDateTime = datetime.newInstance(onedayBfrEvent.year(), onedayBfrEvent.month(),onedayBfrEvent.day(), 09, 00,00); } evtTask.IsReminderset = true; evtTask.whatId = eventNew.id; evtTask.ownerId = userId; // for now instead of drive manager, its being assigned to logged in user. taskList.add(evtTask); } } } } system.debug('taskList--->'+taskList.size()); //Inserting task list.. insert taskList; } }
and my test class is,,,
@isTest public class EventTriggerHandller_Test { public static testMethod void eventTaskCreationTest() { Profile ProfileObj= [SELECT Id FROM Profile WHERE Name='System Administrator' LIMIT 1]; //Inserting data into User User UserObj = new User(LastName = 'Test User Account1',Isactive =true,Username = 'testuseraccount1@test.com',Email = 'testuser@test.com',Alias = 'ta1' ,CommunityNickname= 'TestCN1' ,TimeZoneSidKey = 'America/Los_Angeles',LocaleSidKey='en_US',EmailEncodingKey= 'ISO-8859-1',ProfileId = ProfileObj.Id, LanguageLocaleKey = 'en_US'); insert UserObj ; System.assert(UserObj.id!=null); System.runAs(UserObj) { Map<id,String> mprecordtypeName=new Map<id,String>(); FOR(RecordType rt:[SELECT Id, Name FROM RecordType WHERE SobjectType = 'Events__c']) { mprecordtypeName.put(rt.id,rt.Name); system.debug('mprecordtypeName---' +mprecordtypeName); } Events__c evnt = new Events__c(); evnt.name = 'Test Gala Event'; evnt.Start_Date_and_Time__c= system.today(); insert evnt; //access test data Events__c insertedevnt=[select id,name,Start_Date_and_Time__c from Events__c where id =:evnt.Id]; System.assertEquals(insertedevnt.Name, 'Test Gala Event'); //Events__c testevntnew = new Events__c( Name = 'Test event new',Start_Date_and_Time__c= system.today()); //Events__c testevntold = new Events__c( Name = 'Test event old',Start_Date_and_Time__c= system.today()); //Events__c.eventTaskCreation(testevntnew, testevntold); } } }
So it is in the initial state. You can leave the assert at line 22 in if you want - it ensures the data is correct (I might instead assert that "Status__c" is 'Planned' rather than the Start_date - because Status is the field we are actually interested in.
Then once it is inserted, you need to alter your condition, call an update and check the trigger has fired.. so around line 24 go for:
This will change the status - as your trigger expects, and call the update (the test.startTest() stuff is good practise for testing triggers, as it introduces a new govener limit scope for the tested code)
One you have that, you should be able to assert that the tasks are created, around line 28 then:
Is that what you are after? It doesn't seem that complicated considering the rest of your code! I would have thought you could get all that yourself, unless I have misunderstood the requirement!
All Answers
So it is in the initial state. You can leave the assert at line 22 in if you want - it ensures the data is correct (I might instead assert that "Status__c" is 'Planned' rather than the Start_date - because Status is the field we are actually interested in.
Then once it is inserted, you need to alter your condition, call an update and check the trigger has fired.. so around line 24 go for:
This will change the status - as your trigger expects, and call the update (the test.startTest() stuff is good practise for testing triggers, as it introduces a new govener limit scope for the tested code)
One you have that, you should be able to assert that the tasks are created, around line 28 then:
Is that what you are after? It doesn't seem that complicated considering the rest of your code! I would have thought you could get all that yourself, unless I have misunderstood the requirement!