In Review
Platform / Development (Apex, LWC & VF)
"The external callout is not allowed in Apex test" error occurs when running the Apex test class for testing NamedCredentials.createCredential()
Summary
Create the external credential and following Apex class and test class for refreshing credentials.
When running the apex test class, "ConnectApi.ConnectApiException: java.sql.SQLException: The external callout is not allowed in Apex test." error occurs.
At this time, there is not a way to test ConnectApi.NamedCredentials.createCredential(requestBody, action).
* Apex Class
public class refreshExternalCredential {
public static void runRefreshExternalCredential() {
ConnectApi.ExternalCredential myExternalCredential01 = ConnectApi.NamedCredentials.getExternalCredential('<ExternalCredentialName>');
ConnectApi.CredentialInput myCredentialInput01 = new ConnectApi.CredentialInput();
myCredentialInput01.externalCredential = myExternalCredential01.developerName;
myCredentialInput01.principalName = myExternalCredential01.principals[0].principalName;
myCredentialInput01.principalType = ConnectApi.CredentialPrincipalType.PerUserPrincipal;
myCredentialInput01.authenticationProtocol = ConnectApi.CredentialAuthenticationProtocol.OAuth;
myCredentialInput01.credentials = new Map<String, ConnectApi.CredentialValueInput>();
ConnectApi.Credential refreshedCredential = ConnectApi.NamedCredentials.createCredential(myCredentialInput01, ConnectApi.CreateCredentialAction.Refresh);
}
}
* Apex Test Class
@isTest
public class refreshExternalCredentialTest {
@IsTest(SeeAllData=true)
static void runRefreshExternalCredentialTest() {
Test.startTest();
refreshExternalCredential.runRefreshExternalCredential();
Test.stopTest();
}
}
When running the apex test class, "ConnectApi.ConnectApiException: java.sql.SQLException: The external callout is not allowed in Apex test." error occurs.
At this time, there is not a way to test ConnectApi.NamedCredentials.createCredential(requestBody, action).
* Apex Class
public class refreshExternalCredential {
public static void runRefreshExternalCredential() {
ConnectApi.ExternalCredential myExternalCredential01 = ConnectApi.NamedCredentials.getExternalCredential('<ExternalCredentialName>');
ConnectApi.CredentialInput myCredentialInput01 = new ConnectApi.CredentialInput();
myCredentialInput01.externalCredential = myExternalCredential01.developerName;
myCredentialInput01.principalName = myExternalCredential01.principals[0].principalName;
myCredentialInput01.principalType = ConnectApi.CredentialPrincipalType.PerUserPrincipal;
myCredentialInput01.authenticationProtocol = ConnectApi.CredentialAuthenticationProtocol.OAuth;
myCredentialInput01.credentials = new Map<String, ConnectApi.CredentialValueInput>();
ConnectApi.Credential refreshedCredential = ConnectApi.NamedCredentials.createCredential(myCredentialInput01, ConnectApi.CreateCredentialAction.Refresh);
}
}
* Apex Test Class
@isTest
public class refreshExternalCredentialTest {
@IsTest(SeeAllData=true)
static void runRefreshExternalCredentialTest() {
Test.startTest();
refreshExternalCredential.runRefreshExternalCredential();
Test.stopTest();
}
}
Workaround
We haven't loaded the full details for this issue yet. Check the official page.
History
First tracked here as In Review with 2 reports
Nov 30, 2026