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
bharath kuamarbharath kuamar 

Apex Test class help

 Error: Compile Error: Constructor not defined: [PreferenceChildController].<Constructor>() at line 410 column 39  for when i attempt to write testclass for the below class

 

public class PreferenceChildController
{
public Preference__c prefer{get;set;}
String LongDate = ' ';
public Preference__c preferobj{get;set;}
List<Commitment__c> list_commitment = new List<Commitment__c>();
private final Preference__c obj;
Public String oppId = System.currentPageReference().getParameters().get('opportunityId');
public List<ChildDetails> detailsList {get; set;}
public PreferenceChildController(ApexPages.StandardController stdController)
    {
        this.preferobj= (Preference__c)stdController.getRecord();
        
    }
   
public class ChildDetails
    {
        public String Name {get; set;}
        public String id {get;set;}
        public String regDate {get;set;}
        public String sex {get; set;}
        public String dob {get;set;}
        public String fieldOfficier{get;set;}
        public String religion {get;set;}
        public String status {get;set;}
        public Boolean isAssign {get;set;}
        
    }
    
    
    public void generateQuery()
    {
        system.debug('<<<<<<<<<<<<<>>>>>>>>>>>>>>'+preferobj.id);
        system.debug('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'+oppId);
      
        detailsList = new List<childDetails>();
         Preference__c prefObj =[Select Id, Age_From__c, Age_To__c, Sex__c, Continent__c, Country__c, Language__c, Religion__c
                                              From Preference__c Where Id=:preferobj.Id];
        List<String> list_status = new List<String>();
        list_status.add('Pooled');
        list_status.add('UnAssigned');
        list_status.add('Cancelled');
         system.debug('<<<<<<<<<<<<<>>>>>>>>>>>>>>');
        String sOQL = 'Select Age__c, Name, Child_Registration_Date__c, Comments__c, Continent__c, Country__c,'
                                + ' Date_of_Birth__c, Field_Officer__r.Name, Language__c, Religion__c, Sex__c, Status__c '
                                + ' From Child__c  Where Status__c IN :list_status';
        String whereSQOL = '';
        if (prefObj.Age_From__c != null && prefObj.Age_To__c != null)
        {
            whereSQOL = whereSQOL + ' And Age__c >='+prefObj.Age_From__c +' And Age__c <= '+ prefObj.Age_To__c;
        }
        else if (prefObj.Age_From__c == null && prefObj.Age_To__c != null)
        {
            whereSQOL = whereSQOL + ' And Age__c >='+prefObj.Age_To__c + ' And Age__c <= ' + prefObj.Age_To__c;
        }
        else if (prefObj.Age_From__c != null && prefObj.Age_To__c == null)
        {
            whereSQOL = whereSQOL + ' And Age__c >= '+ prefObj.Age_From__c + 'And Age__c <= '+prefObj.Age_From__c;
        }
        if (prefObj.Continent__c != null)
        {
            whereSQOL = whereSQOL + ' And Continent__c =\''+prefObj.Continent__c+'\'';
        }
        if (prefObj.Country__c != null)
        {
            whereSQOL = whereSQOL + ' And Country__c =\''+prefObj.Country__c+'\'';
        }
        /*else if (prefObj.Language__c != null)
        {
            whereSQOL = whereSQOL + ' And Language__c =:prefObj.Country__c';
        }*/
        if (prefObj.Religion__c != null)
        {
            whereSQOL = whereSQOL + ' And Religion__c =\''+prefObj.Religion__c+'\'';
        }
        if (prefObj.Sex__c != null)
        {
            whereSQOL = whereSQOL + ' And Sex__c =\''+prefObj.Sex__c+'\'';
        }
        
        sOQL = sOQL +  whereSQOL;
        system.debug('<<<<<<<<<<<<<>>>>>>>>>>>>>>');
        System.debug(sOQL + ' == sOQL');
        for (SObject oSobject : Database.Query(sOQL))
        {
            Child__c obj = (Child__c)oSobject;
            ChildDetails oChildDetails = new ChildDetails();
            oChildDetails.Name = obj.Name;
            oChildDetails.id = obj.Id;
            oChildDetails.regDate = obj.Child_Registration_Date__c != null ? obj.Child_Registration_Date__c.format() : '';
            oChildDetails.sex = obj.Sex__c;
            oChildDetails.dob = obj.Date_of_Birth__c != null ? obj.Date_of_Birth__c .format() : '';
            oChildDetails.fieldOfficier = obj.Field_Officer__r.Name;
            oChildDetails.religion = obj.Religion__c;
            oChildDetails.isAssign = false;
            detailsList.add( oChildDetails );
        }
    }
    public void getOpportunityId(){
    
    prefer =[select id,name,Duration__c,Opportunity__c from Preference__c where Id=:preferobj.id];
    system.debug('<<<<<arun>>>>>'+prefer.id);
    
    }
    
    
    
     
    
    
    public void manualAssignAction()
    
    {
    if(detailsList.size() == 0)
    {
    ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'No matching record for given criteria.');
    ApexPages.addMessage(myMsg);
    }else{
    Preference__c p=[select opportunity__c from preference__c where id=:preferobj.id];
    system.debug('<<<<<arun>>>>>'+oppId);
     getOpportunityId();
        for (ChildDetails obj : detailsList)
        {
            if (obj.isAssign)
            {
               Commitment__c com=new Commitment__c();
               com.Preference__c=preferobj.id;
               com.Commitment_Type__c = 'Sponsor Child';
               com.Start_Date__c = Date.today();
               com.Commitments__c=p.opportunity__c ;
               com.End_Date__c =  Date.today().addMonths(Integer.valueOf(prefer.Duration__c));
               com.Child__c =obj.id;
               com.Commitment_Details__c = 'Manual Assignment';
               com.Status__c = 'Assigned';
             //system.debug('<<<<<arun>>>>>'+prefer.name+'>>>>>>>>>>>>'+prefer.ID);
               list_commitment.add(com);
               
           }
        }
    
       if ( list_commitment != null &&  list_commitment.size() > 0 ) insert list_commitment;
          generatePaymentSchedules();
        }
    }
 public void autoAssignAction(){
 if(detailsList.size() == 0)
    {
    ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'No matching record for given criteria.');
    ApexPages.addMessage(myMsg);
    }else{
 
                    List<Preference__c> pref = [Select ID,
                                                    Age_From__c,
                                                    Age_To__c,
                                                    Country__c,
                                                    Continent__c,
                                                    Duration__c,
                                                    Payment_Mode__c,
                                                    Religion__c,
                                                    Sex__c,
                                                    Payment_Frequency__c
                                                From
                                                    Preference__c
                                                Where id=:preferobj.id ];
                    //System.debug('Preference Details : '+ opp1.Preference__c);
                    Preference__c p=[select opportunity__c from preference__c where id=:preferobj.id];
                   OpportunityContactRole oRole = [Select ContactId from OpportunityContactRole Where OpportunityId=:p.opportunity__c ];
                    System.debug('Contact Role : '+ oRole);
                    Opportunity opp = [Select Id, Amount from Opportunity Where Preference__c =:preferobj.id];

                    assignChild(pref.get(0), oRole.ContactId);
                       }
                     }
 
          public void assignChild(Preference__c prefObj, String contactId)
            {
                List<String> list_status = new List<String>();
                list_status.add('Pooled');
                list_status.add('UnAssigned');
                list_status.add('Cancelled');
               Preference__c p=[select opportunity__c from preference__c where id=:preferobj.id];
                //RecordType oRecordType = [Select Id From RecordType Where SObjectType= 'Child__c'];
                system.debug('<<<<<>>>>>>>');
                String sOQL = 'Select Age__c, Name, Child_Registration_Date__c, Comments__c, Continent__c, Country__c, '
                                        + ' Date_of_Birth__c, Field_Officer__r.Name, Language__c, Religion__c, Sex__c, Status__c '
                                        + ' From Child__c  Where Status__c IN :list_status';
                String whereSQOL = '';
                if (prefObj.Age_From__c != null && prefObj.Age_To__c != null)
                {
                    whereSQOL = whereSQOL + ' And Age__c >='+prefObj.Age_From__c +' And Age__c <= '+ prefObj.Age_To__c;
                }
                else if (prefObj.Age_From__c == null && prefObj.Age_To__c != null)
                {
                    whereSQOL = whereSQOL + ' And Age__c >='+prefObj.Age_To__c + ' And Age__c <= ' + prefObj.Age_To__c;
                }
                else if (prefObj.Age_From__c != null && prefObj.Age_To__c == null)
                {
                    whereSQOL = whereSQOL + ' And Age__c >= '+ prefObj.Age_From__c + 'And Age__c <= '+prefObj.Age_From__c;
                }
                if (prefObj.Continent__c != null)
                {
                    whereSQOL = whereSQOL + ' And Continent__c =\''+prefObj.Continent__c+'\'';
                }
                if (prefObj.Country__c != null)
                {
                    whereSQOL = whereSQOL + ' And Country__c =\''+prefObj.Country__c+'\'';
                }
                /*else if (prefObj.Language__c != null)
                {
                    whereSQOL = whereSQOL + ' And Language__c =:prefObj.Country__c';
                }*/
                if (prefObj.Religion__c != null)
                {
                    whereSQOL = whereSQOL + ' And Religion__c =\''+prefObj.Religion__c+'\'';
                }
                if (prefObj.Sex__c != null)
                {
                    whereSQOL = whereSQOL + ' And Sex__c =\''+prefObj.Sex__c+'\'';
                }
                 sOQL = sOQL +  whereSQOL + ' limit 1';
                system.debug('<<<<<<<<<<<<<>>>>>>>>>>>>>>'+contactId);
                System.debug(sOQL + ' == sOQL');
                List<Commitment__c> list_commitment = new List<Commitment__c>();
                for (SObject oSobject : Database.Query(sOQL))
                {
                    Child__c oChild = (Child__c)oSobject;
                    Commitment__c com=new Commitment__c();
                    com.Contact__c = contactId;
                    com.Preference__c=prefObj.ID;
                    com.Commitment_Type__c = 'Sponsor Child';
                    com.Child__c = oChild.Id;
                    com.Commitments__c = p.opportunity__c;
                    com.Start_Date__c = Date.today();
                    com.End_Date__c =  Date.today().addMonths(Integer.valueOf(prefObj.Duration__c));
                    com.Commitment_Details__c = 'Auto Assignment';
                    com.Status__c = 'Assigned';
                    list_commitment.add(com);
                }
                if ( list_commitment != null &&  list_commitment.size() > 0 ) insert list_commitment;
                 generatePaymentSchedules();
            }
 
 
 
 

            
            public void generatePaymentSchedules()
              {
                              Integer calculateduration=0;
              Preference__c p=[select opportunity__c from preference__c where id=:preferobj.id];
              
              system.debug('<<<<<bhart>>>>>');
               Opportunity oppamt =[select OppAmount__c from opportunity where id=:p.opportunity__c ];
                 system.debug('<<<<<<<<<<<venki>>>>>>>>>>');
                 Opportunity opp1=[select Preference__c from opportunity where id=:p.opportunity__c ];
                 Preference__c payPref = [select Payment_Frequency__c,Duration__c from preference__c where id=:opp1.Preference__c];
                  Integer divisionRatio = 1;
                   Integer scheduleDueDate = 0;
                    Integer PayDurationInt = Integer.valueOf(payPref.Duration__c);

                  if (payPref.Payment_Frequency__c == 'Quarterly')
                  {
                      divisionRatio = 3;
                      scheduleDueDate = 3;
                    system.debug('<<<<<<<<<<<schedule date calculation>>>>>>>>>>');
                  }
                  if (payPref.Payment_Frequency__c == 'Half-Yearly')
                  {
                      divisionRatio = 6;
                      scheduleDueDate = 6;
                    
                  }
                  if(payPref.Payment_Frequency__c =='Annually(Full Payment)')
                  {
                  calculateduration =1;
                  
                  
                  }
                  else
                  {
                  calculateduration =(PayDurationInt/divisionRatio);
                  }
                  
                  
                  List<Payment_Schedule__c> list_schedule = new List<Payment_Schedule__c>();
                  System.debug(divisionRatio + ' == paymentMode ==');
                  Double scheduleAmt = oppamt.OppAmount__c/divisionRatio;
                  Integer temp =0;
                  system.debug('<<<<<<<<111111111111>>>>>>>>>>>');
                  for (Integer j = 0; j < calculateduration ; j++)
                  {
                        
                        Payment_Schedule__c obj = new Payment_Schedule__c();
                        obj.opportunity__c = p.opportunity__c ;
                        obj.Amount__c = scheduleAmt;
                        temp+= scheduleDueDate;
                        obj.Payment_Start_Date__c = Date.today().addMonths(temp);
                        list_schedule.add(obj);
                  }
                  
                  if (list_schedule.size() > 0){
                  system.debug('<<<<<<<<222222222222>>>>>>>>>>>');
                   insert list_schedule;
                   closedOpportunity();
                    
                  
                 }
            }
            
            
 
 
  public void closedOpportunity(){
           Preference__c p=[select opportunity__c from preference__c where id=:preferobj.id];
           Opportunity opp = [select id,StageName from opportunity where id=:p.opportunity__c ];
           opp.StageName ='Closed Won';
           update opp;
          EmailReportGen();
       
 }
public void EmailReportGen()
      {
        
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
        String[] toAddresses = new String[] {'arunsoftviewrmd@gmail.com'};
        Blob b;
        Messaging.SendEmailResult [] r;
        email.setSubject('Child Assigned');
        email.setToAddresses(toAddresses);
        email.setHtmlBody( 'Hi,<br/><br/> Commitment Details has attached for your reference  <br/><br/>Thank you,<br/>');
        PageReference attachPdf =  Page.PDFGenratorpage;
        String pageUrl=attachPdf.getUrl();
        pageUrl = pageUrl + '?id=' +preferobj.id;
        PageReference pdf = new Pagereference(pageUrl);
        pdf.setRedirect(true);
        b = pdf.getContentAsPdf();
        efa.setFileName('.pdf');
        efa.setBody(b);
        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
        r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
    }
    
    // test class for the class "PreferenceChildController"
    
    static testmethod void PreferenceChildController()
    {
    
     
    PreferenceChildController pref1 = new PreferenceChildController();
    ApexPages.StandardController sc = new ApexPages.StandardController(pref1);
            
            
         
    }
    
    
   
 
 }


Please anyone help me

JitendraJitendra

Hi You are trying to create test class for the Controller extension:

 

1. First create Object of Preference__c
2. Then insert it.
3. After that pass the object in ApexPages.StandardController(obj)

 

Preference__c obj = new Preference__c();
//Set few required fields and insert obj
ApexPages.StandardController sc = new ApexPages.StandardController(obj);
PreferenceChildController prefCntrlr= new PreferenceChildController(sc);  

 

bharath kuamarbharath kuamar

static testmethod void Projectassignment()
 {
 try
 {
  String paymentfrequency ='Annually(Full Payment)';
  String paymentModes='cash';
 String Duration='3';
 Boolean isProjAssign =true;
 final Preference__c obj;
 String oppId = System.currentPageReference().getParameters().get('opportunityId');
 //List<ProjectDetails> detailsList {get; set;}
 
 }catch(exception e){
 }
 

 ApexPages.StandardController sc = new ApexPages.standardController(new Project__c());
  Project__c project = new Project__c ();
 ProjectDetails newproj = new ProjectDetails();
 try
 {
 newproj.Name ='test';
 newproj.Id='a0790000004gzbG';
 newproj.StartDate=date.today();
 newproj.fieldOfficier='test';
 newproj.isAssign =true;
 
 }
 catch(exception e)
 {
 }

 Payment_Schedule__c obj = new Payment_Schedule__c();
 //obj.generatePaymentSchedules(String PreferenceID);
 obj.opportunity__c ='00690000006y86z';
 obj.Amount__c = 1200;
 obj.Payment_Start_Date__c = Date.today();

 insert obj;
 
 opportunity opp =  new opportunity();
         
opp.name='00690000006yeky';
opp.StageName ='Closed Won';
opp.CloseDate=date.today();
insert opp ;


 i have written the class also called the constructor ,  but the coverage shows only 10% can , u please help me out with this case

bob_buzzardbob_buzzard

In your test, you have the following:

 

PreferenceChildController pref1 = new PreferenceChildController();
ApexPages.StandardController sc = new ApexPages.StandardController(pref1);

 

but you haven't defined a no argument constructor for PreferenceChildController.  If you had defined no constructors, Apex would provide an implicit no argument constructor for you to call. However, as you have defined a constructor that takes a standardcontroller as a parameter, you don't get the implicit constructor.

 

Also, you have passed the standard controller an instance of the preferencechildcontroller, whereas that needs to be a standard or custom object instance.  What type of data is the standard controller managing?

 

 

 

bharath kuamarbharath kuamar
Please help me out what needs to be done, would be great if u can help me out with my test class
bob_buzzardbob_buzzard

If you tell me the object type being managed by the standard controller I'll be able to help you with the constructor.

bharath kuamarbharath kuamar
the object type managed is project
bob_buzzardbob_buzzard

In that case you should create a project, create a standard controller containing that, then create your extension controller containing the standard:

 

Project__c proj=new Project__c();
ApexPages.StandardController sc = new ApexPages.StandardController(proj);
PreferenceChildController pref1 = new PreferenceChildController(sc);