Hope this helps you..
In Test Class, You need to insert Account first and then Lead
I guess, your trigger would be something like this:
trigger StackExchangeQ2 on Lead (before insert) {
List<String> companies = new List<String>();
For (lead l:trigger.new){
if(l.country=='India'){
companies.add(l.company);
system.debug(companies);
}
}
List<String> soqlLikeStr = new List<String>();
for(String str:companies) {
soqlLikeStr.add('%'+str+'%');
}
System.debug('soql like str = ' + soqlLikeStr);
List<Account> leadAccountIds=[Select Id, OwnerId, Name FROM Account
WHERE Name LIKE :soqlLikeStr];
System.debug('leadAccountIds = ' + leadAccountIds);
Map<String, Id> acctNameId=new Map<String,Id>();
Map<String, Id> acctNameOwner=new Map<String,Id>();
if(!leadAccountIds.isEmpty() && leadAccountIds.size()==1){
For(Account a : leadAccountIds){
acctNameId.put(a.name,a.Id);
acctNameOwner.put(a.Name,a.OwnerId);
For(Lead l2 : trigger.new){
l2.ownerId=a.OwnerId;
}
}
}
}
Test Class for this -(gives 100% code coverage) @istest public class StackExchangeQ2Test {
@istest
public class StackExchangeQ2Test {
@isTest static void TestDeleteAccountWithOneOpportunity() {
//insert Account
Account acc = new Account(
Name = 'Test Park controls Account'
);
insert acc;
//insert Lead
Lead l = new Lead(
FirstName = 'Jake',
LastName = 'Doe',
Phone = '9874563210',
Country = 'India',
Company = 'Park controls'
);
Insert l;
}
}