You need to sign in to do that
Don't have an account?
sfg1
Need to increase Code coverage
I am able to cover only 39%. Not able cover code coverage for bold lines.Can you please guide me.
My class:
global class autoCompleteForAccount
{
@RemoteAction
global static SObject[] findSObjects(string qry, string accId)
{
String loginUserProfile = UserInfo.getProfileId();
System.debug('Community Plus' + System.Label.Customer_Community_Plus_Profile);
String filter = ' like \'%' + String.escapeSingleQuotes(qry) + '%\'';
String soql = '';
String userId = UserInfo.getUserId();
List<User> conId = Database.query('SELECT ContactId from User where Id=\'' + userId + '\'');
if(loginUserProfile == System.Label.Customer_Community_Plus_Profile)
{
String commaSepratedList = '';
List<Shared_Contact__c> sharedCon = [SELECT Account__c FROM Shared_Contact__c where contact__c =:conId[0].ContactId];
if(sharedCon.size()>0)
{
for(Shared_Contact__c sc: sharedCon)
{
//listIds.add(sc.Account__c);
if(sc.Account__c != NULL)
commaSepratedList += '\''+ sc.Account__c + '\',' ;
}
commaSepratedList = commaSepratedList.subString(0,commaSepratedList.length()-1);
}
String commaSepratedList1 = '';
List<Shared_Contact_Del__c> sharedCon1 = [SELECT Account__c FROM Shared_Contact_Del__c where contact__c =:conId[0].ContactId];
if(sharedCon1.size()>0)
{
for(Shared_Contact_Del__c sc: sharedCon1)
{
//listIds.add(sc.Account__c);
if(sc.Account__c != NULL)
commaSepratedList1 += '\''+ sc.Account__c + '\',' ;
}
commaSepratedList1 = commaSepratedList1.subString(0,commaSepratedList1.length()-1);
commaSepratedList = commaSepratedList+','+commaSepratedList1;
}
soql = ' SELECT Name FROM Account WHERE Name ' + filter + ' and Id IN (' + commaSepratedList + ') ';
}
else
{
soql = ' SELECT Name FROM Account WHERE Name ' + filter;
}
soql += ' ORDER BY Name LIMIT 10';
system.debug('$$$Qry: ' + soql);
List<sObject> L = new List<sObject>();
try
{
L = Database.query(soql);
}
catch (QueryException e)
{
system.debug('Query Exception:'+e.getMessage());
return null;
}
return L;
}
}
My test class:
@istest
global class Test_autoCompleteForAccount
{
global static testMethod void testClass()
{
account acc = new account(name='TestAccount',Company_Name__c='TestAccount');
insert acc;
Test.startTest();
sObject[] s1 = autoCompleteForAccount.findSObjects( 'Test', '');
Test.stopTest();
}
}
My class:
global class autoCompleteForAccount
{
@RemoteAction
global static SObject[] findSObjects(string qry, string accId)
{
String loginUserProfile = UserInfo.getProfileId();
System.debug('Community Plus' + System.Label.Customer_Community_Plus_Profile);
String filter = ' like \'%' + String.escapeSingleQuotes(qry) + '%\'';
String soql = '';
String userId = UserInfo.getUserId();
List<User> conId = Database.query('SELECT ContactId from User where Id=\'' + userId + '\'');
if(loginUserProfile == System.Label.Customer_Community_Plus_Profile)
{
String commaSepratedList = '';
List<Shared_Contact__c> sharedCon = [SELECT Account__c FROM Shared_Contact__c where contact__c =:conId[0].ContactId];
if(sharedCon.size()>0)
{
for(Shared_Contact__c sc: sharedCon)
{
//listIds.add(sc.Account__c);
if(sc.Account__c != NULL)
commaSepratedList += '\''+ sc.Account__c + '\',' ;
}
commaSepratedList = commaSepratedList.subString(0,commaSepratedList.length()-1);
}
String commaSepratedList1 = '';
List<Shared_Contact_Del__c> sharedCon1 = [SELECT Account__c FROM Shared_Contact_Del__c where contact__c =:conId[0].ContactId];
if(sharedCon1.size()>0)
{
for(Shared_Contact_Del__c sc: sharedCon1)
{
//listIds.add(sc.Account__c);
if(sc.Account__c != NULL)
commaSepratedList1 += '\''+ sc.Account__c + '\',' ;
}
commaSepratedList1 = commaSepratedList1.subString(0,commaSepratedList1.length()-1);
commaSepratedList = commaSepratedList+','+commaSepratedList1;
}
soql = ' SELECT Name FROM Account WHERE Name ' + filter + ' and Id IN (' + commaSepratedList + ') ';
}
else
{
soql = ' SELECT Name FROM Account WHERE Name ' + filter;
}
soql += ' ORDER BY Name LIMIT 10';
system.debug('$$$Qry: ' + soql);
List<sObject> L = new List<sObject>();
try
{
L = Database.query(soql);
}
catch (QueryException e)
{
system.debug('Query Exception:'+e.getMessage());
return null;
}
return L;
}
}
My test class:
@istest
global class Test_autoCompleteForAccount
{
global static testMethod void testClass()
{
account acc = new account(name='TestAccount',Company_Name__c='TestAccount');
insert acc;
Test.startTest();
sObject[] s1 = autoCompleteForAccount.findSObjects( 'Test', '');
Test.stopTest();
}
}
Your test code is running as system context. You have to modify your test method so that it can be run as specific user context. To do that use System.runAs() method. For more details see https://developer.salesforce.com/docs/atlas.en-us.200.0.apexcode.meta/apexcode/apex_testing_tools_runas.htm