+ Start a Discussion
BaguiarBaguiar 

Create account and a contact and reference account on new contact

Hi,

 

might be simple but I'm trying to reference this new contact with the test account created, this is a test for a controller.

on the NEW contact "con", I need it to be related to the account acc that I've just created. I'm trying account.name='xxxx" but doesn;' work.

 

@isTest
private class TestKeyLeadership
{
   static testMethod void testGetLeaders()
   {
      // insert a test account
      Account acc=new Account(Name='Test123');
      insert acc;
      // insert a test Contact
      Contact con=new Contact(FirstName='Lester', Lastname='DbigTester', account=acc.id, email='123123123@888555.com');
      insert con;
      // insert a test Leadership Code
      List<leadership_position_code__c> LDS=new List<leadership_position_code__c>{new leadership_position_code__c(Contact__C='Lester DbigTester', position_type__C='Clergy', Position__C='Cantor', Start_Date__C='01/01/2011', Thru_date__C='01/02/2011' ),
       new leadership_position_code__c(Contact__C='Lester DbigTester', position_type__C='Officer', Position__C='President', Start_Date__C='01/01/2011' ), 
       new leadership_position_code__c(Contact__C='Lester DbigTester', position_type__C='Clergy', Position__C='Senior (or only) Rabbi', Start_Date__C='01/01/2011' )};
      insert LDS;
     
      // add the id to the current page
      ApexPages.currentPage.getParameters().put('id', acc.id);

      // instantiate the class under test
      KeyLeadership kl=new KeyLeadership();
     
      List<Leadership_Position_code__c> leaders=kl.getLeaders();

      System.assertEquals(2, leaders.size());
 
   }

 

Thanks!

B

BaguiarBaguiar

Actually found a little help on another post, by luck. modified to this:

 

private class TestKeyLeadership
{
   static testMethod void testGetLeaders()
   {
      // insert a test account
      Account acc=new Account(Name='Test123');
      Database.saveresult sr = database.insert(acc);
     

      Contact con=new Contact(FirstName='Lester', Lastname='DbigTester', accountID=sr.getId(), email='123123123@888555.com');
      insert con;

      List<leadership_position_code__c> LDS=new List<leadership_position_code__c>{new leadership_position_code__c(Contact__C='Lester DbigTester', position_type__C='Clergy', Position__C='Cantor', Start_Date__C= system.Today()-2, Thru_date__C= system.Today()-1 ),
       new leadership_position_code__c(Contact__C='Lester DbigTester', position_type__C='Officer', Position__C='President', Start_Date__C= system.Today()-1 ), 
       new leadership_position_code__c(Contact__C='Lester DbigTester', position_type__C='Clergy', Position__C='Senior (or only) Rabbi', Start_Date__C=system.Today()-1 )};
      insert LDS;
     
      // add the id to the current page
      ApexPages.currentPage.getParameters().put('id', acc.id);

      // instantiate the class under test
      KeyLeadership kl=new KeyLeadership();
     
      List<Leadership_Position_code__c> leaders=kl.getLeaders();

      // change the number below to the number of leadership_position_code__c objects created above
      System.assertEquals(2, leaders.size());

        
   }

 But now I'm getting and error on the     ApexPages.currentPage.getParameters().put('id', acc.id);   ...

B

Manu ErwinManu Erwin

Hi Baguiar,

you need to set the Contact's AccountId field rather than Account e.g., 

 

 

Contact con=new Contact(FirstName='Lester', Lastname='DbigTester', accountId=acc.id, email='123123123@888555.com');

 

 

You don't need the Saveresult either - your first piece of code should work just fine.

 

I recommend cross-referencing your testcode to the VF page so the page can't be deleted.

 

e.g., 

 

Test.startTest();
PageReference pageRef = Page.yourVfPageName;
ApexPages.StandardController sc = new ApexPages.StandardController(acc);
Test.setCurrentPage(pageRef);
KeyLeadership controller = new KeyLeadership(sc);

List<Leadership_Position_code__c> leaders = controller.getLeaders();
System.assertEquals(2, leaders.size());
Test.stopTest();

 

 

Hope this helps.

 

regards,

Manu

BaguiarBaguiar

HI Manu and thanks for the reply!

 

the Contact inser is fine but I'm getting an error now on the    ApexPages.currentPage.getParameters().put('id', acc.id);  

. The error comes up as "Method does not exist or incorrect signature."

 

Really thanks for the test mehod on the page. I'm learning the test methods now and that was really good. Might need some more help on that.:)

Manu ErwinManu Erwin
Hi Baguiar, Try replacing that whole section of your code (to the bottom) with the other piece of code I provided to cross reference the page that should work for you.
BaguiarBaguiar

HUmm.. tried that but now a Constructor not defined.... here is the Error:

 

Compile Error: Constructor not defined: [KeyLeadership].<Constructor>(ApexPages.StandardController)

 

Really thanks for your help!

BaguiarBaguiar

Here is the code:

 

@isTest
private class TestKeyLeadership
{
   static testMethod void testGetLeaders()
   {
      // insert a test account
      Account acc=new Account(Name='Test123');
      Database.saveresult sr = database.insert(acc);
     

      Contact con=new Contact(FirstName='Lester', Lastname='DbigTester', accountID=acc.id, email='123123123@888555.com');
      insert con;

      List<leadership_position_code__c> LDS=new List<leadership_position_code__c>{new leadership_position_code__c(Contact__C='Lester DbigTester', position_type__C='Clergy', Position__C='Cantor', Start_Date__C= system.Today()-2, Thru_date__C= system.Today()-1 ),
       new leadership_position_code__c(Contact__C='Lester DbigTester', position_type__C='Officer', Position__C='President', Start_Date__C= system.Today()-1 ), 
       new leadership_position_code__c(Contact__C='Lester DbigTester', position_type__C='Clergy', Position__C='Senior (or only) Rabbi', Start_Date__C=system.Today()-1 )};
      insert LDS;
     
      ApexPages.currentPage().getParameters().put('id', acc.id);


      // instantiate the class under test
      KeyLeadership kl=new KeyLeadership();
     
      List<Leadership_Position_code__c> leaders=kl.getLeaders();

      // change the number below to the number of leadership_position_code__c objects created above
      System.assertEquals(2, leaders.size());

      // should also assert that the inserted records above match those retrieved.
  
   }
}

 ANd the error not passing the test is:    Failure Message: "System.StringException: Invalid id: Lester DbigTester", Failure Stack Trace: "Class.TestKeyLeadership.testGetLeaders: line 14, column 126 External entry point"

 

B

BaguiarBaguiar

Found the isuue with the ID..

This:   Contact__C='Lester DbigTester'

should be this:  Contact__C=con.id

 

It is giving me 70% coverage though... need 5% more..

BaguiarBaguiar

Code is good. it was a nother class on production causing the % on converage.

Manu ErwinManu Erwin
Good to hear, glad I could help.