I don't know much about Salesforce, but I've learned a lot these last few months. I was given the assignment to create an app for the Salesforce App Exchange, so that our API would be available inside of Salesforce.
When I first created the app, I put our API key inside of an Apex class. When I submitted the app for security review, this was rejected. I asked for advice on salesforce.stackexchange.com and I was told that sensitive data such as API keys should go into a Protected Custom Setting. Which I did.
So I ended up with code like this:
Http httpProtocol = new Http();
HttpRequest request = new HttpRequest();
HttpResponse response;
// we need a string that looks like this:
// /[email protected]&pass=test1234
String authpoint = 'authtoken?service=salesforce&user=' + ps.SameDayUsername__c + '&pass=' + ps.SameDayPassword__c;
String endpoint = be + authpoint;
request.setEndPoint(endpoint);
request.setMethod('GET');
request.setHeader('Accept', 'application/json');
request.setHeader('x-api-key', PrivateIncInfo__SameDayConfiguration__c.getOrgDefaults().PrivateIncInfo__xApiKey__c);
try {
// Send the HTTP request and get the response.
// response.getBody() returns the JSON as a String.
if (Test.isRunningTest() && (mock!=null)) {
response = mock.respond(request);
} else {
response = httpProtocol.send(request);
}
This seemed to work, but now when I run the unit test, it is now failing on this line:
request.setHeader('x-api-key', PrivateIncInfo__SameDayConfiguration__c.getOrgDefaults().PrivateIncInfo__xApiKey__c);
I get this error:
System.NullPointerException: Argument 2 cannot be null
So what is the normal way of dealing with this? How do I call this code and have it work in a test?