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
Proposal ButtonProposal Button 

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

 

 

@IsTest
private class testaccountCreditApproval
{
    static testmethod void testaccountcreditApproval()
    {
        accountCreditApprovalController acc = new accountCreditApprovalController();
        User user = [SELECT Id FROM User WHERE IsActive = true LIMIT 1];
        Account_Credit__c accac = new Account_Credit__c();
       
        accac.Credit_Status__c= 'Drafting';
        accac.Credit_Reason__c= 'Provisioning Issue';
        accac.Requested_Credit_Amount__c= Decimal.valueOf(1000);
        insert accac;
        
       }
       static testmethod void testPageReferencereject1() 
       {
           ProcessInstanceWorkitem piwi= new processInstanceworkitem();
           piwi.ProcessInstance.Status= 'Pending';
           piwi.ProcessInstance.TargetObjectId= '0014000000IA6n3';
           
           insert piwi;
           Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();

 

}

}

sfjgsfjg

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.

 

 

 

static testmethod void testaccountcreditApproval()
{
        // Prep data
        Account testAccount = new Account(Name='test account');
        insert testAccount;
        Account_Credit__c testAccountCredit = new AccountCredit__c(Name='test account credit', Account__c=testAccount.Id);
        insert testAccountCredit;
        // Pass the account id to your page        
        PageReference pageRef = Page.YOURPAGENAMEHERE;
        Test.setCurrentPage(pageRef);
        ApexPages.currentPage().getParameters().put('acId', testAccount.Id)
        accountCreditApprovalController acc = new accountCreditApprovalController();
        Test.startTest();
        acc.setComment('foo');
        String c = acc.getComment();
        
        Account_Credit__c ac = acc.getAc();
        acc.approve1();
        Test.stopTest();
 }