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
companyDBCcompanyDBC 

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;
 }        

 

companyDBCcompanyDBC

This is the test I currently have but it seems it isn't enough. Please help!

 

 

@istest
private class propiedades1{
 static testMethod void testpropiedades() {
 
    userinteresting__c sp = new userinteresting__c();
   sp.propiedad2__c = 'a0RQ0000000ArLU';
   insert sp;
 /*system.assert(controller.saveuserint()!=null);*/
  controller.getuserint();
   system.assertEquals(sp.propiedad2__c ,'a0RQ0000000ArLU');
   
////////////////////////////////////////////////////////////////////////////
   registro_de_usuarios__c ru = new registro_de_usuarios__c(
   primer_nombre__c = 'test',
   apellidos__c = 'test',
   email__c = 'test@salesforce.com',
   password__c = '12345678',
   telefono__c = '22222222');
   test.startTest();
    insert ru;
    test.StopTest();
   registro_de_usuarios__c prueba = [select id,primer_nombre__c from registro_de_usuarios__c where primer_nombre__c =:ru.primer_nombre__c];
 /*controller.save();*/
    system.assertEquals('test',prueba.primer_nombre__c);
   
   
    controller.getreguser();
    system.assertEquals('test',ru.primer_nombre__c);
    system.assertEquals('test',ru.apellidos__c);
    system.assertEquals(ru.email__c,'test@salesforce.com'  );
    system.assertEquals( '12345678',ru.password__c );
    system.assertEquals( '22222222',ru.telefono__c );
 /////////////////////////////////////////////////////////////////////////////////////   
    propiedad2__c pr = new propiedad2__c();
    pr.registro_de_usuarios__c = ru.id;
    insert pr;
    /*system.assert(controller.savepropiedad()==null);*/
    controller.getregprop();
    system.assertEquals( ru.id,pr.registro_de_usuarios__c);
    
    attachment att = new Attachment();
    att.parentid =pr.id ;
    att.name='testAttach';
     att.body = Blob.valueOf( 'this is an attachment test' );
    insert att;
      
    system.assertEquals( pr.id,att.parentid);
dmchengdmcheng

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();

companyDBCcompanyDBC

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.

companyDBCcompanyDBC

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.

 

@isTest
private class propinvzum1{
 public static testMethod void testpropinvzum() {

       propinvzum controller = new propinvzum();
     
      controller.doSearch();
      controller.fincas();
      controller.vehiculos();
      controller.ecomercial();
     controller.otros();
     controller.magricolas();
     controller.page();   
     controller.terrenos();    
     controller.getinteresado();
      controller.buscar();
     controller.apartamentos();
     controller.uoferta();
     controller.Oespecial();
       controller.getsavesusc();
        controller.getsaveint();

solicitud_de_informacion__c obj = new solicitud_de_informacion__c(email__c='test@force.com');
insert obj;

PageReference pageRef = Page.contactoiz;
test.SetCurrentPage(pageRef);

ApexPages.StandardController controler = new ApexPages.StandardController(obj);
propinvzum controllerExt = new propinvzum(controler);
PageReference clickButton = controllerExt.savesuscripcion();
}
}