0

I will start out by apologizing for being a total n00b but, I need some help increasing my code coverage ASAP. Currently I am at 73%... so close!! Any help/advice/anything is a huge help! Thank You! Here is my Controller Extension code

public class GroupSignUpSAVEEXT {
    private final Contact con;
    // The extension constructor initializes the private member
    // variable acct by using the getRecord method from the standard
    // controller.
    public GroupSignUpSAVEExt(ApexPages.StandardController stdController) {
        this.con = (Contact)stdController.getRecord();
    }
    public static String firstName {
        get { return System.currentPageReference().getParameters().get('FirstName'); }
    set; 
    }
    public static String lastName {
        get { return System.currentPageReference().getParameters().get('LastName'); }
    set; 
    }
    public static String theEmail {
        get { return System.currentPageReference().getParameters().get('Email'); }
    set; 
    }
   public String strSaveResult {
        get;
        set;
    }  
}

And here is my Test Code

    @isTest
public class GroupSignUpEXT_TEST {

    static testmethod void testMyConstructor() 
    {
        Contact a = new Contact(); 
        ApexPages.StandardController controller = new ApexPages.StandardController(a);
        GroupSignUpSAVEEXT extension = new GroupSignUpSAVEEXT(controller); 

       Account testAccount = new Account();
        testAccount.Name='Test Account' ;
        insert testAccount; 

      Contact con= new contact();
     con.Email = 'Test@gmail.com';
     con.LastName= 'TodayTest';
     con.FirstName= 'Test';
     insert con;   

   ApexPages.currentPage().getParameters().put('qp', 'yyyy');

    }
}
0

You need to follow writing test method like this:

//create Account
Account acct = new Account(Name='test', RecordTypeId=redTypeId,......);
insert acct;

//Create Contact
Contact con= new contact();
con.Email = 'Test@gmail.com';
con.LastName= 'TodayTest';
con.FirstName= 'Test';
con.AccountId = acct.Id;
insert con;   

//create pageReference
PageReference page = Page.MyVFP;
Test.setCurrentPage(page);

//pass parameter to the page
ApexPages.currentPage().getParameters().put('id',con.Id);
ApexPages.currentPage().getParameters().put('FirstName',con.FirstName);
ApexPages.currentPage().getParameters().put('LastName',con.LastName);
ApexPages.currentPage().getParameters().put('Email',con.Email);


//create an instance of controller.
ApexPages.StandardController standrdcontroller = new ApexPages.StandardController(con);  

GroupSignUpSAVEEXT clsInstance = new GroupSignUpSAVEEXT(standrdcontroller);
System.assertEquals(.....);

String strFirstName = GroupSignUpSAVEEXT.firstName;
System.assertEquals(.....);

String strLastName = GroupSignUpSAVEEXT.lastName;
System.assertEquals(.....);

String strtheEmail = GroupSignUpSAVEEXT.theEmail;
System.assertEquals(.....);

Refer Testing Best Practices for properly writing test classes, as in your code block there are no asserts.

Test classes are meant for testing unit test case scenario, and not meant for 75% coverage for deployment.

  • I cannot get the //create an instance of controller section to work. Where does the "mpr_copy" come from? – BarrieP Jul 27 '17 at 21:29
  • small typo mistake, updated the code – Santanu Boral Jul 27 '17 at 22:13

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.