You need to sign in to do that
Don't have an account?
Test class needed. Help!!
Can somebody pls help me? I have been trying to work around the following code but cannot find the way to make the test method to have over 54% and I need 75%.
Please, your help will be greatly appreciated. Here is the following code:
global with sharing class propinvzum {
/************* variables de registro de formulalrio de contacto por solicitud de informacion********************/
public string correo{get;set;}
public string ncliente{get;set;}
public string paiscliente{get;set;}
public string mencliente{get;set;}
public string telcliente{get;set;}
public string body{get;set;}
public string subject{get;set;}
public string addresses{get;set;}
public string ccaddresses{get;set;}
public solicitud_de_informacion__c saveint;
/*************************************** Variables de Suscripcion*************************************/
public solicitud_de_informacion__c savesusc;
public solicitud_de_informacion__c getsavesusc(){
if(savesusc == null) savesusc = new solicitud_de_informacion__c(suscripcion__c= true);
return savesusc;
}
/*****************************variables de registro de interesados******************************/
public interesado__c interesado;
public interesado__c getinteresado(){
if(interesado == null) interesado = new interesado__c(propiedad__c = system.currentpageReference().getparameters().get('idprop'));
return interesado;
}
/*********************************************Registro de Formulario de contacto*********************************************************************/
public solicitud_de_informacion__c getsaveint(){
if(saveint == null) saveint = new solicitud_de_informacion__c(solicita_info__c= true);
return saveint;
}
public pageReference saveform(){
insert saveint;
correo = saveint.email__c;
ncliente = saveint.nombre__c;
paiscliente = saveint.pais__c;
mencliente = saveint.mensaje__c;
telcliente = saveint.telefono__c;
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
body = 'El(a) Sr(a) ' + ncliente + ' tiene el Email: ' + correo + ' y es del pais: ' + paiscliente + ' Tiene Telefono: ' + telcliente + ' y Solicita: ' + mencliente ;
// Define the email
subject = 'Nueva Solicitud de Informacion';
addresses = 'omrivas@lafise.com.ni';
ccaddresses = 'info@dynamicbcenter.com';
addresses += ':' + addresses;
ccaddresses += ':' + ccaddresses;
String[] toccAddresses = ccAddresses.split(':',0);
String[] toAddresses = addresses.split(':', 0);
// Sets the paramaters of the email
email.setSubject( subject );
email.setToAddresses( toAddresses );
email.setccAddresses( toccAddresses);
email.setPlainTextBody( body );
// Sends the email
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
pageReference newpage = new pageReference(system.currentPageReference().getURL());
newpage.setRedirect(true);
return newpage;
}
/**************************************Registro de Suscripcion**********************************************/
public pageReference savesuscripcion(){
insert savesusc;
correo = savesusc.email__c;
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
body = 'El siguiente Email se ha Suscribido:' + correo;
// Define the email
subject = 'Nueva Suscripcion';
addresses = 'omrivas@lafise.com.ni';
ccaddresses = 'info@dynamicbcenter.com';
addresses += ':' + addresses;
ccaddresses += ':' + ccaddresses;
String[] toccAddresses = ccAddresses.split(':',0);
String[] toAddresses = addresses.split(':', 0);
// Sets the paramaters of the email
email.setSubject( subject );
email.setToAddresses( toAddresses );
email.setccAddresses( toccAddresses);
email.setPlainTextBody( body );
// Sends the email
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
pageReference newpage = new pageReference(system.currentPageReference().getURL());
newpage.setRedirect(true);
return newpage;
}
/*********************************** Registro de Interesados***************************************/
public PageReference saveinteresado(){
insert interesado;
correo = interesado.email__c;
ncliente = interesado.nombre__c;
paiscliente = interesado.pais__c;
mencliente = interesado.mensaje__c;
telcliente = interesado.telefono__c;
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
body = 'El(a) Sr(a) ' + ncliente + ' tiene el Email: ' + correo + ' y es del pais: ' + paiscliente + ' Tiene Telefono: ' + telcliente + ' y Solicita: ' + mencliente ;
// Define the email
subject = 'Nuevo interesado en propiedad' ;
addresses = 'omrivas@lafise.com.ni';
ccaddresses = 'info@dynamicbcenter.com';
addresses += ':' + addresses;
ccaddresses += ':' + ccaddresses;
String[] toccAddresses = ccAddresses.split(':',0);
String[] toAddresses = addresses.split(':', 0);
// Sets the paramaters of the email
email.setSubject( subject );
email.setToAddresses( toAddresses );
email.setccAddresses( toccAddresses);
email.setPlainTextBody( body );
// Sends the email
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
pageReference newpage = new pageReference(system.currentPageReference().getURL());
newpage.setRedirect(true);
return newpage;
}
This is the test I currently have but it seems it isn't enough. Please help!
Your test method is not calling the savesuscripcion or the saveinteresado methods. I presume these are buttons on your VF page? You need to instantiate a page reference that invokes your VF page, then instantiate your controller, then instantiate a page reference that calls the method.
I don't understand your code, but you need something like this:
Object__c obj = new Object__c(Name = 'Test');
insert obj;
PageReference pageRef = Page.YourVFPage;
test.SetCurrentPage(pageRef);
ApexPages.StandardController controller = new ApexPages.StandardController(obj);
YourControllerExtension controllerExt = new YourControllerExtension(controller);
//Click the button.
PageReference clickButton = controllerExt.yourMethod();
Thank you very much for your help. I will try it with my code and I'll let you know. Thanks! Once I have it I will post the Test Method so it can help somebody else.
I have the following Test Method by using your example but I am still not able to have more than 55%. I am getting the following error message and don't know what to do. Why is it that I am getting the message?:
System.NullPointerException: Attempt to de-reference a null object
Please help in any way possible! Thank you.