I used a get property in a controller how can I get the coverage for that method in my test class
My Controller:
public class VisitRaisedDateTime {
public String RecordId;
public Decimal tvalue;
public decimal businessMillisecondsDiff;
public decimal businessMinsDiff ;
public decimal hourDiff;
public decimal businessHoursDifferen ;
public decimal minDiff ;
public integer hour;
public integer hour1;
public integer minute;
public String raisedhours;
public boolean differenthours;
public List<Opportunity> OppList{get;set;}
public DateTime raiseddte;
public DateTime rdte;
public String dtFormat ='dd/MM/yyyy HH:mm';
public VisitRaisedDateTime(ApexPages.StandardController controller) {
RecordId=ApexPages.currentPage().getParameters().get('id');
}
public boolean getRaisedDateandTime(){
OppList= [SELECT Name,Raised_Date_Time__c,Arrived_in_Site__c,Visit_Priority__c,Reactive_Sheet__c,Maintenance_Sheet__c
FROM Opportunity WHERE Raised_Date_Time__c!=Null AND Visit_Priority__c !=Null AND Arrived_in_Site__c!=Null AND
/*Reactive_Sheet__c!=Null AND Maintenance_Sheet__c='No' AND */ ID=:RecordId ];
System.debug('=====inside the method=====');
for(opportunity ac: OppList) {
if(ac.Arrived_in_Site__c != null && ac.Raised_Date_Time__c != null && ac.Visit_Priority__c!=null && ac.Arrived_in_Site__c > ac.Raised_Date_Time__c){
businessMillisecondsDiff = decimal.valueOf((ac.Arrived_in_Site__c.getTime() - ac.Raised_Date_Time__c.getTime()));
businessMinsDiff = businessMillisecondsDiff / (1000*60);
businessHoursDifferen = businessMillisecondsDiff / (1000.0*60.0*60.0);
minDiff = businessHoursDifferen.setScale(2);//(2, RoundingMode.HALF_UP);
system.debug('-----before corrected hours:'+minDiff );
hour =integer.Valueof(minDiff);
//hour = Math.Mod(integer.Valueof(minDiff),60);
system.debug('-----hour :'+hour);
minute = Math.Mod(integer.Valueof(businessMinsDiff), 60);
system.debug('-----minute :'+minute);
raisedhours= hour+'.'+minute;
system.debug('-----corrected hours:'+raisedhours);
}
System.debug('=====Outside For Loop=====');
if(ac.Visit_Priority__c != null){
if(ac.Visit_Priority__c == '2 Hour'){
tvalue=2.00;
system.debug('$$$ tvalue:' + tvalue);
}else if(ac.Visit_Priority__c == '3 Hour'){
tvalue=3.00;
system.debug('$$$ tvalue:' + tvalue);
}else if(ac.Visit_Priority__c == '4 Hour'){
tvalue=4.00;
system.debug('$$$ tvalue:' + tvalue);
}else if(ac.Visit_Priority__c == '8 Hour'){
tvalue=8.00;
system.debug('$$$ tvalue:' + tvalue);
}else if(ac.Visit_Priority__c == '24 Hour'){
tvalue=24.00;
system.debug('$$$ tvalue:' + tvalue);
}else if(ac.Visit_Priority__c == '3 Day'){
tvalue=72.00;
system.debug('$$$ tvalue:' + tvalue);
}else if(ac.Visit_Priority__c == '5 Day'){
tvalue=120.00;
system.debug('$$$ tvalue:' + tvalue);
}else if(ac.Visit_Priority__c == '10 Day'){
tvalue=240.00;
system.debug('$$$ tvalue:' + tvalue);
}
}
if(raisedhours!=null && tvalue!=null){
if(decimal.valueOf(raisedhours) > tvalue){
differenthours=true;
}else{
differenthours=False;
}
}
system.debug('----afterdifference-----'+differenthours);
}
return differenthours;
}
}
Test Class:
In this getting error
"Initial term of field expression must be a concrete SObject:
List at line 8 column 71"
@isTest(seeAllData=true)
Public Class VisitRaisedDateTimeTest{
@isTest Static void VisitRaisedDateTimeTests(){
List<Opportunity> Oppid;
Oppid = [SELECT Name,Raised_Date_Time__c,Arrived_in_Site__c,Visit_Priority__c,Reactive_Sheet__c,Maintenance_Sheet__c
FROM Opportunity WHERE Raised_Date_Time__c!=Null AND Visit_Priority__c !=Null AND Arrived_in_Site__c!=Null Limit 1];
apexPages.CurrentPage().getParameters().put('id',String.valueOf(Oppid.id));
ApexPages.StandardController sc = new ApexPages.StandardController(Oppid);
VisitRaisedDateTime controller= New VisitRaisedDateTime(sc);
controller.OppList= Oppid;
List<Opportunity> op=controller.getRaisedDateandTime();
}
}
Best Answer
The issue is your method is returning a boolean but in your test you are trying to assign it to a List of Opportunities:
List<Opportunity> op = controller.getRaisedDateandTime();
Try the following line instead of the one above:
Boolean differentHours = controller.getRaisedDateandTime();
In addition I would recommend reading the Apex Testing Best Practices and apply them to your test cases.
EDIT:
Upon further inspection there is issues with the lines -
ApexPages.StandardController sc = new ApexPages.StandardController(Oppid);
OppId in this case is actually defined as a List of Id's. The ApexPages.StandardController expects an instance of an sObject (e.g. Opportunity) therefore this should be changed to something like the following:
apexPages.CurrentPage().getParameters().put('id',String.valueOf(Oppid[0].id));
Fixing the lines highlighted will allow your test class to compile which in turn will allow the test method to provide some coverage on your method.