+ Start a Discussion
DivyaReddyDivyaReddy 

Help on test case ..

Hi

 

I got 21 % can any one help to make me code coverage in between 80 % to 100 %.

 

trigger trgCaptureConsultants on Opportunity (before Insert, before Update) {

    If(Trigger.New.Size() == 1)
    {
        If(Trigger.IsInsert)
        {
            String ProfileName = [SELECT Profile.Name FROM User where Id = :Trigger.New[0].OwnerId].Profile.Name;    
            
            If(ProfileName.contains('Sales'))//&& (Trigger.New[0].Sales_Consultant__c == Null || Trigger.New[0].Sales_Consultant__c == '')
            {
                Trigger.New[0].Sales_Consultant__c = Trigger.New[0].OwnerId;
                If(Trigger.New[0].Sales_Consultan__c == null)
                {
                Trigger.New[0].Sales_Consultan__c = Trigger.New[0].OwnerId;
                }


            }
            if(ProfileName.contains('Process') ) //&& (Trigger.New[0].Process_Consultant__c == Null || Trigger.New[0].Process_Consultant__c == ''))
            {    
                Trigger.New[0].Process_Consultant__c = Trigger.New[0].OwnerId;
                If(Trigger.New[0].Process_Consultant_User__c == null)
                {
                Trigger.New[0].Process_Consultant_User__c = Trigger.New[0].OwnerId;
                }
            }
            if( ProfileName.contains('Evaluation')) 
            {
                Trigger.New[0].Process_Consultant__c = Trigger.New[0].OwnerId;
                Trigger.New[0].Process_Consultant_User__c = Trigger.New[0].OwnerId;
            }
        } 
        Else If(Trigger.IsUpdate)
        {
            String ProfileName = [SELECT Profile.Name FROM User where Id = :Trigger.New[0].OwnerId].Profile.Name;    
            If(Trigger.New[0].OwnerId != Trigger.Old[0].OwnerId)
            {
                
                
                If(ProfileName.contains('Sales'))
                {
                    Trigger.New[0].Sales_Consultant__c = Trigger.New[0].OwnerId;
                    
                    If(Trigger.New[0].Sales_Consultant__c != null)
                    {
                    Trigger.New[0].Sales_Consultan__c = Trigger.New[0].OwnerId;
                    }

                }
                if(ProfileName.contains('Process'))
                 {
                    Trigger.New[0].Process_Consultant__c = Trigger.New[0].OwnerId;
                    
                    If(Trigger.New[0].Process_Consultant__c != null)
                    {
                    Trigger.New[0].Process_Consultant_User__c = Trigger.New[0].OwnerId;
                    }
                 }
                 
            }
            if(ProfileName.contains('Evaluation'))
            {
                Trigger.New[0].Process_Consultant__c = Trigger.New[0].OwnerId;
                Trigger.New[0].Process_Consultant_User__c = Trigger.New[0].OwnerId;
            }
                
                    
        }
  
    }    
    
    /* The below section of code checks whether the Product associated with the opportunity
    contains some specific set of characters. if they are found then the record type of the 
    Opportunity is updated accordingly*/
    
    OpportunityLineItem[] OppLineItems = [SELECT PricebookEntry.Product2Id, PricebookEntry.Product2.Name FROM OpportunityLineItem where Opportunity.Id In :Trigger.New];
    
    If(OppLineItems.Size()==1)
    {
        String ProductName = OppLineItems[0].PricebookEntry.Product2.Name;        
        
        Map<String,Id> RecordTypeMap = New Map<String,Id>();
        For(RecordType RecType : [SELECT Id, Name, SobjectType FROM RecordType Where SobjectType = 'Opportunity'])// and Name in ('CAN','AUS','HSW','DEN','JAS','VISA','Evaluation')])
        {
            RecordTypeMap.put(RecType.Name , RecType.Id);
        }
      /* Record Type should change only when the Opportunity is moving to Process*/ 
        
        If (Trigger.New[0].StageName == 'Evaluation Sale Made' && Trigger.New[0].Apply_For_Exception_Withdrawal_Refund__c != TRUE )
        {
           Trigger.New[0].RecordTypeId = RecordTypeMap.get('Evaluation');
        }
        Else If (Trigger.New[0].StageName == 'Send to Process Consultant'&& Trigger.New[0].Apply_For_Exception_Withdrawal_Refund__c != TRUE   )
        {   
            For(String RecTypeString: RecordTypeMap.KeySet())
            {
                If(ProductName.startsWith(RecTypeString))
                    Trigger.New[0].RecordTypeId = RecordTypeMap.get(RecTypeString);
            }

       
        }
        
      }

}

 

 

Mine test class :

 

@isTest
private class trgCaptureConsultants_TESTS{
    static testMethod void mytestclass(){
   Account a = [Select id, name from account limit 1 ];
     //Opportunity o = [Select id ,name,Process_Consultant__c from opportunity where accountid =: a.id and Process_Consultant__c != ''];
     User u = [Select Profile.Name,id FROM User where Profile.Name != 'Sales' limit 1 ];
  
    Opportunity o = new Opportunity();
    o.name = 'raja';
    o.Accountid = a.id;
    o.StageName = 'Opened';
    o.CloseDate = system.today();
   o.Process_Consultant__c = '0053000000203VvAAI';
    o.Sales_Consultan__c = null ;
    o.StageName = 'Evaluation Sale Made';
    o.Apply_For_Exception_Withdrawal_Refund__c = False ;
    insert o;

    Case c = new Case ();
    c.accountid = a.id;
    c.OwnerId  = o.Process_Consultant__c;
    c.Opportunity_Name__c  = o.id;
    c.Status = 'New';
    c.Origin = 'Email';
    c.Case_Reason__c = 'Process';
    c.Type = 'Suggestions';
      try{
       Insert c;
    Update c;
       }
       catch (Exception e)
      {}
       
       
      
      }
    }

ashish raiashish rai

Hello Divya,

                   Well first of all you have to insert user inside the test method having profile Custom: Sales Profile and then insert and opportunity  with this field Process_Consultant__c  value as blank or null. It will cover your first if part now again create a user or update the user profile which contains Process inside the profile name. After than again create a new Opportunity with the same value that you used in privious insert. Your Test Mathod should be look like this:

 

@isTest
private class trgCaptureConsultants_TESTS

{
    static testMethod void mytestclass()

   {

       Profile p = [select id from profile where name='Custom: Sales Profile'];
        User u1 = new User(alias = 'standt2', email='standarduser@test12.com',
        emailencodingkey='UTF-8', lastname='Testing1', languagelocalekey='en_US',
        localesidkey='en_US', profileid = p.Id,firstname='Heather',
        timezonesidkey='America/Los_Angeles', username='standarduser@test12.com');
        insert u1; 

        Opportunity o = new Opportunity();

        o.name = 'raja';
        o.Accountid = a.id;
        o.StageName = 'Opened';
       o.CloseDate = system.today();
       o.Process_Consultant__c = '0053000000203VvAAI';
       o.Sales_Consultan__c = null ;
       o.StageName = 'Evaluation Sale Made';
       o.Apply_For_Exception_Withdrawal_Refund__c = False ;
       insert o;

Insert User2;

insert opportunity2;

insert user3;

insert opportunity3;

    }

}

Again create a new user with different profile that matches your Profile name Criteria.Insert that user and again create a new opportunity with same value privious one it will cover another if block and so on. Similary do this for update. Think this will help you to cover your trigger. If there is any issue than ping me on main.hu.9a@gmail.com.

 

  

ashish raiashish rai

Hello,

    Sorry i forgot to mention that u can create different profile also inside your test method. Make sure that you will create the profile before the user and pass the different profileid to the users.

DivyaReddyDivyaReddy

hey i got 50%

 

now i am unable to cover this part :

 

 Else If(Trigger.IsUpdate)
        {
            String ProfileName = [SELECT Profile.Name FROM User where Id = :Trigger.New[0].OwnerId].Profile.Name;    
            If(Trigger.New[0].OwnerId != Trigger.Old[0].OwnerId)
            {
                
                
                If(ProfileName.contains('Sales'))
                {
                    Trigger.New[0].Sales_Consultant__c = Trigger.New[0].OwnerId;
                    
                    If(Trigger.New[0].Sales_Consultant__c != null)
                    {
                    Trigger.New[0].Sales_Consultan__c = Trigger.New[0].OwnerId;
                    }

                }
                if(ProfileName.contains('Process'))
                 {
                    Trigger.New[0].Process_Consultant__c = Trigger.New[0].OwnerId;
                    
                    If(Trigger.New[0].Process_Consultant__c != null)
                    {
                    Trigger.New[0].Process_Consultant_User__c = Trigger.New[0].OwnerId;
                    }
                 }
                 
            }

 

 

My test class :

 

@isTest
private class trgCaptureConsultants_TESTS{
static testMethod void mytestclass(){
      Account a = [Select id, name from account limit 1 ];
   user u = [SELECT Profile.Name FROM User where profile.name = 'Sales Consultant' limit 1 ];
 
Opportunity o = new Opportunity();
o.name = 'raja';
o.OwnerId  = u.id;
o.Accountid = a.id;
o.StageName = 'Opened';
o.CloseDate = system.today();
o.Process_Consultant__c = '0053000000203VvAAI';
o.Sales_Consultan__c = null ;
o.StageName = 'Evaluation Sale Made';
o.Apply_For_Exception_Withdrawal_Refund__c = False ;
try
{
insert o;
}
catch (Exception e)
{}

Case c = new Case ();
c.accountid = a.id;
c.OwnerId  = o.Process_Consultant__c;
c.Opportunity_Name__c  = o.id;
c.Status = 'New';
c.Origin = 'Email';
c.Case_Reason__c = 'Process';
c.Type = 'Suggestions';
      try{
         Insert c;
Update c;
         }
         catch (Exception e)
      {}
           
           
        
      }
      static testMethod void mytestclass1(){
      Account a = [Select id, name from account limit 1 ];
   user u = [SELECT Profile.Name FROM User where profile.name = 'Process Consultant' limit 1 ];
 
Opportunity o = new Opportunity();
o.name = 'raja';
o.OwnerId  = u.id;
o.Accountid = a.id;
o.StageName = 'Opened';
o.CloseDate = system.today();
o.Process_Consultant__c = '0053000000203VvAAI';
o.Sales_Consultan__c = null ;
o.StageName = 'Evaluation Sale Made';
o.Apply_For_Exception_Withdrawal_Refund__c = False ;
try
{
insert o;
}
catch (Exception e)
{}

Case c = new Case ();
c.accountid = a.id;
c.OwnerId  = o.Process_Consultant__c;
c.Opportunity_Name__c  = o.id;
c.Status = 'New';
c.Origin = 'Email';
c.Case_Reason__c = 'Process';
c.Type = 'Suggestions';
      try{
         Insert c;
Update c;
         }
         catch (Exception e)
      {}
           
           
        
      }
     
         static testMethod void mytestclass2(){
      Account a = [Select id, name from account limit 1 ];
   user u = [SELECT Profile.Name FROM User where profile.name = 'Evaluation Consultant' limit 1 ];
 
Opportunity o = new Opportunity();
o.name = 'raja';
o.OwnerId  = u.id;
o.Accountid = a.id;
o.StageName = 'Opened';
o.CloseDate = system.today();
o.Process_Consultant__c = '0053000000203VvAAI';
o.Sales_Consultan__c = null ;
o.StageName = 'Evaluation Sale Made';
o.Apply_For_Exception_Withdrawal_Refund__c = False ;
try
{
insert o;
}
catch (Exception e)
{}

Case c = new Case ();
c.accountid = a.id;
c.OwnerId  = o.Process_Consultant__c;
c.Opportunity_Name__c  = o.id;
c.Status = 'New';
c.Origin = 'Email';
c.Case_Reason__c = 'Process';
c.Type = 'Suggestions';
      try{
         Insert c;
Update c;
         }
         catch (Exception e)
      {}
           
           
        
      }
      /*-ve test case*/
      static testMethod void mytestclass3(){
      Account a = [Select id, name from account limit 1 ];
   user u = [SELECT Profile.Name FROM User where profile.name != 'Sales Consultant' limit 1 ];
 
Opportunity o = new Opportunity();
o.name = 'raja';
o.OwnerId  = u.id;
o.Accountid = a.id;
o.StageName = 'Opened';
o.CloseDate = system.today();
o.Process_Consultant__c = '0053000000203VvAAI';
o.Sales_Consultan__c = null ;
o.StageName = 'Evaluation Sale Made';
o.Apply_For_Exception_Withdrawal_Refund__c = False ;
  
try
{
insert o;
}
catch (Exception e)
{}

Case c = new Case ();
c.accountid = a.id;
c.OwnerId  = o.Process_Consultant__c;
c.Opportunity_Name__c  = o.id;
c.Status = 'New';
c.Origin = 'Email';
c.Case_Reason__c = 'Process';
c.Type = 'Suggestions';
      try{
         Insert c;
Update o;
         }
         catch (Exception e)
      {}
   }
  
}

ashish raiashish rai

Hello,

      To cover the update part you have to update the opportunity but makesure that you have to update the opportunity ownerid with different user id.For example;

 @isTest
private class trgCaptureConsultants_TESTS

{

static testMethod void mytestclass()

   {

       Profile p = [select id from profile where name='Custom: Sales Profile'];
        User u1 = new User(alias = 'standt2', email='standarduser@test12.com',
        emailencodingkey='UTF-8', lastname='Testing1', languagelocalekey='en_US',
        localesidkey='en_US', profileid = p.Id,firstname='Heather',
        timezonesidkey='America/Los_Angeles', username='standarduser@test12.com');
        insert u1; 

        Insert u2;

        Opportunity o = new Opportunity();

        o.name = 'raja';

        o.ownerId=u1.id;
        o.Accountid = a.id;
        o.StageName = 'Opened';
       o.CloseDate = system.today();
       o.Process_Consultant__c = '0053000000203VvAAI';
       o.Sales_Consultan__c = null ;
       o.StageName = 'Evaluation Sale Made';
       o.Apply_For_Exception_Withdrawal_Refund__c = False ;
       insert o;

       o.ownerId=u2.id;

       update o;

}

}

 

This will help you to over the update part.Now update the same at all insert position but after insert write this..