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
nr123nr123 

Need Help With Test for Contructor

I currently have 69% test coverage on the calls. The only code left in the class to test is the constructor. Any suggestions would be greatly appreciated.

Here is the Class: It is basically is just displaying query results for a dashboard:

 

 

public with sharing class retrieveQuotedata {
    Public Date lastbusinessday;
    Public Datetime lastBDtime;
    Public String LBDS;
    Public Date getLastBusinessDay(){
    BusinessHours bh = [select id from businesshours where IsDefault=true];
    Date MyDate = date.today();
    Datetime startTime = Datetime.valueOf(MyDate +' 00:00:00');
    Datetime lastBDTime = BusinessHours.add(bh.id, startTime, (-24 * 60 * 60 * 1000L)); 
    lastbusinessday = date.newinstance(lastBDtime.year(), lastBDtime.month(), lastBDtime.day());  
    if(MyDate == MyDate.tostartofweek()+1){
        lastbusinessday-= 1;
        }
    //LBDS = lastbusinessday.format();
    return lastbusinessday;
    }

    public class QuotesRecvd{

        Public String quotecount {get; set;}
        Public String oId {get;set;}
        Public String OName {get;set;}
        
        public QuotesRecvd(string n ,string q){     
            this.oName = n;
            this.quotecount = q;  
        }
        
        public QuotesRecvd(string n ,string q, string o){     
            this.oName = n;
            this.quotecount = q; 
            this.oid = o;
        }
 
    }
       public List<QuotesRecvd> QuotesRecvdList = new List<QuotesRecvd>();    
       
           public List<QuotesRecvd> getQuotesRecvdCount(){

              AggregateResult[] agr = [SELECT Owner.Name, count(Name) FROM Opportunity WHERE RecordtypeId = '012500000009RqP' AND Version__c = 'TSC Original Quote' AND Quote_Requested__c = :getLastBusinessDay() Group By rollup (Owner.Name) LIMIT 100];
           for(AggregateResult qrcvd : agr){

               QuotesRecvdList.add(new QuotesRecvd(String.valueof(qrcvd.get('Name')),String.valueof(qrcvd.get('expr0'))));
               }  
               return QuotesRecvdList;         
        }
}

 

 

 

Here is what i have so far for test code: (i'm at 69% so far)

 

 

 

 

@isTest
Private class testretrieveQuotedata{
       static testMethod void insertOpps(){    
          //First, create account for the test data
          retrieveQuotedata qs = new retrieveQuotedata();
          Account acct = new Account(name='test account');
          
          insert acct;
           //create an opportunity record
          Opportunity[] opptsToCreate = new Opportunity[]{};
          
          for(Integer i=0; i<1;i++){
              Opportunity oppt = new Opportunity(AccountId=acct.Id, RecordTypeId='012500000009RqP',Name ='testing trigger', Quote_requested__c = qs.getlastbusinessday(), CloseDate = date.parse('9/1/2011'), StageName = '0-Quote Request');
              opptsToCreate.add(oppt);
          }
          insert opptsToCreate;

          for(Opportunity o:opptsToCreate){
          o.Info_Requested_CB__c = true;
          o.Missing_info_requested__c = qs.getlastbusinessday();
          o.Pending_Codes__c = 'IT';
        //  o.Quote_Sent_CB__c = true;
          o.Quote_sent_date__c = qs.getlastbusinessday();
          o.due_date__c = date.today();
          o.quote_id__c = '99999-9';
          o.Lead_Convert__c = true;

          }
          update opptsToCreate;
    }
     
    static testMethod void testgetQuotesRecvdCount(){
          // instantiate the class under test
      retrieveQuotedata qs = new retrieveQuotedata();
         
      List<retrieveQuotedata.QuotesRecvd> QuotesRecvdList = qs.getQuotesRecvdCount();

      System.assertEquals(0, QuotesRecvdList.size());
   }
}

 

 

 

 

 

Thank you.

 

 

yvk431yvk431

is this the complete class, i do not see a contructor defined in here .

If its a custom controller then instantiating the class will call the contructor.

 

If its an standard controller, then you need pass the standardcontroller instance as an argument to the contructor before you call it.

 

Object o = new object();
o.name = 'test';
o.insert;
ApexPages.StandardController stdController = new ApexPages.StandardController(o);
Controller c = new Controller(stdController);

This will call the standard contructor.

stdController
nr123nr123

I'm using a custom controller. I got my test coverage to 100%. Thanks.

 

 

I haven't been able to figure out how to get my test to point to the test records that I inserted in my test class. When I run my test, it queries real data instead of my test records. When I try to deploy, my tests will fail because my assertions wont match. How do I fix this?