1

I'm pretty new to VisualForce and Apex but I know a little bit of Java so I've been able to figure out how to set up a pretty simple page to create an account and two contacts. I found some useful info online that helped me create a test class that worked before I added the conditionals to the extension but it no longer covers enough to be deployed.

I'm hoping I can get some help writing the test class and if anyone has any input or advice on the way I went about this I would love to hear it.

Visualforce

<apex:page standardController="Contact" extensions="qcSimpleExt">
<apex:form>
    <apex:pageblock title="New Account & Contact" mode="edit">
        <apex:pageblockButtons >
            <apex:commandButton action="{!Save}" value="Save"/>
            <apex:commandButton action="{!Cancel}" value="Cancel"/>
        </apex:pageblockButtons>
        <apex:pageblocksection >
            <apex:inputField value="{!Contact.firstname}" required="True" taborderhint="1"/>
            <apex:inputfield value="{!Contact.SpouseFirstName__c}" taborderhint="4"/>
            <apex:inputfield value="{!Contact.lastname}" required="True" taborderhint="2"/>
            <apex:inputfield value="{!Contact.SpouseLastName__c}" taborderhint="5"/>
        </apex:pageblocksection>
    </apex:pageblock>
</apex:form>

Extension

public class qcSimpleExt{
public Contact newCon {get;set;}

public qcSimpleExt(ApexPages.StandardController con){
    newCon = (Contact)con.getRecord();
}

public pagereference save(){
    account a = New Account(name = newCon.LastName + ', ' + newCon.Firstname);
    if(string.isnotblank(newCon.SpouseFirstName__c) && string.isnotblank(newCon.SpouseLastName__c) && newCon.LastName <> newCon.SpouseLastName__c){
        a.name = newCon.LastName + ', ' + newCon.Firstname + ' & ' + newcon.SpouseLastName__c + ', ' + newcon.spouseFirstName__c;
        insert a;
        contact s = new Contact(lastname = newCon.SpouseLastName__c, firstname = newCon.SpouseFirstName__c, AccountID = a.id);
        newCon.AccountID = a.id;
        insert newCon;
        insert s;
    }
    else if((string.isblank(newCon.SpouseLastName__c) && string.isnotblank(newCon.SpouseFirstName__c)) || (string.isnotblank(newCon.SpouseLastName__c) && string.isnotblank(newCon.SpouseFirstName__c) && newCon.LastName == newCon.SpouseLastName__c)){
        a.name = newCon.LastName + ', ' + newCon.Firstname + ' & ' + newcon.spouseFirstName__c;
        insert a;
        contact s = new Contact(lastname = newCon.LastName, firstname = newCon.SpouseFirstName__c, AccountID = a.id);
        newCon.AccountID = a.id;
        insert newCon;
        insert s;
    }
    else{
        insert a;
        newCon.AccountID = a.id;
        insert newCon;
    }
    return New PageReference('/' + a.id);
}

Test Class This is the test class that I pretty much just copied from some website but I don't really understand it.

@isTest
private class qcSimpleTest {

static testMethod void qcSimpleTest(){
    Contact c = new Contact(lastname='Test');
    insert c;

    ApexPages.StandardController sc = new ApexPages.StandardController(c);
    qcSimpleExt e = new qcSimpleExt(sc);
    system.assert(e != null);

    Test.startTest();
    sc.save();
    test.stopTest();
}
1

You were so close (coverage).....

Test.startTest();
e.save();  // this tests the save method on the extension class
test.stopTest();

You had

sc.save();

That invokes save on the standard controller

You will want to assert results by doing SOQL after stoptest() to verify that inputs you provided show up in the database.

There are also two use cases - new Contact with values provided via the VF page and existing Contact with edits provided by VF page

New Contact use case ...

Contact c = new Contact(lastname = 'foo', firstname = 'bar');
ApexPages.StandardController sc = new ApexPages.StandardController(c);
qcSimpleExt e = new qcSimpleExt(sc);
system.assert(e != null);

Test.startTest();
e.save();
test.stopTest();
// add soql to fetch Account,Contact and verify field values are as expected

Existing Contact use case ...

Contact c = new Contact(lastname = 'foo', firstname = 'bar');
insert c;
ApexPages.StandardController sc = new ApexPages.StandardController(c);
qcSimpleExt e = new qcSimpleExt(sc);
system.assert(e != null);

Test.startTest();
c.lastname = 'fooChgd';
e.save();
test.stopTest();
// add soql to fetch Accoount, Contact and verify field values are as expected
| improve this answer | |
  • This looks great, thank you so much, I haven't had a chance to try it out yet but I really appreciate the response. If it doesn't take too much time would you mind showing me an example of the SQQL. If not that's fine, I'm sure I'll be able to find out more about that online. – Sam Pickard Sep 3 '15 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.