0

I am unable to get an apex class into production from my sandbox

I assume it is because i have not written any tests

Could someone help me write some tests for this class so i can get it over to production for my demo tomorrow?

The apex class and VF page use 4 text boxes to create a "Physician's Note". There are only 4 boxes and 1 save button

My code is pasted below:

public with sharing class AddPhysicianNoteExtension
{
    public Physicians_Note__c note { get; private set; }
    public AddPhysicianNoteExtension(ApexPages.StandardController controller)
    {
        // this constructor must be defined to add the extension to the page
        Id accountId = controller.getId();
        note = new Physicians_Note__c(Account__c=accountId);
    }

    public PageReference save()
    {
        try
        {
            insert note;
        }
        catch (DmlException dmx)
        {
            ApexPages.addMessages(dmx);
        }

        Return Null;
    }
}

Visualforce Page:

<apex:page standardController="Account" extensions="AddPhysicianNoteExtension">
<apex:form >
    <apex:pageBlock title="Physician's Note" mode="edit">
        <apex:pageBlockButtons >
            <apex:commandButton action="{!save}" value="Save"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection title="SOAP Note" columns="2">
            <apex:inputField Label="Subjective" value="{!Note.Subjective__c}" style="width:500px"/>
            <br></br>
            <apex:inputField Label="Objective" value="{!Note.Objective__c}" style="width:500px"/>
            <br></br>
            <apex:inputField Label="Assessment" value="{!Note.Assessment__c}" style="width:500px"/>
            <br></br>
            <apex:inputField Label="Plan" value="{!Note.Plan__c}" style="width:500px"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>     

Edit: This is my class thus far:

@isTest
public class Physicians_Note_test_Class{
    Static Void Physicians_Note_Test();
Physicians_Note__c PhysicianNote1 = New Physicians_note__c(Subjective__c = 'This is a subjective Test', Objective__c = 'This is an objective Test'
,Assessment__c = 'This is an Assessment Test', Plan__c = 'This is a Plan Test');
}
  • Nothing XD I am very new to this, reading up on the process right now but it's hard for me to write these from scratch – Alex Waddell Mar 15 '17 at 20:34
  • 1
    I've formatted your code properly. The Stackexchange network uses a variant of a formatting style called 'markdown' to format questions (and answers). Formatting something as a block of code (as you'd do for a class, or a visualforce page) requires that each line have at least 4 spaces at the very beginning. The easiest way to format code here is to copy/paste your class/method/fragment, highlight it, and then press ctrl + k. – Derek F Mar 15 '17 at 20:38
  • Community team members can code for you, and its quite easy. But if you really know the process to write a test class, you would no longer need additional help and it'd help in longer run. Suggest you to go through some documentation like this super awesome trailhead module. And feel free to ask for help if you are stuck in particular place. – Raul Mar 15 '17 at 20:39
  • I am having trouble interpreting the trailhead and changing it to fit my use case. Not really sure how to supply a negative test if there is no real way to fail (being free text) – Alex Waddell Mar 15 '17 at 20:52
1

The first step is to get the basic syntax right. Every test should look something like:

static testMethod void testSomeMethod()
{
    // set up data

    Test.startTest();
        //execute some method
    Test.stopTest();

    // assert on behavior
}

So, you need to at least start with that template. Your entire test class might then look something like:

@IsTest
class AddPhysicianNoteExtensionTests
{
    static testMethod void testConstructor()
    {
        // same template as above
        // assert on note being properly set up
    }

    static testMethod void testSave()
    {
        // same template as above
        // assert on note being properly inserted
    }
    static testMethod void testSave_Error()
    {
        // same template as above
        // assert on error handling behavior
    }
}

Now, how do you instantiate your extension? Three steps:

  1. Insert an Account record.
  2. Instantiate an ApexPages.StandardController with this record.
  3. Instantiate your extension with this controller.

Basically your setup process will look like:

// Step 1
Account record = new Account(/*required fields*/);
insert record;

// Step 2
ApexPages.StandardController controller = new ApexPages.StandardController(record);

// Step 3
AddPhysicianNoteExtension extension = new AddPhysicianNoteExtension(controller);

For your constructor test, step 3 would come between startTest and stopTest. For the other tests, it would come before startTest (it's just setup). Your other tests will add a fourth step, calling the save method.

// set up extension as above

Test.startTest();
    extension.save();
Test.stopTest();

For your error case, you can cause a DmlException by setting the Id value on the note before calling save.

// set up extension as above

Id dummyId = SObjectType.Physician_Note__c.getKeyPrefix().rightPad(15, '0');
extension.note.Id = dummyId;

Test.startTest();
    extension.save();
Test.stopTest();

Keep in mind that the most important aspect of testing is to assert on application behavior. For the above tests, you need to make sure:

  • Your constructor instantiates note as a non-null value.

    system.assertNotEquals(null, extension.note,
        'The note record should be constructed');
    
  • The constructed Physician_Note__c record actually looks up to the Account.

    system.assertEquals(record.Id, extension.note.Account__c,
        'The note record should look up to the Account');
    
  • The Physician_Note__c record will get inserted when you save (re-query from the database).

    system.assertNotEquals(1, [SELECT count() FROM Physician_Note__c],
        'The note record should be inserted');
    system.assert(ApexPages.hasMessages(ApexPages.Severity.CONFIRM),
        'A confirmation message should be added to the page');
    
  • If your save fails, the errors will get added to the page.

    system.assertNotEquals(1, [SELECT count() FROM Physician_Note__c],
        'The note record should have failed to insert');
    system.assert(ApexPages.hasMessages(),
        'The error messages should be added to the page');
    
| improve this answer | |
  • You'll notice that a lot of this content is laid out in a way that it's not the easiest to copy-paste. Copy-paste programming is not your friend when you are learning, in my opinion. – Adrian Larson Mar 15 '17 at 21:29
  • 1
    +1 Its like a custom trailhead module tailored specifically for this question, No idea on how you do that, really appreciate your efforts! – Raul Mar 15 '17 at 21:31

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.