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
KevSnellKevSnell 

TestMethod Help - Trying but Failing!!!

Hi All,

 

I'm slowly understanding the apex code but the testmethods are still outwitting me.

 

I created the below custom controller which loads a custom New Task Page prepopulated from a Custom Object so the User can submit the task without having to fill in the details.

 

This works great, however, I can't figure out the testmethod.  As far as I understand I need to create the data, create the task and confirm the task was created.

 

So as you can see in my Test Method I have created two contact records and associated them with a new custom object record.  I've then created the Task and checked it's inserted.  However none of the test method works and it shows as 0% Coverage.

Any help or guidance would be really appreciated as I'm starting to go mad!!

Kev 

 

 

static testmethod void testCustomReferralTaskController() {
    Test.startTest();
    	//Create Referrer
    	VistageTestData data = new VistageTestData();
    	Account[] testAccounts = data.gimmeAccounts(2);
    	Contact testContact = data.gimmeContacts(testAccounts)[0];
    	Contact testContactDetails = [SELECT Id, FirstName, LastName, Staff__c FROM Contact WHERE Id = :testContact.Id];
    	testContactDetails.Staff__c = true;
    	update testContactDetails;
    
	//Create Referral
	VistageTestData data2 = new VistageTestData();
	Account[] testAccounts2 = data2.gimmeAccounts(2);
	Contact testContact2 = data2.gimmeContacts(testAccounts)[0];
	Contact testContactDetails2 = [SELECT Id, FirstName, LastName, Staff__c FROM Contact WHERE Id = :testContact.Id];
	testContactDetails2.Member__c = true;
	update testContactDetails2;    
    
	Referral__c Referral = new Referral__c(
        Referrer__c = testContactDetails.Id,
        Contact_Referral_Name__c = testContactDetails2.Id
		);
        insert Referral;
            
	Task testTask = new Task (
		WhoId = testContactDetails2.Id,
		WhatId = Referral.Id,
		Subject = 'Referral Validation',
		ActivityDate = date.today() + 7,
		ReminderDateTime = datetime.now() + 7,
		Description = 'Please Validate the Referral');
	insert testTask; 
    
   	Task t = [Select Id, whoid from Task where WhatId = :Referral.Id Limit 1];
   	system.assertEquals(t.whoid,testContactDetails2.Id);
   	Test.stopTest();
} 

 

 

apapapap

I think your code is giving some exception and that's why the code coverage is 0 %.

Try this code->

 

static testmethod void testCustomReferralTaskController() {

	try
	{
	
    Test.startTest();
    	//Create Referrer
    	VistageTestData data = new VistageTestData();
    	Account[] testAccounts = data.gimmeAccounts(2);
    	Contact testContact = data.gimmeContacts(testAccounts)[0];
    	Contact testContactDetails = [SELECT Id, FirstName, LastName, Staff__c FROM Contact WHERE Id = :testContact.Id];
    	testContactDetails.Staff__c = true;
    	update testContactDetails;
    
	//Create Referral
	VistageTestData data2 = new VistageTestData();
	Account[] testAccounts2 = data2.gimmeAccounts(2);
	Contact testContact2 = data2.gimmeContacts(testAccounts)[0];
	Contact testContactDetails2 = [SELECT Id, FirstName, LastName, Staff__c FROM Contact WHERE Id = :testContact.Id];
	testContactDetails2.Member__c = true;
	update testContactDetails2;    
    
	Referral__c Referral = new Referral__c(
        Referrer__c = testContactDetails.Id,
        Contact_Referral_Name__c = testContactDetails2.Id
		);
        insert Referral;
            
	Task testTask = new Task (
		WhoId = testContactDetails2.Id,
		WhatId = Referral.Id,
		Subject = 'Referral Validation',
		ActivityDate = date.today() + 7,
		ReminderDateTime = datetime.now() + 7,
		Description = 'Please Validate the Referral');
	insert testTask; 
    
   	Task t = [Select Id, whoid from Task where WhatId = :Referral.Id Limit 1];
   	system.assertEquals(t.whoid,testContactDetails2.Id);
   	Test.stopTest();
	
	}
	catch(exception e)
	{
		system.assertequals(e,null);
	}

} 

This will show you the error due to which the method is failing.

KevSnellKevSnell

Thanks but unfortunately that made no difference and it is still showing as 0%.

 

I'm sure it most be something simple but I can't find out what!!

KevSnellKevSnell

Update - Missed the Start - Stop in the test and now it failed saying Insufficient Access on Cross Reference Entity, Actual: null and references the system.assertequals(e, null).


So now just need to work out what that means.

 

KevSnellKevSnell

If anyone can point me in the right direction I would really appreciate it, as at the moment I seem at a loss.


Thanks in advance.

Kev