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
DeeRDeeR 

Simple Account Contact Controller - test Class help needed

Dear  all, forgive the level of newbiness. I'm trying to use the simplest code structures at first to get myself use to Apex coding.

So I am trying to make a Visual Force page that lets me make an Account and Contact in one (or two) page(s).

Essentially I was/am following the Visualforce Developer's Guide but rather than create Account, Contact, Opportunity and Opportunity Contact Role, I only want to create an Account and Contact. I thought that might be simpler :/

 

I found a class (controller) that supposedly does this, rather than chop the VF Dev' Guide example down..especially as the VF Dev' Guide didn't have a test class for their example. :(

 

 

public class newAccContactController {

public Account account { get; set; }
public Contact contact { get; set; }

public newAccContactController() {
account = new Account();
contact = new Contact();
}

public PageReference next() {
return Page.newAccContactStep2;
}

public PageReference previous() {
return Page.newAccContactStep1;
}

public PageReference save() {
Database.insert(account);
contact.accountId = account.id;
ApexPages.StandardController contactController = new ApexPages.StandardController(contact);
return contactController.save();
}
}

  but  I'm unable to find/figure out quite how to test a controller.

 

I found a controller test class for a slightly difference controller but I'm still stuck trying to mangle it to meet my needs. If I run this test below I get 50% coverage, but I'm not even sure it's testing what needs to be tested :/

 

 

@isTest
private class testAccContactController {

static testMethod void myNewAccContactController() {
// TO DO: implement controller test
//Use the PageReference Apex class to instantiate a page
PageReference pageRef = Page.newAccContactStep1;

//In this case, the Visualforce page named 'newAccContactStep1'
//is the starting point of this test method.
Test.setCurrentPage(pageRef);
//Instantiate and construct the controller class.
newAccContactController controller = new newAccContactController();

//Example of calling an Action method. Same as calling any other Apex method.
//Normally this is executed by a user clicking a button or a link from the Visualforce
//page, but in the test method, just test the action method the same as any
//other method by calling it directly.

//The .getURL will return the page url the Save() method returns.
String nextPage = controller.save().getUrl();

//Check that the save() method returns the proper URL.
System.assertEquals('/apex/failure?error=noParam', nextPage);

//Add parameters to page URL
ApexPages.currentPage().getParameters().put('qp', 'yyyy');

//Instantiate a new controller with all parameters in the page
controller = new newAccContactController();

//Example of calling the 'setter' method for several properties.
//Normally these setter methods are initiated by a user interacting with the Visualforce page,
//but in a test method, just call the setter method directly.
//controller.setLastName('lastname');
//controller.setFirstName('firstname');
//controller.setCompany('acme');
//controller.setEmail('firstlast@acme.com');
nextPage = controller.save().getUrl();

//Verify that the success page displays
System.assertEquals('/apex/success', nextPage);
}
}

 

and the results of running this test:

 

 

*** Beginning Test 1: testAccContact.static testMethod void myNewAccContactController()

20100104055728.393:Class.testAccContact.myNewAccContactController: line 13, column 46: returning from end of method public newAccContactController<Constructor>() in 0 ms
20100104055728.393:Class.newAccContactController.save: line 20, column 9: Database.insert(SOBJECT:Account)
20100104055728.393:Class.newAccContactController.save: line 20, column 9: DML Operation executed in 77 ms
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name]: [Name]

Class.newAccContactController.save: line 20, column 9
Class.testAccContact.myNewAccContactController: line 21, column 27
External entry point

 

 

 

 

 I'm sure for some this is bleedingly obvious/simple, and I hope that once I get past this stage the light will go on for the subsequent tests that I'll surely be writing.

 

Thank you anyone that can help me here.

 

D

 

 

Message Edited by DeeR on 01-03-2010 11:35 PM
Message Edited by DeeR on 01-03-2010 11:46 PM
bob_buzzardbob_buzzard

So this code will test the constructor and save method, but not the next/previous methods.  Hence the 50% code coverage.

 

In order to test these methods, simply invoke the methods on the controller and check that the returned page reference is what you are expecting, using asserts.

 

The existing test class that you have is executing a save without filling in any of the information for the account/contact.

 

You will need to do something like:

 

 

controller.account.Name='Unit Test Name';

 in order to stop the exception you are seeing.  You may need to repeat this for a number of fields to ensure the account and contact are populated appropriately for saving.

 

 

DeeRDeeR

Bob,

 

thank you kindly for your reply. May I persist with newbie irritations and ask how I might check for a returned page reference? ie. I'm not sure what I'm expecting, well except to see the page that is next or previous but...

 

I'll add the field filling statements (like the one you so kindly provided) and hopefully that will start to clear some of the exceptions. Just can foresee myself struggling a bit withthe page checking "Assert" statements.

 

Again, many thanks.

 

Dee

bob_buzzardbob_buzzard

You'll have to capture the page reference that is returned from those functions and check that it matches what you are expecting.  I normally check the URL (PageReference.getUrl()).  If you don't know the exact information, you can always dump out the returned page reference using System.debug and, as long as you are happy with it, construct some asserts based on that.

 

e.g.

 

 

PageReference nextPR=controller.next();

 

System.assert(nextPR.getUrl().contains('myPage'));

 

 

 

Message Edited by bob_buzzard on 01-04-2010 11:31 PM