You need to sign in to do that
Don't have an account?

Data Integration Specialist superbadge challenge #4
I am stuck with Data Integration Specialist superbadge challenge #4. While trying to run the test i am getting an error 'Methods defined as TestMethod do not support Web service callouts'. Below are my code:
@IsTest
public class ProjectCalloutServiceMock implements HttpCallOutMock{
//Implement http mock callout here
public HttpResponse respond(HttpRequest req)
{
HttpResponse res = new HttpResponse();
res.setHeader('Content-Type', 'application/json');
res.setStatus('OK');
res.setStatusCode(201);
return res;
}
}
@Istest
public class ProjectCalloutServiceMockFailure implements HttpCallOutMock {
//Implement http mock callout failure here
public HttpResponse respond(HttpRequest req)
{
HttpResponse res = new HttpResponse();
res.setHeader('Content-Type', 'application/json');
res.setStatus('Error');
res.setStatusCode(500);
return res;
}
}
@isTest
private class ProjectCalloutServiceTest {
//Implement mock callout tests here
@testsetup
public static void setupdata()
{
List<Opportunity> oppsToInsert = new List<Opportunity>();
Account acct = new Account();
acct.Name='test Account';
insert acct;
Opportunity opp1 = new Opportunity();
opp1.Name = 'Opp1';
opp1.Type='New Customer';
opp1.AccountId = acct.id;
opp1.amount=500;
opp1.CloseDate = date.today();
opp1.StageName = 'Prospecting';
oppsToInsert.add(opp1);
Opportunity opp2 = new Opportunity();
opp2.Name = 'Opp2';
opp2.Type='New Customer';
opp2.AccountId = acct.id;
opp2.amount=2500;
opp2.CloseDate = date.today().addDays(-3);
opp2.StageName = 'Prospecting';
oppsToInsert.add(opp2);
insert oppsToInsert;
ServiceTokens__c st = new ServiceTokens__c();
st.Name = 'ProjectServiceToken';
st.Token__c = 'thisistesttoken';
insert st;
}
@istest
public static void testSuccessMessage()
{
Opportunity oppList = [Select Id from Opportunity where Name='Opp1' limit 1];
List<Id> oppIds = new List<Id>();
oppIds.add(oppList.Id);
Test.setMock(HttpCalloutMock.class, new ProjectCalloutServiceMock());
test.startTest();
ProjectCalloutService.postOpportunityToPMS(oppIds);
test.stopTest();
oppList = [Select StageName from Opportunity where Name='Opp1' limit 1];
system.assertEquals('Submitted Project',oppList.StageName);
}
@istest
public static void testFailureMessage()
{
Opportunity oppList = [Select Id from Opportunity where Name='Opp2' limit 1];
List<Id> oppIds = new List<Id>();
oppIds.add(oppList.Id);
Test.setMock(HttpCalloutMock.class, new ProjectCalloutServiceMockFailure());
test.startTest();
ProjectCalloutService.postOpportunityToPMS(oppIds);
test.stopTest();
oppList = [Select StageName from Opportunity where Name='Opp2' limit 1];
system.assertEquals('Resubmit Project',oppList.StageName);
}
}
@IsTest
public class ProjectCalloutServiceMock implements HttpCallOutMock{
//Implement http mock callout here
public HttpResponse respond(HttpRequest req)
{
HttpResponse res = new HttpResponse();
res.setHeader('Content-Type', 'application/json');
res.setStatus('OK');
res.setStatusCode(201);
return res;
}
}
@Istest
public class ProjectCalloutServiceMockFailure implements HttpCallOutMock {
//Implement http mock callout failure here
public HttpResponse respond(HttpRequest req)
{
HttpResponse res = new HttpResponse();
res.setHeader('Content-Type', 'application/json');
res.setStatus('Error');
res.setStatusCode(500);
return res;
}
}
@isTest
private class ProjectCalloutServiceTest {
//Implement mock callout tests here
@testsetup
public static void setupdata()
{
List<Opportunity> oppsToInsert = new List<Opportunity>();
Account acct = new Account();
acct.Name='test Account';
insert acct;
Opportunity opp1 = new Opportunity();
opp1.Name = 'Opp1';
opp1.Type='New Customer';
opp1.AccountId = acct.id;
opp1.amount=500;
opp1.CloseDate = date.today();
opp1.StageName = 'Prospecting';
oppsToInsert.add(opp1);
Opportunity opp2 = new Opportunity();
opp2.Name = 'Opp2';
opp2.Type='New Customer';
opp2.AccountId = acct.id;
opp2.amount=2500;
opp2.CloseDate = date.today().addDays(-3);
opp2.StageName = 'Prospecting';
oppsToInsert.add(opp2);
insert oppsToInsert;
ServiceTokens__c st = new ServiceTokens__c();
st.Name = 'ProjectServiceToken';
st.Token__c = 'thisistesttoken';
insert st;
}
@istest
public static void testSuccessMessage()
{
Opportunity oppList = [Select Id from Opportunity where Name='Opp1' limit 1];
List<Id> oppIds = new List<Id>();
oppIds.add(oppList.Id);
Test.setMock(HttpCalloutMock.class, new ProjectCalloutServiceMock());
test.startTest();
ProjectCalloutService.postOpportunityToPMS(oppIds);
test.stopTest();
oppList = [Select StageName from Opportunity where Name='Opp1' limit 1];
system.assertEquals('Submitted Project',oppList.StageName);
}
@istest
public static void testFailureMessage()
{
Opportunity oppList = [Select Id from Opportunity where Name='Opp2' limit 1];
List<Id> oppIds = new List<Id>();
oppIds.add(oppList.Id);
Test.setMock(HttpCalloutMock.class, new ProjectCalloutServiceMockFailure());
test.startTest();
ProjectCalloutService.postOpportunityToPMS(oppIds);
test.stopTest();
oppList = [Select StageName from Opportunity where Name='Opp2' limit 1];
system.assertEquals('Resubmit Project',oppList.StageName);
}
}
We have a separate Trailhead team who can help you with these issues. So, can you please use the below link to reach out to them so that one of the agent will get in touch with you.
Support:https://trailhead.salesforce.com/help
Thank you!
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.
Thanks,
Vinay Kumar
When I removed the setup data method and did the setup in test methods it worked.
@isTest
private class CalloutServiceTest {
/*@testSetup
static void setup(){}
*/
@isTest
static void testMethod(){
Account acc = new Account(Name='Test Account');
insert acc;
Test.startTest();
Test.setMock(HttpCalloutMock.class, new CalloutServiceMock());
Test.stopTest();
}
}
ThankYou
Thank you