+ Start a Discussion
arun rupesharun rupesh 

i want to increase test class codeverage by 80%

hi we have one apex class and one test class.if  i run test class code will covered only 54 % i need to increase this to 80% could u please help me to increase test code coverege increases

apex class
===========================
public class DataCashProcessController{
  public String statusMsg {get;set;}
  public String taskID{get;set;}
  public String taskSubject{get;set;}
  public String taskCaseId{get;set;}
  
  /**
  @Summary: Designed to query payment trasaction and update in OneCRM
  **/
  public PageReference validateTransactionStatus(){
    String transReferenceID=apexpages.currentpage().getparameters().get('dts_reference');
    System.debug('dts_reference---->'+transReferenceID);
    
    if(!String.isEmpty(transReferenceID)){
    List<Task> lstTaskRecords =[Select Datacash_Reference__c,DISE_Account_Number__c,Session_Id__c,Reason__c  From Task where Datacash_Reference__c=:transReferenceID ];
    Set<String> setSubscriptionAcc = new Set<String>();
    
    if(lstTaskRecords !=null && !lstTaskRecords.isEmpty()){
        for(Task objTask :lstTaskRecords){
            taskID = objTask.Id;
            if(!String.isEmpty(objTask.DISE_Account_Number__c)){
                setSubscriptionAcc.add(objTask.DISE_Account_Number__c);
            }
            
        }
     }
     Subscription_Account__c subAcc;
     
     if(setSubscriptionAcc.size()>0){
        List<Subscription_Account__c> lstSubAccs=[Select Name,Billing_Environment__c From Subscription_Account__c where Name IN :setSubscriptionAcc];
        if(lstSubAccs !=null && !lstSubAccs.isEmpty()){
            for(Subscription_Account__c obj : lstSubAccs){
                subAcc=obj;
            }
        }
     }
    if(subAcc !=null)
   {    
      //Retrive and Initialize  configured values.
       String clientId;
       String EndPoint;
       String Password;
       String merchantreference; 
      if(!String.isEmpty(subAcc.Billing_Environment__c) && subAcc.Billing_Environment__c == 'Connect') {
      clientId = HelperMethods.getDataCashSetting('DataCashServiceConnect','clientId');
      EndPoint = HelperMethods.getDataCashSetting('DataCashServiceConnect','EndPoint');
      Password = HelperMethods.getDataCashSetting('DataCashServiceConnect','Password');
      }else if(!String.isEmpty(subAcc.Billing_Environment__c) && subAcc.Billing_Environment__c == 'Consult'){
      clientId = HelperMethods.getDataCashSetting('DataCashServiceConsult','clientId');
      EndPoint = HelperMethods.getDataCashSetting('DataCashServiceConsult','EndPoint');
      Password = HelperMethods.getDataCashSetting('DataCashServiceConsult','Password');
      }
      
      // Instantiate a new http object
      Http http = new Http();

      // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
      HttpRequest req = new HttpRequest();
      req.setEndpoint(EndPoint);
      req.setMethod('POST');
      //Create DATACASH session request.
      String body= '<?xml version="1.0" encoding="utf-8"?>'+    
          '<Request>'+
          '<Authentication>'+
          '<client>'+clientId+'</client>'+
          '<password>'+password+'</password>'+
          '</Authentication>'+
          '<Transaction>'+
          '<HistoricTxn>'+
          '<reference>'+transReferenceID+'</reference>'+
          '<method>'+'query'+'</method>'+
          '</HistoricTxn>'+
          '</Transaction>'+
          '</Request>';

      //Set a request body
      req.setbody(body);
      system.debug('getTransactionDetails Reuest Body------------>'+body);
      //Set a max request time out    
      req.setTimeout(120000);
      //Set Request header
      req.setHeader('Content-Type', 'text/xml;charset=utf-8'); 
      // Send the request, and return a response  
      HttpResponse res = new HttpResponse();
      if(Test.isRunningTest()){
            res.setBody(GetBalance.testResponse);
            res.setStatusCode(200);
      }else
        res = http.send(req);
        
        
      system.debug(' getTransactionDetails Response Body------------>'+res.getBody());

      //get Response body and parse it.
      Dom.Document doc = res.getBodyDocument();

      //Retrieve the root element for this document.
      Dom.XMLNode transactionStatusDetails = doc.getRootElement();

      // Loop through the child elements.
      for(Dom.XMLNode child : transactionStatusDetails.getChildElements()) {
        if(child.getName()=='information'){
          statusMsg=child.getText();
          if(statusMsg.indexof('successfully') !=-1){
            statusMsg=label.MakePaymentSuccessMessage;
          }
        }else if(child.getName()=='merchantreference'){
          merchantreference=child.getText();
        }
        else if(child.getName()=='reason'){
           String newStatus = child.getText();
           if(newStatus.indexof('Invalid') !=-1){
             statusMsg=label.MakePaymentFailureMessage;
           } 
        }
      }   
    
      if(!String.isEmpty(statusMsg) && !String.isEmpty(taskID)){
        //taskID=merchantreference;
        List<Task> lstTasks =[Select Subject, Payment_amount__c, Datacash_Reference__c,Session_Id__c,Reason__c ,whatID,whoID,isSendEmail__c,hdn_isSendAnEmail__c,To_Email_Address__c  From Task where ID=:taskID ];
        if(lstTasks.size()>0){
          Task obj = lstTasks.get(0);
          obj.isSendEmail__c = obj.hdn_isSendAnEmail__c;
          obj.Success__c=statusMsg;
          system.debug('=========obj.subject====='+obj.subject);
          obj.Subject=statusMsg+' '+obj.Payment_amount__c;
          taskSubject=obj.subject;
          String casestr=obj.whatID;
          if(!String.isEmpty(casestr) && casestr.length()>15){
            taskCaseId=casestr.subString(0,15);
          }
          update lstTasks;
          
        }
      } 

    }
  } 
    
    return null;
  }
  
  public static string testResponse = '<Response><HpsTxn><AuthAttempts><Attempt><datacash_reference>4100204312607010</datacash_reference><dc_response>1</dc_response><reason>ACCEPTED</reason></Attempt></AuthAttempts><datacash_reference>4100204312607010</datacash_reference></HpsTxn><datacash_reference>4000204312606935</datacash_reference><information>You have querie a Full-HPS transaction, where the payment was successfully collected</information><merchantreference>00Tf000000HizMLEAZ</merchantreference><mode>TEST</mode><reason>ACCEPTED</reason><status>1</status><time>1420594187</time></Response>';
   
}

apex test class
=========================================

@isTest
private class DataCashProcessControllerTest {
  @isTest(SeeAllData=true)
  static void process1()
    {
      
      Account newAccount = new Account(name='TestWebService');
        insert newAccount;
        Contact newContact = new Contact(accountId = newAccount.id, lastName = 'abcde',email='a@w.com');
        insert newContact;
        Subscription_Account__c sa = new Subscription_Account__c(
            Parent_Company_Name__c = newAccount.Id,
            name = '12345',
            DISE_Account_Number__c = '12345',
            Billing_System_Id__c = '882'
        );
        insert sa;
        
        
        
        
        
      Task tsk = new Task(DISE_Account_Number__c='12345',Session_Id__c='xyz',Reason__c='xyz', Datacash_Reference__c='aaccd',subject='DTS Staus Changed',status='progress');
      insert tsk;
      
      PageReference pageRef = Page.DataCashProcess;
        Test.setCurrentPage(pageRef); 
        DataCashProcessController obj = new DataCashProcessController();
      apexpages.currentpage().getparameters().put('dts_reference','aaccd');
      
      obj.validateTransactionStatus();
      
      
      
    }
}