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
Mischa14Mischa14 

Apex Test Class

 

Hi All,

I am new to apex triggers, but somehow I managed to create a trigger that works.. I am having more trouble creating a Test Class that reaches 75% of code coverage.. my Test Class sticks at 0% coverage :-(

my trigger fires after the creation of a custom object, called Case.. the trigger inserts a Case Leadtime record in the related list on Case (the case leadtime record is then filled with workflow rules to track the leadtime per department for management reporting ).

This is the trigger:

 

trigger createLeadtime on Case__c (after insert)
{

List<Case_Leadtime__c> cl = new List<Case_Leadtime__c>();
for (Case__c newCase_Leadtime: Trigger.New)
cl.add (new Case_Leadtime__c(
Case__c = newCase_Leadtime.Id));
insert cl;
}

 

 

In the Test Class I have intended to first create a Case and then to create a related Case Leadtime

 

This is the test class:


@isTest
private class TestcreateLeadtime {

static testMethod void TestcaseLeadtime () {

Case__c a = new Case__c ();
a.Account__c = [select Id from Account limit 1].Id;
a.Issue_Type__c = 'Customer Claim';
a.Office__c = [select Id from Office__c limit 1].Id;
a.Case_Created_By__c = 'Test';
a.Assigned_To__c = [select Id from User limit 1].Id;
a.Status__c = 'Open';
a.Complaint_Subject__c = 'Miscellaneous';
a.Change_Flag__c = true;
a.Proposed_Solution__c = 'Discount/Compensate By Credit Note';
insert a;

Case_Leadtime__c[] cl = new Case_Leadtime__c []{
new Case_Leadtime__c (Case__c = a.id)};
insert cl;


test.startTest();
insert cl;
test.stopTest();
}

}

 

------

 

Hope someone can help

 

Thanks

 

Best Answer chosen by Admin (Salesforce Developers) 
Naidu PothiniNaidu Pothini
@isTest(SeeAllData=true)
private class TestcreateLeadtime
{
	static testMethod void TestcaseLeadtime()
	{
		Case__c a = new Case__c ();
		a.Account__c = [select Id from Account limit 1].Id;
		a.Issue_Type__c = 'Customer Claim';
		a.Office__c = [select Id from Office__c limit 1].Id;
		a.Case_Created_By__c = 'Test';
		a.Assigned_To__c = [select Id from User limit 1].Id;
		a.Status__c = 'Open';
		a.Complaint_Subject__c = 'Miscellaneous';
		a.Change_Flag__c = true;
		a.Proposed_Solution__c = 'Discount/Compensate By Credit Note';
		
		test.startTest();

		insert a;

		test.stopTest();
	}
}

 Try this.

All Answers

Naidu PothiniNaidu Pothini
@isTest(SeeAllData=true)
private class TestcreateLeadtime
{
	static testMethod void TestcaseLeadtime()
	{
		Case__c a = new Case__c ();
		a.Account__c = [select Id from Account limit 1].Id;
		a.Issue_Type__c = 'Customer Claim';
		a.Office__c = [select Id from Office__c limit 1].Id;
		a.Case_Created_By__c = 'Test';
		a.Assigned_To__c = [select Id from User limit 1].Id;
		a.Status__c = 'Open';
		a.Complaint_Subject__c = 'Miscellaneous';
		a.Change_Flag__c = true;
		a.Proposed_Solution__c = 'Discount/Compensate By Credit Note';
		
		test.startTest();

		insert a;

		test.stopTest();
	}
}

 Try this.

This was selected as the best answer
Vinit_KumarVinit_Kumar

You are  missing (SeeAllData=true)

 

change from 

 

@isTest

 

to 

 

@isTest(SeeAllData=true)

Anil MeghnathiAnil Meghnathi

is  this issue solved or not?

you have to use @isTest(SeeAllData=true).thanks

Mischa14Mischa14

Thanks for helping. adding this worked! 

(SeeAllData=true)