0

Can someone please help me how can I get the code coverage for below line:

code coverage

Test class code:

@istest
public class Testownerassisgnbasedonaccount {

      @isTest static void TestDeleteAccountWithOneOpportunity() {
        // Test data setup
        // Create an account with an opportunity, and then try to delete it
                Lead le=new Lead(
                        FirstName='Hqwewqinwedu',
                        LastName='H',
                        Company='Park controls',   
                        phone='2568552',
                        country='India'    
                );
        Insert le;
               List<string> cmpny=new List<String>();
               Map<string,Id> mp1=new Map<string,Id>();   
               Account acct = new Account(Name='Test Account',Billingcountry='India',Industry='Energy',Activity__c='Distributor');
                insert acct;
                cmpny.add('Test Account');
          system.debug(cmpny);
               List<Account> ACC = [select Phone,Name,Id,OwnerId from Account where Name Like:cmpny LIMIT 1];
          System.debug(ACC);
            for(Account ac:ACC){
              mp1.put(ac.name,ac.ownerId);   
          }
       Lead lne=new lead();
               lne.FirstName='Pweawqeqwerk';
               lne.LastName='Cwqeontrweqweols';
               lne.company=acct.Name;
               lne.OwnerId=acct.ownerId;  
        Insert lne;
     }   

}

Thanks in advance!...

8
  • Can you please share the code where you define leadAccoubtIds?
    – bpilling
    Commented Jul 16, 2018 at 4:50
  • Hi bpilling Thanks for your reply List<Account> leadAccountIds=[Select Id, OwnerId, Name FROM Account WHERE Name LIKE :soqlLikeStr]; The above actually I'm with leadAccountIds
    – VINOD
    Commented Jul 16, 2018 at 4:59
  • 1
    Hi Vinod - Looks like you have a query at line #7 of your screenshot. Have you tried to create test data so that at least one record will be returned for the query at line #7
    – Ansuman
    Commented Jul 16, 2018 at 5:45
  • HI Ansuman, Yes, I have tried with that.
    – VINOD
    Commented Jul 16, 2018 at 6:16
  • @VINOD, in your screenshot you have applied condition : leadAccountIds.size()==1. Have you satisfying this condition in your test class? means, Is your test-data returning only one record which satisfy your query?
    – sfdc_sk
    Commented Jul 16, 2018 at 6:41

1 Answer 1

0

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 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;
  }   
}
2
  • 2
    It may give coverage, but this test method is incomplete because it doesn't have any assertions. Assertions are the things that make unit tests truly useful because they are used to verify that your code produces the results you expect it to. Without assertions, the only thing that's being tested is that your code doesn't run into any governor limits or unhandled exceptions. A high-quality answer will also take the time to explain why your solution works. Both of these things are a tad on the hard side to do though, given OP only provided a small portion of their code.
    – Derek F
    Commented Jul 16, 2018 at 13:00
  • Hi @DerekF, thanks for your guidance but my main aim behind this answer was to tell that Account should be inserted first and then the lead, rest of the logic can be added/modified by the asker himself. Commented Jul 17, 2018 at 5:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .