You need to sign in to do that
Don't have an account?
Karthikeya Mullunja Ramakrishna
Help in writing the test class
My controller class --
Public Class ContactUpdateServer {
public Contact objContact{get;set;}
public String currentRecordId {get;set;}
public Contact con{get;set;}
public List<contact> up = new List<Contact>();
List<ContactCreationCalloutParser.Profile> lstProfilemain = new List<ContactCreationCalloutParser.Profile>() ;
public ContactUpdateServer(ApexPages.StandardController controller) {
Boolean isValid = False;
currentRecordId = ApexPages.CurrentPage().getparameters().get('id');
con = [select SSO_Number__c,Name from Contact where id =: currentRecordId ];
system.debug('----- sso --- ' +con);
string searchstring = con.SSO_Number__c;
if(searchString.length() == 9){
// Check if search string is 9 digit long numeric value. i.e SSO number.
if(searchString.isNumeric()){
isValid = True;
}}
if(isValid){
system.debug('----isvalid----' +isValid);
ContactServer();}
}
Public Pagereference Goback(){
Contact objContact = new Contact();
objContact.Id = ApexPages.CurrentPage().getparameters().get('id');
PageReference pageRef = new PageReference('/' + objContact.Id);
pageRef.setRedirect(true);
return pageRef;
}
Test Class :
@isTest
private Class ContactUpdateServerTest{
static testMethod void test1()
{
Contact objContact = new Contact(lastname = 'test',firstname = 'firstTest', title ='titletest',type__c = 'type',SSO_Number__c = '502066194');
insert objContact;
Test.startTest();
ContactUpdateServer obj = new ContactUpdateServer(new ApexPages.StandardController(objContact));
ApexPages.currentPage().getParameters().put(obj.currentRecordId,objContact.id);
system.assertequals(objContact.SSO_Number__c.Length(),9);
// string searchstring = objContact.SSO_Number__c;
// string currentRecordId = objContact.Id;
Test.stopTest();
}
Static TestMethod void test2(){
Contact objContact = new Contact(lastname = 'test',firstname = 'firstTest', title ='titletest',type__c = 'type',SSO_Number__c = '502066194');
insert objContact;
test.startTest();
ApexPages.StandardController sc = new ApexPages.standardController(objContact);
ContactUpdateServer obj = new ContactUpdateServer(sc);
PageReference pageRef = obj.Goback();
Test.setCurrentPage(pageRef);
pageRef.getParameters().put('id',objContact.id);
obj.GoBack();
System.assertNotEquals(null,pageRef);
test.stopTest();
}
}
Please suggest where i am going wrong -- I am getting an error "List has no rows for assignment to SObject".
Public Class ContactUpdateServer {
public Contact objContact{get;set;}
public String currentRecordId {get;set;}
public Contact con{get;set;}
public List<contact> up = new List<Contact>();
List<ContactCreationCalloutParser.Profile> lstProfilemain = new List<ContactCreationCalloutParser.Profile>() ;
public ContactUpdateServer(ApexPages.StandardController controller) {
Boolean isValid = False;
currentRecordId = ApexPages.CurrentPage().getparameters().get('id');
con = [select SSO_Number__c,Name from Contact where id =: currentRecordId ];
system.debug('----- sso --- ' +con);
string searchstring = con.SSO_Number__c;
if(searchString.length() == 9){
// Check if search string is 9 digit long numeric value. i.e SSO number.
if(searchString.isNumeric()){
isValid = True;
}}
if(isValid){
system.debug('----isvalid----' +isValid);
ContactServer();}
}
Public Pagereference Goback(){
Contact objContact = new Contact();
objContact.Id = ApexPages.CurrentPage().getparameters().get('id');
PageReference pageRef = new PageReference('/' + objContact.Id);
pageRef.setRedirect(true);
return pageRef;
}
Test Class :
@isTest
private Class ContactUpdateServerTest{
static testMethod void test1()
{
Contact objContact = new Contact(lastname = 'test',firstname = 'firstTest', title ='titletest',type__c = 'type',SSO_Number__c = '502066194');
insert objContact;
Test.startTest();
ContactUpdateServer obj = new ContactUpdateServer(new ApexPages.StandardController(objContact));
ApexPages.currentPage().getParameters().put(obj.currentRecordId,objContact.id);
system.assertequals(objContact.SSO_Number__c.Length(),9);
// string searchstring = objContact.SSO_Number__c;
// string currentRecordId = objContact.Id;
Test.stopTest();
}
Static TestMethod void test2(){
Contact objContact = new Contact(lastname = 'test',firstname = 'firstTest', title ='titletest',type__c = 'type',SSO_Number__c = '502066194');
insert objContact;
test.startTest();
ApexPages.StandardController sc = new ApexPages.standardController(objContact);
ContactUpdateServer obj = new ContactUpdateServer(sc);
PageReference pageRef = obj.Goback();
Test.setCurrentPage(pageRef);
pageRef.getParameters().put('id',objContact.id);
obj.GoBack();
System.assertNotEquals(null,pageRef);
test.stopTest();
}
}
Please suggest where i am going wrong -- I am getting an error "List has no rows for assignment to SObject".
Please try this code and let me know how this works for you.
Please mark this as Solved if this helps you so that others can view it as a proper solution.
Thanks,
Apoorv
All Answers
Please modify the code between Test.startTest() and Test.StopTest() in your method test1() to the following:
Test.startTest();
ContactUpdateServer obj = new ContactUpdateServer(new ApexPages.StandardController(objContact));
ApexPages.currentPage().getParameters().put('id',objContact.id);
system.assertequals(objContact.SSO_Number__c.Length(),9);
Test.stopTest();
Please let me know how this works for you, mark this as Solved if this helps you so that others can view it as a proper solution.
Thanks,
Apoorv
Please try this code and let me know how this works for you.
Please mark this as Solved if this helps you so that others can view it as a proper solution.
Thanks,
Apoorv