0

I have this apex class:

public class APILogManager {
public static Boolean logByEmail(HttpRequest req, String responseHeader, Integer code, String endpoint, String responseContent, String entity){
    Boolean result = true;
    Developer_Monitoring__c monitoring = Developer_Monitoring__c.getOrgDefaults();
    
    if(monitoring != null){
        if((monitoring.Send_API_Errors__c != null)&&(monitoring.Send_API_Errors__c)){
            try{
                result = false; 
                List<String> emailTo = new String[]{monitoring.Developer_Email__c};
                
                if(emailTo.size() > 0){
                    String subject = 'Error_'+ code + '_' + Helper.formatDatetimeForRender(Datetime.now());
                    
                    String body = Datetime.now() + ':\t Response code: ' + code + '\n\n';
                    body += 'REQUEST:\n Endpoint:' + req.getEndpoint()+ ', Method:' + req.getMethod()+ ', Accept:' + req.getHeader('Accept') +', Content-Type:' + req.getHeader('Content-Type') +', ML_Admin_Id:' + req.getHeader('ML_Admin_Id') +', SFDC_App:' + req.getHeader('SFDC_App') + '\n\n';
                    
                    body += 'RESPONSE HEADERS: ' + responseHeader + '\n'; 
                    
                    body += 'RESPONSE CONTENT:\n' + responseContent + '\n';             
                    
                    //Utils.sendEmail(emailTo, subject, body);
                    
                    result = true;
                }
                
            }catch(Exception e){
                System.Debug(LoggingLevel.ERROR, e);
            }
        }
    }
    return result;
}

}

So, the vast majority of the code relies on whether monitoring returns null or not. Obviously, being in a test environment it will return null. Now, I know I can instantiate custom settings, but I can't with this one, because the test execution throws a DUPLICATE_VALUE saying there is already a developer monitoring with the same SetupOwnerId. So, I assume I can't assign a hardcoded value to SetupOwnerId, so how can I get past this? Is there a way to get that coverage?

This is what I tried to do from my test class:

static testmethod void test2(){
    HttpRequest req = new HttpRequest();

    Developer_Monitoring__c dm = new Developer_Monitoring__c(
        Send_API_Errors__c = true,
        Developer_Email__c = 'test@test.com'
    );
    insert dm;

    System.assert(APILogManager.logByEmail(req, null, 200, '', '', ''));
}
0

You do need to specify a SetupOwnerId. I'd recommend the more usual method of getting the (optional) default value and using upsert.

// testMethod is deprecated
@isTest static void test2(){
    HttpRequest req = new HttpRequest();

    // SetupOwnerId will be set for you here.
    Developer_Monitoring__c dm = Developer_Monitoring__c.getOrgDefaults();
    dm.Send_API_Errors__c = true;
    dm.Developer_Email__c = 'test@test.com';
    upsert dm;

    System.assert(APILogManager.logByEmail(req, null, 200, '', '', ''));
}

This makes sure that you get the value that may have been previously set up elsewhere in your unit tests (e.g. in a TestSetup method, if applicable).

Also, make sure your unit test class set to a more modern API version. Before 24.0 (23.0 and less), database isolation did not exist, which may be another reason why you got this error. It may be time to update your API version--your code works perfectly fine in my developer org when set to a modern API version.

As a side note, you can simplify your code with the Safe Navigation Operator:

if(monitoring?.Send_API_Errors__c == true){

Note that the documentation also says:

In API version 42.0 and later, if a hierarchy custom setting is inserted in a testSetup method, inserting a hierarchy custom setting record with the same SetupOwnerId in a test method throws a DUPLICATE_VALUE exception.

Using upsert instead of insert avoids this problem. Note that records modified in a unit test will be rolled back, so you don't need to worry about modified settings affecting production data.

1
  • I'm actually modifying a already made unit test, I checked and it actually is set to an old API version (20.0 to be exact), so it is an old unit test. I'll change that, thank you. Apr 26 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.