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
admin 7admin 7 

Can any one help in Test class please

This is my code please write test class for my class

Apex Code:

public with sharing class ContactNew{
 public contact org { get; private set; }

private ApexPages.StandardController sController;  
    private String queryString;  
              public ContactNew(ApexPages.StandardController controller) {  
        sController = controller;  
        org = (contact)controller.getRecord();  
    } 
    public Pagereference SaveAndnew()
 {
 
   upsert org;

   string s = '/' + ('' + org.get('Id')).subString(0, 3) + '/e?';
   ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.Info, s));
   return new Pagereference(s);
 }
 }


Visualforce Page

<apex:page standardController="contact" extensions="ContactNew" >
  <apex:sectionHeader Title="Contact edit" Subtitle="New contact"/>
  <apex:form >
  <apex:pageBlock Title="contact Edit">
  <apex:pageBlockButtons >
  <apex:commandButton value="Save" action="{!save}"/>
    <apex:commandButton value="Save & New" action="{!SaveAndnew}"/>
      <apex:commandButton value="cancel" action="{!cancel}"/>
      <apex:pageBlockSection >
      <apex:inputField value="{!Contact.FirstName}"/>
       <apex:inputField value="{!Contact.LastName}"/>
  
      </apex:pageBlockSection>
        </apex:pageBlockButtons>

      </apex:pageBlock>
      </apex:form>
      </apex:page>
Best Answer chosen by admin 7
BALAJI CHBALAJI CH
Hi Naveen,
Please find below Test Class for your class which has 100% code coverage.
@IsTest
public class Test_ContactNew{
    static testmethod void test1()
    {
        Contact C = new contact(lastname = 'TestContact');
        Insert c;
        
        Apexpages.StandardController controller = new Apexpages.StandardController(c);
        ContactNew obj = new ContactNew(controller);
        obj.SaveAndnew();
    }
}

Also find below few links for Test Classes:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

Please let me know if that helps you.

Best Regards,
BALAJI

All Answers

BALAJI CHBALAJI CH
Hi Naveen,
Please find below Test Class for your class which has 100% code coverage.
@IsTest
public class Test_ContactNew{
    static testmethod void test1()
    {
        Contact C = new contact(lastname = 'TestContact');
        Insert c;
        
        Apexpages.StandardController controller = new Apexpages.StandardController(c);
        ContactNew obj = new ContactNew(controller);
        obj.SaveAndnew();
    }
}

Also find below few links for Test Classes:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

Please let me know if that helps you.

Best Regards,
BALAJI
This was selected as the best answer
admin 7admin 7
Hi BALAJI CH,

Thanks for your valuable answer