You need to sign in to do that
Don't have an account?
shakila G
Apex class Code Coverage error ?
Kindly let me know, how to write a test class for this Apex class. I wrote a test class but it's not covering code coverage
public class AggregateTasklist{
public List<AggregateResult> Result {get;set;}
List<Account> ListAct= New List<Account>();
public List<AddWrapperField> wraplist{get;set;}
public AggregateTasklist(ApexPages.StandardController controller) {
}
public void GetRecords(){
String userid=UserInfo.getUserId();
ListAct=[select ID,ownerId from Account where ownerId =:userid];
If(ListAct.size()>0) {
For(Account act:ListAct) {
wraplist=new List<AddWrapperField>();
Result=[select Max(Time_New__c) LastDueDate, Task.Account.Name Name from Task where ownerId =:userid and Time_New__c!=Null
group By Task.Account.Name Having Max(Time_New__c)<LAST_N_DAYS:110];
}
}
for(AggregateResult res:Result){
wraplist.add(new AddWrapperField(Res));
}
}
public class AddWrapperField{
public Date LastDueDate{get;set;}
public String Name {get;set;}
AddWrapperField(AggregateResult res){
this.LastDueDate=(Date)res.get('LastDueDate');
this.Name =(String)res.get('Name');
}
}
}
My Test Class:
@isTest(SeeAllData=false) class AggregateResultTest {
@isTest static void test() {
Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
User u = new User(Alias = 'Test', Email='Test@testorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='shakila@gmail.com');
insert u;
Account a = new Account(Name = 'ApprovalTest' ,Lock_record__C=FALSE ,ownerid=u.ID);
insert a;
Contact con = new COntact();
con.email='test@fmail.co';
con.ACCOUNTID=a.ID;
con.ownerid=a.ownerid;
con.lastname='test' ;
insert con;
System.runAs(u)
{
Task testTTask= new Task(Subject = 'EventTest', Description = 'Description',WhoId= con.Id ,
ownerid=con.ownerid);
Insert testTTask;
Test.startTest();
testTTask= new task(Subject = 'EventTest', Description = 'Description',WhoId= con.Id ,
ownerid=con.ownerid);
Database.SaveResult result = Database.insert(testTTask, false);
Test.stopTest();
}
List<Account> listact=New List<Account>{[select OwnerID from Account Where ownerID=:u.ID ]};
AggregateResult[] results = [SELECT Max(Time_New__c), WhatId FROM Task GROUP BY WhatId ];
}
}
public class AggregateTasklist{
public List<AggregateResult> Result {get;set;}
List<Account> ListAct= New List<Account>();
public List<AddWrapperField> wraplist{get;set;}
public AggregateTasklist(ApexPages.StandardController controller) {
}
public void GetRecords(){
String userid=UserInfo.getUserId();
ListAct=[select ID,ownerId from Account where ownerId =:userid];
If(ListAct.size()>0) {
For(Account act:ListAct) {
wraplist=new List<AddWrapperField>();
Result=[select Max(Time_New__c) LastDueDate, Task.Account.Name Name from Task where ownerId =:userid and Time_New__c!=Null
group By Task.Account.Name Having Max(Time_New__c)<LAST_N_DAYS:110];
}
}
for(AggregateResult res:Result){
wraplist.add(new AddWrapperField(Res));
}
}
public class AddWrapperField{
public Date LastDueDate{get;set;}
public String Name {get;set;}
AddWrapperField(AggregateResult res){
this.LastDueDate=(Date)res.get('LastDueDate');
this.Name =(String)res.get('Name');
}
}
}
My Test Class:
@isTest(SeeAllData=false) class AggregateResultTest {
@isTest static void test() {
Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
User u = new User(Alias = 'Test', Email='Test@testorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='shakila@gmail.com');
insert u;
Account a = new Account(Name = 'ApprovalTest' ,Lock_record__C=FALSE ,ownerid=u.ID);
insert a;
Contact con = new COntact();
con.email='test@fmail.co';
con.ACCOUNTID=a.ID;
con.ownerid=a.ownerid;
con.lastname='test' ;
insert con;
System.runAs(u)
{
Task testTTask= new Task(Subject = 'EventTest', Description = 'Description',WhoId= con.Id ,
ownerid=con.ownerid);
Insert testTTask;
Test.startTest();
testTTask= new task(Subject = 'EventTest', Description = 'Description',WhoId= con.Id ,
ownerid=con.ownerid);
Database.SaveResult result = Database.insert(testTTask, false);
Test.stopTest();
}
List<Account> listact=New List<Account>{[select OwnerID from Account Where ownerID=:u.ID ]};
AggregateResult[] results = [SELECT Max(Time_New__c), WhatId FROM Task GROUP BY WhatId ];
}
}
Let us know if this will help you
All Answers
Let us know if this will help you
Thanks for the reply,
Still am unable complete code coverage., Kindly let me know what i missed
My Apex class Code Coverage 70 %
public class AggregateTasklist{
public List<AggregateResult> Result {get;set;}
List<Account> ListAct= New List<Account>();
public List<AddWrapperField> wraplist{get;set;}
public list<AddWrapperField> getwraplist(){
return wraplist;
}
public AggregateTasklist(ApexPages.StandardController controller) {
}
public void GetRecords(){
String userid=UserInfo.getUserId();
ListAct=[select ID,ownerId from Account where ownerId =:userid];
If(ListAct.size()>0) {
For(Account act:ListAct) {
wraplist=new List<AddWrapperField>();
Result=[select Max(Time_New__c) LastDueDate, Task.Account.Name Name from Task where ownerId =:userid and Time_New__c!=Null
group By Task.Account.Name Having Max(Time_New__c)<LAST_N_DAYS:110];
}
}
for(AggregateResult res:Result){
wraplist.add(new AddWrapperField(Res));
}
}
public class AddWrapperField{
public Date LastDueDate{get;set;}
public String Name {get;set;}
AddWrapperField(AggregateResult res){
this.LastDueDate=(Date)res.get('LastDueDate');
this.Name =(String)res.get('Name');
}
}
}
My Test Class
@isTest(SeeAllData=false)
class AggregateResultTest {
@isTest
static void test()
{
Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
User u = new User(Alias = 'Test', Email='Test@testorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='shakila@gmail.com');
insert u;
Account a = new Account(Name = 'ApprovalTest' ,Lock_record__C=FALSE);
insert a;
Contact con = new COntact();
con.email='test@fmail.co';
con.ACCOUNTID=a.ID;
con.lastname='test' ;
insert con;
Task testTTask= new Task(Subject = 'EventTest', Description = 'Description',WhoId= con.Id,
whatid=a.ID,OwnerID=U.ID);
Insert testTTask;
Test.startTest();
ApexPages.CurrentPage().getParameters().put('id',U.Id);
Apexpages.Standardcontroller SC= new Apexpages.StandardController(U);
AggregateTasklist obj = new AggregateTasklist(sc);
obj.GetRecords();
obj.Getwraplist();
Test.stopTest();
}
}
testTTask.Time_New__c = System.now();// add value according to data type
Try below code
public with sharing class AggregateTasklist{
public List<AggregateResult> Result {get;set;}
List<Account> ListAct= New List<Account>();
public List<AddWrapperField> wraplist{get;set;}
public list<AddWrapperField> getwraplist(){
return wraplist;
}
public AggregateTasklist(ApexPages.StandardController controller) {
}
public void GetRecords(){
String userid=UserInfo.getUserId();
ListAct=[select ID,ownerId from Account where ownerId =:userid];
If(ListAct.size()>0) {
For(Account act:ListAct) {
wraplist=new List<AddWrapperField>();
Result=[select Max(Time_New__c) LastDueDate, Task.Account.Name Name from Task where ownerId =:userid and Time_New__c!=Null
group By Task.Account.Name Having Max(Time_New__c)<LAST_N_DAYS:110];
}
}
for(AggregateResult res:Result){
wraplist.add(new AddWrapperField(Res));
}
}
public class AddWrapperField{
public Date LastDueDate{get;set;}
public String Name {get;set;}
AddWrapperField(AggregateResult res){
this.LastDueDate=(Date)res.get('LastDueDate');
this.Name =(String)res.get('Name');
}
}
}
Test Class:
@isTest(SeeAllData=false)
class AggregateResultTest {
@isTest
static void test()
{
List<AggregateResult> Result=New List<AggregateResult>();
Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
User u = new User(Alias = 'Test', Email='Test@testorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='shakila@gmail.com');
insert u;
Account a = new Account(Name = 'ApprovalTest' ,Lock_record__C=FALSE);
insert a;
Contact con = new COntact();
con.email='test@fmail.co';
con.ACCOUNTID=a.ID;
con.lastname='test' ;
insert con;
Task testTTask= new Task(Subject = 'EventTest',
ActivityDate=Date.newInstance(2018, 03, 23), // Formula field of(Time_New__c)
Description = 'Description',
WhoId= con.Id,
whatid=a.ID,OwnerID=U.ID);
Insert testTTask;
Test.StartTest();
list<AddWrapperField> wraplist= new list<AddWrapperField>();
Result=[select Max(Time_New__c) LastDueDate, Task.Account.Name Name from Task where ownerId =:U.ID and Time_New__c!=Null
group By Task.Account.Name Having Max(Time_New__c)<LAST_N_DAYS:110];
for(AggregateResult res:Result){
wraplist.add(new AddWrapperField(Res));
}
ApexPages.CurrentPage().getParameters().put('id',U.Id);
Apexpages.Standardcontroller SC= new Apexpages.StandardController(testTTask);
AggregateTasklist obj = new AggregateTasklist(sc);
obj.GetRecords();
obj.Getwraplist();
Test.StopTest();
}
public class AddWrapperField{
public Date LastDueDate{get;set;}
public String Name {get;set;}
AddWrapperField(AggregateResult res){
this.LastDueDate=(Date)res.get('LastDueDate');
this.Name =(String)res.get('Name');
}
}
}
You can try below code
Time_New__c= Activitydate
Let us know if this will help you
While try to validate the code am getting following error.
AggregateTasklistApex Class2516By Task.Account.Name Having Max(Time_New__c)<LAST_N_DAYS:110 ^ ERROR at Row:2:Column:47 value of filter criterion for expression MAX(Time_New__c) must be of type string and should be enclosed in quotes