You need to sign in to do that
Don't have an account?
swain 10
Please review my test class ,its failing.
Use Case whenever we will select a particular travel request(through lookup) from expense then data
from that travel request (Name + account, related to that travel request) concatenately should
populate Claim purpose field in expense.
Datapopulate:-
public with sharing class DataPopulate {
public Travel_Request__c trq {
get;
set;
}
private ApexPages.StandardController stdCtrl;
public DataPopulate(ApexPages.StandardController std) {
stdCtrl = std;
}
public void doInsert() {
Expense__c exp = (Expense__c) stdCtrl.getRecord();
if (exp.Travel_Request__c == null) {
return;
} else {
trq = [select Name , Proposed_Client_to_Visit__r.Name, Status__c from Travel_Request__c where Id =
: exp.Travel_Request__c LIMIT 1];
System.debug('The record fetched '+trq);
//exp.Branch__c = trq.Branch__c;
//exp.Period_To__c = trq.Travel_Start_Date__c;
// exp.Period_From__c = trq.Travel_End_Date__c;
//exp.Department__c=trq.Department__c;
exp.Claim_Purpose__c= trq.Name +''+ ' -- '+''+ trq.Proposed_Client_to_Visit__r.Name ;
upsert exp;
System.debug('The record insert '+exp);
}
}
}
test class:
//Test class for DataPopulate
@isTest
public class DataPopulateTest
{
//setup test data for Class
@testSetup static void testSetDate()
{
Travel_Request__c trq = new Travel_Request__c();
trq.Name='TestTravel' ;
trq.Proposed_Client_to_Visit__c='T-System';
insert trq;
Expense__c expObj = new Expense__c();
expObj.Travel_Request__c='TestTravel';
// expObj.Period_To__c= system.today();
//expObj.Period_From__c= system.today();
insert expObj;
}
@isTest static void testPopulate()
{
Travel_Request__c trq = [SELECT id from Travel_Request__c where Name = 'TestTravel' ][0];
Test.setCurrentPage(page.Expense);
Apexpages.currentPage().getParameters().put('Id',trq.Id);
ApexPages.StandardController stdCon = new ApexPages.StandardController(trq);
DataPopulate dClassObj = new DataPopulate(stdCon);
dClassObj.doInsert();
// List<Expense__c> expObj=[SELECT Claim_Purpose__c from Expense__c WHERE
Travel_Request__c=:trq.id Limit 1];
// Boolean result = expObj.equals('TestTravelT-System');
// System.assertEquals(result,false);
}
}
from that travel request (Name + account, related to that travel request) concatenately should
populate Claim purpose field in expense.
Datapopulate:-
public with sharing class DataPopulate {
public Travel_Request__c trq {
get;
set;
}
private ApexPages.StandardController stdCtrl;
public DataPopulate(ApexPages.StandardController std) {
stdCtrl = std;
}
public void doInsert() {
Expense__c exp = (Expense__c) stdCtrl.getRecord();
if (exp.Travel_Request__c == null) {
return;
} else {
trq = [select Name , Proposed_Client_to_Visit__r.Name, Status__c from Travel_Request__c where Id =
: exp.Travel_Request__c LIMIT 1];
System.debug('The record fetched '+trq);
//exp.Branch__c = trq.Branch__c;
//exp.Period_To__c = trq.Travel_Start_Date__c;
// exp.Period_From__c = trq.Travel_End_Date__c;
//exp.Department__c=trq.Department__c;
exp.Claim_Purpose__c= trq.Name +''+ ' -- '+''+ trq.Proposed_Client_to_Visit__r.Name ;
upsert exp;
System.debug('The record insert '+exp);
}
}
}
test class:
//Test class for DataPopulate
@isTest
public class DataPopulateTest
{
//setup test data for Class
@testSetup static void testSetDate()
{
Travel_Request__c trq = new Travel_Request__c();
trq.Name='TestTravel' ;
trq.Proposed_Client_to_Visit__c='T-System';
insert trq;
Expense__c expObj = new Expense__c();
expObj.Travel_Request__c='TestTravel';
// expObj.Period_To__c= system.today();
//expObj.Period_From__c= system.today();
insert expObj;
}
@isTest static void testPopulate()
{
Travel_Request__c trq = [SELECT id from Travel_Request__c where Name = 'TestTravel' ][0];
Test.setCurrentPage(page.Expense);
Apexpages.currentPage().getParameters().put('Id',trq.Id);
ApexPages.StandardController stdCon = new ApexPages.StandardController(trq);
DataPopulate dClassObj = new DataPopulate(stdCon);
dClassObj.doInsert();
// List<Expense__c> expObj=[SELECT Claim_Purpose__c from Expense__c WHERE
Travel_Request__c=:trq.id Limit 1];
// Boolean result = expObj.equals('TestTravelT-System');
// System.assertEquals(result,false);
}
}
May I request you to please check for Test Class Generator App from APP Exchange.Please refer the below link.
- https://appexchange.salesforce.com/listingDetail?listingId=a0N3A00000EFozgUAD
I hope it will be helpful.Please mark it as best answer if the information is informative.
Best Regards
Rahul Kumar