1

I am trying to test my Apex code to get over 75% code coverage.

I have the following code:

@isTest
private class Send_noti_emailsTestClass3 {    
    static testMethod void validateSend_noti_emails3() {
       Query_and_upload myLicenseGenerator2 = new Query_and_upload();

       myLicenseGenerator2.save(First_Name__c='Steve',Last_Name__c='Mark', Email__c='stevemark@gmail.com');     
    }
}

The Apex code has a PageReference sub called save and I need to pass the first name, last name and the email address.

public with sharing class Query_and_upload {
  public PageReference save() {    
    if(inquery.first_name__c.length() == 0 && inquery.last_name__c.length() == 0 && inquery.email__c.length() == 0 ){  
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'One of the inputs to filter the user is missing. Please enter all the fields.'));
   }
 }

But im getting the following error:

Error Error: Compile Error: Variable does not exist: First_Name__c at line

How can I pas the name and email to the save subroutine?

3

That is not how test methods work.

You need to actually create test data for your test code. Assuming your fields are referring to a Contact, and that Contact is a property in your class, then you actually need to create the contact first, and then test the save method

In your test Class

Contact c = new Contact(First_Name__c='Steve',Last_Name__c='Mark', Email__c='stevemark@gmail.com');
insert c;

Query_and_upload myLicenseGenerator2 = new Query_and_upload();
myLicenseGenerator2.myContact = c;

myLicenseGenerator2.save();

Assuming you use a controller property for the contact in your Query_and_upload class

public with sharing class Query_and_upload {

    public Contact myContact      {get;set;}

    public Query_and_upload(){
        myContact = new Contact();
    }  

    public PageReference save() {    
        if(myContact.first_name__c.length() == 0 
           && myContact.last_name__c.length() == 0 
           && myContact.email__c.length() == 0 ){  
                 ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,
            'One of the inputs to filter the user is missing. Please enter all the fields.'));
    }
}

Its also worth noting that altough this will increase your code coverage, you really need to add some assertions so you are actually 'testing' the class. This will get you coverage, but you really aren't testing if the class accomplishes its goal

.

Check out this article on testing best practices http://wiki.developerforce.com/page/How_to_Write_Good_Unit_Tests

EDIT

An example assertion

Your class has a Contact controller property that upon class instantiation, will be a new contact, with no Id.

In your test class

Contact c = new Contact(First_Name__c='Steve',Last_Name__c='Mark', Email__c='stevemark@gmail.com');
insert c;

Query_and_upload myLicenseGenerator2 = new Query_and_upload();
system.assertEquals(null, myLicenseGenerator2.myContact.Id);

myLicenseGenerator2.myContact = c;
system.assertEquals(c.Id, myLicenseGenerator2.myContact.Id);
4
  • Can you tell me what the myContact represents in myLicenseGenerator2.myContact = c;? – user2333346 Oct 8 '13 at 20:23
  • That's an assumption I made in your code. Assuming you use a controller property to store the contact you are referencing on the VF page. I added a few lines of code to show you – Chris Duncombe Oct 8 '13 at 20:30
  • Great, finally im getting somewhere, But its still confusing how to add assertions!! can you please give me one example, it would be a great help? – user2333346 Oct 8 '13 at 20:51
  • Added a simple example assertion to the answer above – Chris Duncombe Oct 8 '13 at 21:15

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.