You need to sign in to do that
Don't have an account?
test class for a controller
Hi All
I am writing a test class for a Apex class. It only covered 12% of the apex class. Here is my apex class and i mentioned in Red the part of the class covered and the part in blue is not covered. I am new to writing the test classes
public with sharing class accountCreditApprovalController {
Private string comment = '';
public String acId= ApexPages.currentPage().getParameters().get('acId');
public Account_Credit__c ac;
public accountCreditApprovalController(){
ac = [select Id, Name, Account__c, Credit_Status__c, Associated_TN__c, Credit_Reason__c,
Department_Responsible__c, Requested_Credit_Amount__c, Credit_Reason_Comments__c,
Account__r.Name from Account_Credit__c where id= :acId];
}
public Account_Credit__c getAc(){
return ac;
}
public string getComment(){
return comment;
}
public void setComment(string Value){
comment= Value;
}
public PageReference reject1() {
PageReference pr = null;
try{
ProcessInstanceWorkitem piwi = [Select ProcessInstance.Status, ProcessInstance.TargetObjectId, ProcessInstanceId,OriginalActorId,Id,ActorId From ProcessInstanceWorkitem
where ProcessInstance.TargetObjectId = : acId and ProcessInstance.Status = 'Pending' and OriginalActorId =: UserInfo.getUserId() order by CreatedDate desc limit 1];
system.debug('---Piwi---' + piwi);
Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
req.setComments(comment);
req.setAction('Reject');
req.setWorkitemId(piwi.Id);
// Submit the request for approval
Approval.ProcessResult result = Approval.process(req);
//pr = new PageReference('/home/home.jsp');
pr = new PageReference('/apex/mobileApprovalList?sfdc.tabName=01r40000000LnKP');
}
catch(Exception ex){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,ex.getMessage()));
}
return pr;
}
public PageReference approve1(){
PageReference pr = null;
try{
ProcessInstanceWorkitem piwi = [Select ProcessInstance.Status, ProcessInstance.TargetObjectId, ProcessInstanceId,OriginalActorId,Id,ActorId From ProcessInstanceWorkitem
where ProcessInstance.TargetObjectId = : acId and ProcessInstance.Status = 'Pending' and OriginalActorId =: UserInfo.getUserId() order by CreatedDate desc limit 1];
system.debug('---Piwi---' + piwi);
Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
req.setComments(comment);
req.setAction('Approve');
req.setWorkitemId(piwi.Id);
// Submit the request for approval
Approval.ProcessResult result = Approval.process(req);
//pr = new PageReference('/home/home.jsp');
pr = new PageReference('/apex/mobileApprovalList?sfdc.tabName=01r40000000LnKP');
}
catch(Exception ex){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,ex.getMessage()));
}
return pr;
}
}
Here is my Test class
}
}
You need to call the methods in your class in order to test them. Also you should prepare an Account and Account_Credit__c object in your test method instead of relying on one existing in the database. Your test would fail in a developer sandbox where no data exists.
This link will help you.
http://wiki.developerforce.com/index.php/An_Introduction_to_Apex_Code_Test_Methods