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
DoondiDoondi 

How to write test class for if (list.size > 0 )?

Hello Dev's
Please help me to write test class for If(list.size >0 ) 
here is my trigger code
 
List <Project__c> ProjectList = [select id,Name,OwnerId from Project__c where Name =: Name];
// Region__c is custom settings to avoid hard coding ID's of particular profile to which record should be assigned if it meets Region__c condigion on deal object)
            Region__c user = Region__c.getInstance(Userinfo.getUserId());
            if (ProjectList.size() == 0)
            {
                 if(deal.Manager__c == Null && deal.Property__r.Region__c == 'Asia')
                    {
                    	deal.Manager__c = user.Asia__c;
                	}		
                Projects.add(new Project__c
                                  (Name = 'test Name', 
                                   OwnerId = deal.Manager__c
                                   ));
                insert Project;
    }
            else if ((ProjectList.size() > 0))
            {
                 if(deal.Manager__c == Null && deal.Property__r.Region__c == 'Asia')
                {
                    deal.Manager__c = user.Asia__c;
                }
                String Name : deal.Name 
                Project__c NewProjectList = [select id,Name,OwnerId from Project__c where Name =: Name];
                NewProjectList.OwnerId = deal.Manager__c; 
                update NewProjectList;
            }

What I tried till now is I have created data for all the objects and I am getting coverage for that, but I am not able to pass this If(list.size > ) ) condition

it's very urgent. 
Raj VakatiRaj Vakati
WHat is the value you are passing it the the name .. lets suppose you are passing the Name as "SFDC" you need to insert the data like this
 
List<Project__c> pList = new List<Project__c>();
			
			Project__c p = new Project__c() ;
			p.Name ='SFDC' ;
			pList.add(p); 
			
			Project__c p1 = new Project__c() ;
			p1.Name ='SFDC' ;
			pList.add(p1); 
			
			insert pList ;


To cover the if block add like below 
 
List<Project__c> pList = new List<Project__c>();
			
			Project__c p = new Project__c() ;
			p.Name ='SFTestDC' ;
			pList.add(p); 
			
			Project__c p1 = new Project__c() ;
			p1.Name ='Test' ;
			pList.add(p1); 
			
			insert pList ;

 
Raj VakatiRaj Vakati
sorry to miss you reply ... can you give me your test class  ?? i will be able to rewrite it