0

I am trying to write a test class for a web service class and i am not able to get the full code coverage can any one guide me with this would be of great help

Class :

global class CTIintegration_UpdateOpportunity{
    global class ServiceResponse{
        webservice string ResponseMessage           {get;set;}
        webservice string ResponseCode              {get;set;}
        webservice string ResponseFieldName         {get;set;}
        webservice string ResponseObjectName        {get;set;}
        webservice List<string> ResponseFieldValue  {get;set;}
        webservice string ResponseFieldRequired     {get;set;}  
        global ServiceResponse(){
            ResponseMessage         = '';
            ResponseCode            = '';
            ResponseFieldName       = '';
            ResponseObjectName      = '';
            ResponseFieldValue      = new List<string>();
            ResponseFieldRequired   = '';   
        }       
    }
    webservice static ServiceResponse UpdateOpportunityCreditCardNumber(string CreditCardNumber,string AgentCode){
        ServiceResponse response = new ServiceResponse();
        try{
            Opportunity ooptyToUpdate;
            if(null == CreditCardNumber){
                response.ResponseMessage        = 'Credit Card Number can not be empty or null';
                response.ResponseFieldName      = 'Credit Card Number';
                response.ResponseCode           = '400';
                response.ResponseFieldRequired  = 'Yes';
            }
            else if(null == AgentCode){
                response.ResponseMessage        = 'Agent Code can not be empty or null';
                response.ResponseFieldName      = 'Agent Code';
                response.ResponseCode           = '400';
                response.ResponseFieldRequired  = 'Yes';
            }
            else{
                //Integer AgentCodeInteger = Integer.ValueOF(AgentCode);
                String AgentCodeInteger = String.ValueOF(AgentCode);
                User userRecord = [select Id,C_digo_do_Agente_ToolBar__c from User where C_digo_do_Agente_ToolBar__c =: AgentCodeInteger limit 1];
                if(null != userRecord){
                    Opportunity oppty = [select Id,LastmodifiedByID,CV2_CardNumber__c,LastModifiedDate from Opportunity where LastmodifiedByID =: userRecord.Id order by LastModifiedDate desc limit 1];
                    if(null != oppty){
                        ooptyToUpdate = new Opportunity(Id = oppty.Id,CV2_CardNumber__c = CreditCardNumber);
                    }
                    else{
                        response.ResponseMessage        = 'No Opportunity found for the Agent : ' + AgentCode;
                        //response.ResponseFieldName      = 'Agent Code';
                        response.ResponseCode           = '400';
                        response.ResponseFieldRequired  = 'No';
                    }
                }
                else{
                    response.ResponseMessage        = 'Agent not found !';
                    response.ResponseFieldName      = 'Agent Code';
                    response.ResponseCode           = '400';
                    response.ResponseFieldRequired  = 'Yes';
                }
            }
            if(null != ooptyToUpdate){
                update ooptyToUpdate;
                response.ResponseMessage        = 'Success';
                response.ResponseCode           = '200';                
                response.ResponseFieldRequired  = 'No';
            }
            else{
                response.ResponseMessage        = 'No Opportunity found for the Agent : ' + AgentCode;
                //response.ResponseFieldName      = 'Agent Code';
                response.ResponseCode           = '400';
                response.ResponseFieldRequired  = 'No';
            }
        }
        catch(Exception ex){
            system.debug('Exception Occurred as : ' + ex.getMessage() +'\n @ : '+ ex.getLineNumber());
            response.ResponseMessage        = ex.getMessage();
            response.ResponseCode           = '400';
            //response.ResponseFieldName        = '';
            //response.ResponseObjectName   = '';
            //response.ResponseFieldValue   = '';
            response.ResponseFieldRequired  = 'False';
        }       
        system.debug('Response : ' + response);
        return response;             
    }
}

Test class :

@isTest
public class Test_CTIintegration_UpdateOpportunity {
    static testMethod void myUnitTestupdateOpportunity() {
            Profile p = [SELECT Id FROM Profile WHERE Name='Administrador do sistema']; 
        User u = new User(Alias = 'standt33', Email='[email protected]', 
            EmailEncodingKey='UTF-8', LastName='Testing183', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Sao_Paulo', UserName='[email protected]',
            C_digo_do_Agente_ToolBar__c  = '876');

            insert u;

            Contact contato = new Contact();
            contato.LastName = 'Roberto';
            contato.CPF__c = '73642846130';
            contato.CEP__c = '11520-020';
            insert contato;

            Account conta = new Account();
            conta.Name = 'Teste';
            insert conta;

        Opportunity oppt = new Opportunity();
                            oppt.Name ='testopp';
                            oppt.AccountID = conta.ID;
                            oppt.StageName = 'Customer Won';
                            oppt.Amount = 3000;
                            oppt.OwnerId= u.ID;
                            oppt.CloseDate = System.today();
                             oppt.CV2_CardNumber__c = '8765764324';
                           insert oppt;
        //String AgentCodeInteger = String.ValueOF(AgentCode);
        //User userRecord = [select Id,C_digo_do_Agente_ToolBar__c from User where C_digo_do_Agente_ToolBar__c =: '876' limit 1];
        // Opportunity oppty = [select Id,LastmodifiedByID,CV2_CardNumber__c,LastModifiedDate from Opportunity where LastmodifiedByID =: userRecord.Id order by LastModifiedDate desc limit 1];

        CTIintegration_UpdateOpportunity.UpdateOpportunityCreditCardNumber('8765764324','876');

                          // Opportunity oppt1 = new Opportunity();
                          // oppt1.CV2_CardNumber__c = '8765764324';
                          // oppt1.Id = oppty.Id;
                          // update oppt1;         



            }

}
2
  • What lines aren't you getting coverage?
    – EricSSH
    Apr 6, 2015 at 23:01
  • You need to test for every condition, which means all of those 400 response blocks and the catch exception part of your code. I only see you calling your method once in your test.
    – CyberJus
    Apr 7, 2015 at 0:04

0

You must log in to answer this question.

Browse other questions tagged .