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
Sabah FarhanaSabah Farhana 

66% code coverage in test class

Hi ,

I have an apex trigger as follows

trigger updateGroupLookup on Case (before insert) {
for (Case c : Trigger.new) {
   
      if(c.groupid__c!= null){
       c.group_Account__c = [select id from account where Group_ID__c=:c.groupid__c].ID;
                
         
         
    }
  }
}

The line marked in bold is not getting tested


I have written a test class for it as below.Please help.

@isTest


    private class testUpdateLookup {
  
public  static testMethod void testocc(){
   

    
      Account acc = new Account(
       name = 'temp validation account',
       Type = 'Prospect',
             Sub_Type__c='Merchant - Body',
             BillingCity='Dubai',
             BillingCountry='United Arab Emirates'
            
       );
         insert acc;
       
        Account accCreated = [Select Name,group_id__c From Account Where Id =: acc.Id LIMIT 1];
    
     Contact con=new Contact(
        LastName='blah',
        AccountId=accCreated.Id,
        level__c='Other',
        Role__c='Customer Support',
        LeadSource='Consumer Referral'
   
    );
        insert con;
    
           
           
           
            // TEST ADDING A NEW case                       
      Case cas = new Case(
       accountId = accCreated.id,
                contactId=con.Id,
                status='New',
                Priority='Medium',
                Type='Issue',
                Origin='Email',
                groupid__c=acc.group_id__c
       );
         insert cas;

//groupid null
       Case cass = new Case(
       accountId = accCreated.id,
                contactId=con.Id,
                status='New',
                Priority='Medium',
                Type='Issue',
                Origin='Email'
               
       );
         insert cass;   

       
       
     }
}
Best Answer chosen by Sabah Farhana
Arunkumar RArunkumar R
Can you try the below test class, If it is works then check assertion in the below test class.

@isTest
public class testUpdateLookup {

	static testMethod void testocc(){

	Account acc = new Account(
	name = 'temp validation account',
	Type = 'Prospect',
	Sub_Type__c='Merchant - Body',
	BillingCity='Dubai',
	BillingCountry='United Arab Emirates');
	insert acc;

	Account accCreated = [Select Name,group_id__c From Account Where Id =: acc.Id LIMIT 1];

	// TEST ADDING A NEW case                       
	Case cas = new Case(
	accountId = accCreated.id,
	contactId=con.Id,
	status='New',
	Priority='Medium',
	Type='Issue',
	Origin='Email',
	groupid__c=acc.group_id__c);
	insert cas;
  


	}
}


All Answers

Sabah FarhanaSabah Farhana
Its accustom field on the account Thanks, Sabah Farhana
Arunkumar RArunkumar R
Hi Sabah,

What is Group_ID__c hold??

You are inserting account without the value of Group_Id__c field value. 

So Insert account with the corresponding field value called Group_Id__c. Then Check code coverage.

But one suggestion remove SOQL from for Loop.
Sabah FarhanaSabah Farhana
Hi, It is an auto generated number
Sabah FarhanaSabah Farhana
groupid__c -------text field on case
group_id__c------autogenerated number on account
group_account__c----lookup field on case
Arunkumar RArunkumar R
Can you try the below test class, If it is works then check assertion in the below test class.

@isTest
public class testUpdateLookup {

	static testMethod void testocc(){

	Account acc = new Account(
	name = 'temp validation account',
	Type = 'Prospect',
	Sub_Type__c='Merchant - Body',
	BillingCity='Dubai',
	BillingCountry='United Arab Emirates');
	insert acc;

	Account accCreated = [Select Name,group_id__c From Account Where Id =: acc.Id LIMIT 1];

	// TEST ADDING A NEW case                       
	Case cas = new Case(
	accountId = accCreated.id,
	contactId=con.Id,
	status='New',
	Priority='Medium',
	Type='Issue',
	Origin='Email',
	groupid__c=acc.group_id__c);
	insert cas;
  


	}
}


This was selected as the best answer
Sabah FarhanaSabah Farhana
Spot on!

100%
Can you please explain what was the problem so that i can learn to do this better?
Arunkumar RArunkumar R
Previously You have inserted two cases.
with one case have null value of groupid__c and another case groupid__c have a value.. If both positive and negative get collapsed.

So do positive and negative test cases in different methods.