+ Start a Discussion
DivyaReddyDivyaReddy 

Regarding test case

Hi

This is my trigger and can any one help me with the test case



trigger trgPopulateProcessConsultant on Case (before Insert, before Update) {

    If(Trigger.New.Size() == 1)
    {
        If(Trigger.New[0].AccountId != Null)
        {
            Opportunity[] Opp = [Select Id, Sales_Consultant__c, Process_Consultant__c from Opportunity where AccountId = :Trigger.New[0].AccountId Order By CreatedDate Desc];
            
            If(Opp.Size() > 0)
            {
                Trigger.New[0].Opportunity_Name__c = Opp[0].Id;
                If((Opp[0].Process_Consultant__c != Null) && (Opp[0].Process_Consultant__c != '')&& (Trigger.New[0].Type.contains('Suggestions')) && (Trigger.New[0].Case_Reason__c.contains('Process'))) 
                    Trigger.New[0].OwnerId = Opp[0].Process_Consultant__c;
                else if((Opp[0].Sales_Consultant__c != Null) && (Opp[0].Sales_Consultant__c != '') && (Trigger.New[0].Type.contains('Suggestions')) && (Trigger.New[0].Case_Reason__c.contains('Sales')))
                    Trigger.New[0].OwnerId = Opp[0].Sales_Consultant__c;
            }
        }
    }

I am getting 75 % code coverage . i want to made it 100% , Red lines r not covered

 

My test case,

 

@isTest
private class trgPopulateProcessConsultant_TESTS{
    static testMethod void mytestclass(){
    //Case c = [Select id from Case limit 1];
    Account a = [Select id, name from account limit 1 ];
    Opportunity o = [Select id ,name from opportunity where Process_Consultant__c = '' limit 1 ];
    Case c = new Case ();
    c.accountid = a.id;
    c.Opportunity_Name__c  = o.id;
    c.CurrencyIsoCode = '';
    c.Status = 'New';
    c.Origin = 'Email';
    c.Case_Reason__c = 'Process';
    c.Type = 'Suggestions';
        Insert c;
        }
}


 
}

vishal@forcevishal@force

Hello,

 

First of all, what I would suggest you is "Never" query in your testClasses, you should instead create test data for all objects that you'll need.

 

So instead of querying for Account & Opportunity, you can create new test record for both these objects.

 

 

Regarding your coverage, 

Opportunity o = [Select id ,name from opportunity where Process_Consultant__c = '' limit 1 ];

 

here you are getting that opportunity which has a BLANK Process_Consultant__c field, which DOES NOT satisfy your IF Condition, hence it is not covering this line :

  Trigger.New[0].OwnerId = Opp[0].Process_Consultant__c;

 

So , all you need to do is prepare test data, which has the values that satisfy your If conditions, and both your lines will be covered.

 

 

DivyaReddyDivyaReddy

Now i am getting error as portal user so i have to keep Isnot equal portal .

 

I used Usertype = , but i am unable to see pick list values.