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
krishnagkrishnag 

help with test class

hi can anybody help me in writing test class for this controller. I need just a start to finish it.I am new to this test classes creation.

 

public with sharing class gusignup {

     public Boolean emailacept { get; set; }

    public Boolean emailerror { get; set; }

    public String email { get; set; }

    public String job { get; set; }

    public String ctry { get; set; }

    public String zip { get; set; }

    public String state { get; set; }

    public String city { get; set; }

    public String street { get; set; }

    public String comp { get; set; }

    public String lname { get; set; }

    public String fname { get; set; }
    
    public Boolean echeck { get; set; }

    public String rpwd { get; set; }

    public String pwd { get; set; }

}

    public PageReference psignup() {
    emailerror=false;
        emailacept=false;
        List<guestuser__c> ldtls=[select Id from guestuser__c where Email__c=:email];
        
        if(ldtls.size()>0)
        {
            emailerror=true;
            emailacept=false;
            //uid exist
        }
        else
        {
            if(pwd==rpwd)
            {
                emailerror=false;
                emailacept=false;
                guestuser__c gu=new guestuser__c();
                gu.First_Name__c=fname;
                gu.Password__c=pwd;
                gu.Email__c=email;
                gu.Title__c = job;
                gu.Email_Optout__c = echeck;
                gu.Last_Name__c = lname;
                gu.Company__c = comp;
                gu.Street__c = street;
                gu.State__c = state;
                gu.City__c = city;
                gu.Country__c = ctry;
                gu.Zipcode__c = zip;
                
                
                Lead ld=new Lead();          
                ld.FirstName =fname;
                ld.LastName=lname;
                ld.Company=comp;
                ld.Street=street;
                ld.State=state;
                ld.city=city;
                ld.PostalCode=zip;
                ld.country=ctry;
                ld.Title=job;
                ld.Email=email;
                ld.HasOptedOutOfEmail=echeck;
                
                
                try{
                insert ld;
                insert gu;
                Pagereference pageref=new Pagereference('/apex/thankq');     
                    pageref.setRedirect(true);
                    return pageref;
                }
                catch(Exception e)
                {
                    system.debug(e);
                }
            }
            else
            {
                ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.WARNING, 'PASSWORD MISS-MATCH');
                ApexPages.addMessage(myMsg);
            }
        }
        return null;
    }


    
    public void checkavlble() {
        String lusername= Apexpages.currentPage().getParameters().get('mail');
        List<guestuser__c> chkldtls=[select Id from guestuser__c where Email__c=:email];
        if(chkldtls.size()>0)
        {
            emailerror=true;
            emailacept=false;
        }
        else
        {           //try another
            emailerror=false;
            emailacept=true;
        }
    }


    

 

 

krishnagkrishnag

thanks gopi i have gone through that before what i am thinking can we do that as creating a list because if u see my page has i have 20 field.So i need to write 20 get  and set methods.so i am thinking is there any other way to avoid that.

gv007gv007

I dot know any other better way but follow below points that will helpful

 

1.Create a test method in the class

2.CReate objects instances used in the class

3.Access method using object instances.

4.pass positive,negative,null values

you have some SOQL in yours code

 

send some dummy date to the objects and write the queries will work.yours code is very simple it not a mater writing 20 methods in a test method.

 

If you got any better idea update us.first try to post some code based on initial procedure.

krishnagkrishnag

thanks gopi i will definitely try this procedure.

gv007gv007

Here is some sample code that will help you

 

I have method

xyz(String a)

{

}

 

test method()

{

object instance  v;

v.xyz('abc');

v.xyz(123);

v.xyz(null);

 

here theree test case i wrote just try it let us know,

Thanks

 

 

krishnagkrishnag

 

@isTest
private class gusignupTests
{
 public static testMethod void testMyController()
 {
   PageReference p = Page.thankq;
   Test.setCurrentPage(p);
   
   gusignup controller = new gusignup();
   String nextPage = controller.psignup().getURL();
   
   controller = new gusignup();
   controller.fname('abc');
   controller.lname('xyz');
   controller.email('abc@xyz.com');
   controller.job('developer');
   controller.street('golf');
   controller.city('okc');
   controller.state('ok');
   controller.ctry('usa');
   controller.echeck(True);
   controller.comp('zurich');
   controller.zip('48326');
   nextPage = controller.psignup().getURL();
   
   System.assertEquals('apex/thankq',nextPage);
   Lead[]leads = [select Id,Email from lead where Company = 'zurich'];
   System.assertEquals('abc@xyz.com', leads[0].Email);
   
 }
}

 

hi i have written a test class for the above  controller and i am geeting the error like

 

 

 

ErrorError: Compile Error: Method does not exist or incorrect signature: [gusignup].fname(String) at line 13 column 4

 

 

can anybody debug this

Pradeep_NavatarPradeep_Navatar

The error is due to the incorrect way of assigning value in fname. If there is a set function with name “fname” then you can assign value like this. If fname is a variable then you have to assign value as “controller.fname='abc'”.

 

Hope this helps.

krishnagkrishnag

hi pradeep i hade that correction and it got saved.When i run the test its giving me test failure

@isTest
private class gusignupTests {

    static testMethod void myUnitTest() {
        PageReference p = Page.thankq;
   Test.setCurrentPage(p);
   
   gusignup controller = new gusignup();
   String nextPage = controller.psignup().getURL();
   
   controller = new gusignup();
   controller.fname='abc';
   controller.lname='xyz';
   controller.email='abc@xyz.com';
   controller.job='developer';
   controller.street='golf';
   controller.city='okc';
   controller.state='ok';
   controller.ctry='usa';
   controller.echeck=True;
   controller.comp='zurich';
   controller.zip='48326';
   controller.pwd='chinni';
   nextPage = controller.psignup().getURL();
   
   System.assertEquals('apex/thankq',nextPage);
   Lead[]leads = [select Id,Email from lead where Company = 'zurich'];
   guestuser__c[]guest = [select Id,Email__c from guestuser__c where Company__c = 'zurich'];
   System.assertEquals('abc@xyz.com', leads[0].Email);
   System.assertEquals('abc@xyz.com',guest[0].Email__c);
    }
}

 

 

message saying 

 

 

 

 System.NullPointerException: Attempt to de-reference a null object

 

 

Class.gusignupTests.myUnitTest: line 30, column 22 External entry point

 

gv007gv007

First insert some data into the table and query it.When you doiing any data base operation put it in try and catch block.

krishnagkrishnag

hi gopi the insertion of data is taken care by psignup method right which i have reffered in the test class.

Jonye KeeJonye Kee
The advanced materials and manufacturing of this iron combine to produce a higher-performing lightweight shaft (https://www.simtopiagolfski.ca/).