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
Bryan Jimenez 5Bryan Jimenez 5 

Help with simple CONTACT apex test

Hi all,

I am currently trying to create a test for my controller. The test I have currently gives me 100% in salesforce but it fails on validation to production.

What am i doing wrong?

public class ContactExtension {
  public ContactExtension(ApexPages.StandardController controller) {
    Contact C = (Contact)controller.getRecord();
    C.Recordtypeid = '012d0000001cltE';
  }
}




@istest(seeAllData='true')
public class ContactExtensionTest 
{
  public static testMethod  void ContactExtensionTest() {
    Contact Con = new Contact();
    ApexPages.StandardController controller = new ApexPages.StandardController(Con);
    ContactExtension extension = new ContactExtension(controller);
    System.assert(Con.Firstname !=Null);
  }
}

I keep getting this error when validating in production.



System.AssertException: Assertion Failed 
Stack Trace: Class.ContactExtensionTest.ContactExtensionTest: line 8, column 1
Best Answer chosen by Bryan Jimenez 5
Amit Chaudhary 8Amit Chaudhary 8
Always try to avoid SeeAllDate
Please try below post to learn about test classes
1) http://amitsalesforce.blogspot.com/search/label/Test%20Class
2) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

Please update your code like below
@istest
public class ContactExtensionTest 
{
	public static testMethod  void ContactExtensionTest() 
	{
		Account acc = new Account();
		acc.Name ='Test';
		insert acc;

		Contact cont = new Contact();
		cont.FirstName='Test';
		cont.LastName ='Test';
		cont.accountId = acc.id;
		insert cont;

		ApexPages.StandardController controller = new ApexPages.StandardController(cont);
		ContactExtension extension = new ContactExtension(controller);

		System.assert(cont.Firstname !=Null);
	}
}

Let us know if this will help you

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Always try to avoid SeeAllDate
Please try below post to learn about test classes
1) http://amitsalesforce.blogspot.com/search/label/Test%20Class
2) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

Please update your code like below
@istest
public class ContactExtensionTest 
{
	public static testMethod  void ContactExtensionTest() 
	{
		Account acc = new Account();
		acc.Name ='Test';
		insert acc;

		Contact cont = new Contact();
		cont.FirstName='Test';
		cont.LastName ='Test';
		cont.accountId = acc.id;
		insert cont;

		ApexPages.StandardController controller = new ApexPages.StandardController(cont);
		ContactExtension extension = new ContactExtension(controller);

		System.assert(cont.Firstname !=Null);
	}
}

Let us know if this will help you

 
This was selected as the best answer
Bryan Jimenez 5Bryan Jimenez 5
This worked great! Thank you