You need to sign in to do that
Don't have an account?
PC
test class for fetching cookies
Hi, I want to write a test class for this....I am not able to get what i should write for the fetching cookies method and for the go method i am not able to cover all the lines.
public with sharing class ctr_validation {
// Varaible Declaration
Public String lang {get;set;}
Public Contact con { get; set;}
Public String tout { get; set;}
Public String tablen{get;set;}
//Public String existingorderid { get; set;}
List<String> errorMessages = new List<String>();
/* Constructor Starts here */
public ctr_validation(){
lang = ApexPages.currentPage().getParameters().get('lang');
tout = System.currentPageReference().getParameters().get('tout');
tablen = ApexPages.currentPage().getParameters().get('tablen');
languageCookiValue(lang);
con = new Contact();
}
//Fetch cookie's language
private string fetchLanguageCook(){
string data='';
Cookie cook = ApexPages.currentPage().getCookies().get('language');
if(cook != null){
data = cook.getvalue();
}
return data;
}
/* Go method Starts here */
public PageReference go() {
try{
if(con.MobilleNumber__c.length() < 10 || con.MobilleNumber__c.startsWith('00') || con.MobilleNumber__c.length() > 10)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'Please enter 10 Digit for Mobile Number EX: XXXXXXXXXX'));
}
else if (con.MobilleNumber__c!= null && con.MobilleNumber__c!= '')
{
List<Contact> checkcon = new List<Contact>();
checkcon = [select id, name, phone, MobilleNumber__c from contact where MobilleNumber__c=:con.MobilleNumber__c limit 1];
system.debug('&&&&'+con);
if(checkcon.size()>0){
Contact conupdate = new Contact();
conupdate.Last_Login_Reff_Id__c = string.valueof(system.now());
conupdate.id =checkcon[0].id;
update conupdate;
PageReference conpg = new PageReference('/apex/vfp_main?cid='+checkcon[0].id+'&lang='+lang+'&tout='+tout+'&tablen='+tablen);
conpg.setRedirect(TRUE);
return conpg;
}
else{
PageReference connewpg = new PageReference('/apex/vfp_contactInfo?lang='+lang+'&mob='+con.MobilleNumber__c+'&tout='+tout+'&tablen='+tablen);
connewpg.setRedirect(TRUE);
return connewpg;
}
}
else
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter the mobile number'));
return null;
}
}
Catch(Exception e){
ApexPages.addmessages(e);
return null;
}
return null;
}
/* Go method ends here */
/* home method Starts here */
public PageReference home() {
PageReference homepg= new PageReference('/apex/vfp_home');
homepg.setRedirect(TRUE);
return homepg;
}
/* home method ends here */
}
public with sharing class ctr_validation {
// Varaible Declaration
Public String lang {get;set;}
Public Contact con { get; set;}
Public String tout { get; set;}
Public String tablen{get;set;}
//Public String existingorderid { get; set;}
List<String> errorMessages = new List<String>();
/* Constructor Starts here */
public ctr_validation(){
lang = ApexPages.currentPage().getParameters().get('lang');
tout = System.currentPageReference().getParameters().get('tout');
tablen = ApexPages.currentPage().getParameters().get('tablen');
languageCookiValue(lang);
con = new Contact();
}
//Fetch cookie's language
private string fetchLanguageCook(){
string data='';
Cookie cook = ApexPages.currentPage().getCookies().get('language');
if(cook != null){
data = cook.getvalue();
}
return data;
}
/* Go method Starts here */
public PageReference go() {
try{
if(con.MobilleNumber__c.length() < 10 || con.MobilleNumber__c.startsWith('00') || con.MobilleNumber__c.length() > 10)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'Please enter 10 Digit for Mobile Number EX: XXXXXXXXXX'));
}
else if (con.MobilleNumber__c!= null && con.MobilleNumber__c!= '')
{
List<Contact> checkcon = new List<Contact>();
checkcon = [select id, name, phone, MobilleNumber__c from contact where MobilleNumber__c=:con.MobilleNumber__c limit 1];
system.debug('&&&&'+con);
if(checkcon.size()>0){
Contact conupdate = new Contact();
conupdate.Last_Login_Reff_Id__c = string.valueof(system.now());
conupdate.id =checkcon[0].id;
update conupdate;
PageReference conpg = new PageReference('/apex/vfp_main?cid='+checkcon[0].id+'&lang='+lang+'&tout='+tout+'&tablen='+tablen);
conpg.setRedirect(TRUE);
return conpg;
}
else{
PageReference connewpg = new PageReference('/apex/vfp_contactInfo?lang='+lang+'&mob='+con.MobilleNumber__c+'&tout='+tout+'&tablen='+tablen);
connewpg.setRedirect(TRUE);
return connewpg;
}
}
else
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter the mobile number'));
return null;
}
}
Catch(Exception e){
ApexPages.addmessages(e);
return null;
}
return null;
}
/* Go method ends here */
/* home method Starts here */
public PageReference home() {
PageReference homepg= new PageReference('/apex/vfp_home');
homepg.setRedirect(TRUE);
return homepg;
}
/* home method ends here */
}
static testMethod void testMethod4(){
ctr_validation instance1 = new ctr_validation();
instance1.con.MobilleNumber__c = '6789054321';
instance1.go();
}
Give the test mobile number not the one which is already exist in your organization.
Thanks
All Answers
Make fetchLanguageCook method as @testVisible to cover with test data.
Thnaks
Thanks
@isTest(SeeAllData=TRUE)
private class ctr_validationTest
{
static testMethod void sampleTestMethod()
{
Contact con = new Contact();
Tables__c t = new Tables__c();
t.name='1';
t.ActiveTable__c=true;
insert t;
ApexPages.currentPage().getParameters().put('lang', 'en');
ApexPages.currentPage().getParameters().put('tout', 'true');
ApexPages.currentPage().getParameters().put('tablen', t.Id);
ApexPages.currentPage().getCookies().get('en');
ctr_validation instance = new ctr_validation();
PageReference pg = instance.setLanguage('en');
PageReference homepg = instance.home();
String data = instance.fetchLanguageCook();
instance.go();
con.MobilleNumber__c='';
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'Please enter 10 Digit for Mobile Number EX: XXXXXXXXXX'));
con.MobilleNumber__c='8945856965';
List<Contact> checkcon = new List<Contact>();
checkcon = [select id, name, phone, MobilleNumber__c from contact where MobilleNumber__c=:con.MobilleNumber__c limit 1];
}
}
For Go() method ---
In your apex class Go(){
if(con.MobilleNumber__c.length() < 10 || con.MobilleNumber__c.startsWith('00') || con.MobilleNumber__c.length() > 10)
-----
}
So, here from where you are getting con.Mobilenumber value. I can not see contact record/data in the apex controller.
Thanks
<apex:inputField type="tel" value="{!con.MobilleNumber__c}" html-placeholder="10 digit Mobile No. " required="true" id="phone" /><br/>
I have tried a little bit more on the test class. Now its not covering from the 'else if' part .
@isTest(SeeAllData=TRUE)
private class ctr_validationTest
{
static testMethod void sampleTestMethod()
{
Contact con = new Contact();
Tables__c t = new Tables__c();
t.name='1';
t.ActiveTable__c=true;
insert t;
ApexPages.currentPage().getParameters().put('lang', 'en');
ApexPages.currentPage().getParameters().put('tout', 'true');
ApexPages.currentPage().getParameters().put('tablen', t.Id);
ApexPages.currentPage().getCookies().get('en');
ctr_validation instance = new ctr_validation();
PageReference pg = instance.setLanguage('en');
PageReference homepg = instance.home();
String data = instance.fetchLanguageCook();
instance.go();
}
static testMethod void testifMethod()
{
ctr_validation obj = new ctr_validation();
obj.con.MobilleNumber__c = '0034565435';
obj.go();
}
static testMethod void testelseMethod()
{
ctr_validation obj = new ctr_validation();
obj.con.MobilleNumber__c = '';
obj.go();
}
static testMethod void testifelseMethod()
{
ctr_validation obj = new ctr_validation();
obj.con.MobilleNumber__c = '898113926';
obj.go();
List<Contact> checkcon = new List<Contact>();
checkcon = [select id, name, phone, MobilleNumber__c from contact where MobilleNumber__c=:obj.con.MobilleNumber__c limit 1];
if(checkcon.size()>0){
Contact conupdate = new Contact();
conupdate.Last_Login_Reff_Id__c = string.valueof(system.now());
conupdate.id =checkcon[0].id;
update conupdate;
PageReference conpg = obj.go();
}
}
}
Try giving the obj.con.MobilleNumber__c with different values in different methods.
1. obj.con.MobilleNumber__c = '00789978'
2. obj.con.MobilleNumber__c = ''
3. obj.con.MobilleNumber__c= '6789012345' (10 digit number -- you missed this one)
Let me know if it works
Thanks
now only these lines are not getting covered
PageReference connewpg = new PageReference('/apex/vfp_contactInfo?lang='+lang+'&mob='+con.MobilleNumber__c+'&tout='+tout+'&tablen='+tablen);
connewpg.setRedirect(TRUE);
return connewpg;
}
}
else
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter the mobile number'));
return null;
}
{
ctr_validation obj = new ctr_validation();
obj.con.MobilleNumber__c = '8981139326';
obj.go();
List<Contact> checkcon = new List<Contact>();
checkcon = [select id, name, phone, MobilleNumber__c from contact where MobilleNumber__c=:obj.con.MobilleNumber__c limit 1];
if(checkcon.size()>0){
Contact conupdate = new Contact();
conupdate.Last_Login_Reff_Id__c = string.valueof(system.now());
conupdate.id =checkcon[0].id;
update conupdate;
PageReference conpg = obj.go();
}
else
{
PageReference connewpg = obj.go(); // I tried this for the page reference inside else
}
}
}
static testMethod void testMethod4(){
ctr_validation instance1 = new ctr_validation();
instance1.con.MobilleNumber__c = '6789054321';
instance1.go();
}
Give the test mobile number not the one which is already exist in your organization.
Thanks
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter the mobile number'));
return null;
I have wrote the code for null check also.Still its not covering that line.
static testMethod void testelseMethod()
{
ctr_validation obj1 = new ctr_validation();
obj1.con.MobilleNumber__c = '';
obj1.go();
}
That's fine Puja. What is the % of your test class?? if it is more than 75% thats enough to move to production.
And try to implemet all the test classes without SeeAllData = TRUE as it is a best practice.
Thanks
Anyways....Thank you very much for the help. :D