You need to sign in to do that
Don't have an account?
Test Class Runs Successful But No Code Coverage
Hi All,
I created a trigger so that when a new event is created in Public Calendar a new record under custom object CalendarEV will be created automatically, I also created the test class, it runs successfully but no code coverage, please help.
trigger CalendarEV on Event (after insert) {
List<Event> Events = new List<Event>();
for (Event e : Trigger.new) {
if (e.Subject != null ) {
CalendarEV__c C = new CalendarEV__c();
c.Name = e.Subject;
c.Start__c = e.StartDateTime;
c.End__c = e.EndDateTime;
c.Location__c = e.Location;
c.Description__c = e.Description;
insert C;
}
}
}
@isTest
public class CalendarEVTest
{
static testMethod void insertNewCalendarEV()
{
Test.StartTest();
Event event = new Event();
event.Subject = 'Test Event';
event.Location = 'Test Location';
event.Description = 'Test Description';
Test.StopTest();
}
}
I created a trigger so that when a new event is created in Public Calendar a new record under custom object CalendarEV will be created automatically, I also created the test class, it runs successfully but no code coverage, please help.
trigger CalendarEV on Event (after insert) {
List<Event> Events = new List<Event>();
for (Event e : Trigger.new) {
if (e.Subject != null ) {
CalendarEV__c C = new CalendarEV__c();
c.Name = e.Subject;
c.Start__c = e.StartDateTime;
c.End__c = e.EndDateTime;
c.Location__c = e.Location;
c.Description__c = e.Description;
insert C;
}
}
}
@isTest
public class CalendarEVTest
{
static testMethod void insertNewCalendarEV()
{
Test.StartTest();
Event event = new Event();
event.Subject = 'Test Event';
event.Location = 'Test Location';
event.Description = 'Test Description';
Test.StopTest();
}
}
@isTest
public class TestCalendarEV
{
static testMethod void insertNewCalendarEvent()
{
Test.StartTest();
Event testEvent= new Event();
testEvent.Subject = 'EventTest';
testEvent.Location = 'Location';
testEvent.Description = 'Description';
testEvent.IsAllDayEvent=true;
testEvent.StartDateTime = datetime.newInstance(2014, 11, 13, 16, 30, 0);
testEvent.EndDateTime = datetime.newInstance(2014, 11, 13, 18, 30, 0);
insert testEvent;
Test.StopTest();
System.assertEquals ('EventTest', testEvent.Subject);
}
}
All Answers
Add an "insert event" before your Test.stopTest();
You also need to requery the Event record, and use System.assert calls to verify your trigger is working as expected.
Read up on apex tests with the following link: https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
Thanks for the reply, I changed it but still no code coverage, please help:
Error message: System.DmLException: insert failed. First exception on row 0; First error, Required field missing
Stack Trace: Class.CalendarEVTest.insertNewCalendarEV: Line11, colum 1
@isTest
public class CalendarEVTest
{
static testMethod void insertNewCalendarEV()
{
Test.StartTest();
Event e = new Event();
e.Subject = 'Test Event';
e.Location = 'Test Location';
e.Description = 'Test Description';
insert e;
Test.StopTest();
System.assertEquals ('Test Event', e.Subject);
}
}
@isTest
public class CalendarEVTest
{
static testMethod void insertNewCalendarEV()
{
Test.StartTest();
Event e = new Event();
e.Subject = 'Test Event';
e.Location = 'Test Location';
e.Description = 'Test Description';
e.StartDateTime = datetime.newInstance(2014, 9, 15, 12, 30, 0);
e.EndDateTime = datetime.newInstance(2014, 9, 15, 13, 30, 0);
insert e;
Test.StopTest();
System.assertEquals ('Test Event', e.Subject);
}
}
@isTest
public class TestCalendarEV
{
static testMethod void insertNewCalendarEvent()
{
Test.StartTest();
Event testEvent= new Event();
testEvent.Subject = 'EventTest';
testEvent.Location = 'Location';
testEvent.Description = 'Description';
testEvent.IsAllDayEvent=true;
testEvent.StartDateTime = datetime.newInstance(2014, 11, 13, 16, 30, 0);
testEvent.EndDateTime = datetime.newInstance(2014, 11, 13, 18, 30, 0);
insert testEvent;
Test.StopTest();
System.assertEquals ('EventTest', testEvent.Subject);
}
}
It works, thanks so much!! Now I have another problem as I only want trigger fires when new event created in Public Calendar, I found out my trigger will fire when user create events in their own calendar too. I changed the If in the code but not working, any suggestion?
1st Try:
Error: Compile Error: Illegal assignment from String to SOBJECT:Name at line 4 column 1
trigger CalendarEV on Event (after insert) {
List<Event> Events = new List<Event>();
for (Event e : Trigger.new) {
if (e.Owner = 'AdvisorsLink_Calendar' ) {
CalendarEV__c C = new CalendarEV__c();
c.Name = e.Subject;
c.Start__c = e.StartDateTime;
c.End__c = e.EndDateTime;
c.Location__c = e.Location;
c.Description__c = e.Description;
insert C;
}
}
}
2nd Try:
Error: Compile Error: Comparison arguments must be compatible types: SOBJECT:Name, SOBJECT:User at line 4 column 16
trigger CalendarEV on Event (after insert) {
List<Event> Events = new List<Event>();
for (Event e : Trigger.new) {
if (e.Owner != e.CreatedBy ) {
CalendarEV__c C = new CalendarEV__c();
c.Name = e.Subject;
c.Start__c = e.StartDateTime;
c.End__c = e.EndDateTime;
c.Location__c = e.Location;
c.Description__c = e.Description;
insert C;
}
}
}
Try This Code.
trigger CalendarEV on Event (after insert)
{
List<Event> Events = new List<Event>();
for (Event e : Trigger.new)
{
Id assigedtoUser=e.OwnerId;
User createdUser=e.CreatedBy;
if ( assigedtoUser != createdUser.Id )
{
CalendarEV__c C = new CalendarEV__c();
c.Name = e.Subject;
c.Start__c = e.StartDateTime;
c.End__c = e.EndDateTime;
c.Location__c = e.Location;
c.Description__c = e.Description;
insert C;
}
}
}