I am trying to create a test class for below class. I have to create an Sobject object and call the function by passing sibject in parameter. How can I write a complete test class for below class:
I tried SObject so=new Sobject();
// But this seem to be wrong way of initializing it. Any suuggestion.
global with sharing class populateFieldService
{
global static void populateByParameter(Map<String,String> fMap, SObject so)
{
Map<String,Schema.sObjectField> fieldMap=so.getSObjectType().getDescribe().fields.getMap();
for(String key: fMap.KeySet())
{
if(key == 'RecordTypeName')
{
try
{
so.put('RecordTypeId',so.getSObjectType().getDescribe().getRecordTypeInfosByName().get(fMap.get(key)).getRecordTypeId());
}
catch(Exception e)
{
continue;
}
}
else if(key != 'Id' && key !='RecordType' && fieldMap.get(key.toLowerCase()) != NULL && fieldMap.get(key.toLowerCase()).getDescribe().isUpdateable())
{
try
{
String fieldType=String.ValueOf(fieldMap.get(key.toLowerCase()).getDescribe().getType()).toLowerCase();
if(fieldType == 'currency' || fieldType == 'double' || fieldType == 'percent' || fieldType == 'decimal')
{
so.put(key, double.valueOf(fMap.get(key)));
}
else if(fieldType == 'boolean')
{
so.put(key, Boolean.valueOf(fMap.get(key)));
}
else if(fieldType == 'date')
{
Date d=date.parse(fMap.get(key));
so.put(key,d );
}
else If(fieldType == 'datetime')
{
DateTime dt=DateTime.parse(fMap.get(key));
so.put(key,dt);
}
else
{
so.put(key,fMap.get(key));
}
}
catch(Exception e)
{
continue;
}
}
}
}
}
I tried the below piece of code to cover it but some of the lines still shows not covered.. Any idea what fields I should fill in for this?
@isTest
private class populateFieldServiceTest{
@isTest
static void rpopulateByParameterTest(){
Map<String,String> fMap = new Map<String,String>();
Account record = new Account();
Map<String, String> fieldToValue = new Map<String, String> { 'Name' => 'ACME' };
PopulateFieldService.populateByParameter(fieldToValue, record);
}
}