You need to sign in to do that
Don't have an account?
Jeff Bomar
I am trying to develop a Test class for my SlackOpportunityPublisher i keep getting error
public class SlackOpportunityPublisher {
private static final String slackURL = 'https://hooks.slack.com/services/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';
public class Id {
@InvocableVariable(label='Opportunity Name')
public String opptyName;
@InvocableVariable(label='Type')
public String Type;
@InvocableVariable(label='Stage')
public String stage;
@InvocableVariable(label='Probability')
public String probability;
@InvocableVariable(label='First')
public string first;
@InvocableVariable(label='Last')
public string last;
@InvocableVariable(label='Close')
public string close;
}
@InvocableMethod(label='Post to Slack')
public static void postToSlack(List<Id> opportunityId) {
Id oppId = opportunityId[0]; // If bulk, only post first to avoid overloading Slack channel
Opportunity opportunity = [SELECT Name, Type, StageName, Probability, Owner.FirstName, Owner.LastName, CloseDate from Opportunity WHERE id=:OPPid];
Map<String,Object> msg = new Map<String,Object>();
msg.put('text', 'The following opportunity has changed:\n' + 'Account Name And Product: ' + Opportunity.Name + '\nType: ' + opportunity.Type + '\nNew Stage: ' + opportunity.StageName + '\nSales Person: ' + opportunity.Owner.FirstName + ' ' + opportunity.Owner.LastName + '\nProbability: ' + opportunity.Probability + '%' + '\nForcasted Close Date: ' + opportunity.CloseDate);
msg.put('mrkdwn', true);
String body = JSON.serialize(msg);
System.enqueueJob(new QueueableSlackCall(slackURL, 'POST', body));
}
public class QueueableSlackCall implements System.Queueable, Database.AllowsCallouts {
private final String url;
private final String method;
private final String body;
public QueueableSlackCall(String url, String method, String body) {
this.url = url;
this.method = method;
this.body = body;
}
public void execute(System.QueueableContext ctx) {
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod(method);
req.setBody(body);
Http http = new Http();
if (!Test.isRunningTest()) { // HTTP callout is not allowed in tests apparently...
HttpResponse res = http.send(req);
}
}
}
}
@isTest
private class SlackOpportunityPublisherTest{
static testMethod void testPostToSlack() {
SlackOpportunityPublisher.Id pubTest = new SlackOpportunityPublisher.Id();
pubTest.opptyName = 'Test opportunity';
pubTest.type = 'Test Type';
pubTest.stage = 'Test stage';
pubtest.probability = 'Test probability';
pubTest.first = 'Test First';
pubTest.last = 'Test Last';
pubTest.close = 'Test Close';
List<SlackOpportunityPublisher.Id> theList = new List<SlackOpportunityPublisher.Id>();
theList.add(pubTest);
SlackOpportunityPublisher.postToSlack(theList);
System.assertEquals(theList, theList); // Can't really test this, just put something that is true
}
}
Any help is appriciated this is the problem Method does not exist or incorrect signature: void postToSlack(List<SlackOpportunityPublisher.Id>) from the type SlackOpportunityPublisher
private static final String slackURL = 'https://hooks.slack.com/services/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';
public class Id {
@InvocableVariable(label='Opportunity Name')
public String opptyName;
@InvocableVariable(label='Type')
public String Type;
@InvocableVariable(label='Stage')
public String stage;
@InvocableVariable(label='Probability')
public String probability;
@InvocableVariable(label='First')
public string first;
@InvocableVariable(label='Last')
public string last;
@InvocableVariable(label='Close')
public string close;
}
@InvocableMethod(label='Post to Slack')
public static void postToSlack(List<Id> opportunityId) {
Id oppId = opportunityId[0]; // If bulk, only post first to avoid overloading Slack channel
Opportunity opportunity = [SELECT Name, Type, StageName, Probability, Owner.FirstName, Owner.LastName, CloseDate from Opportunity WHERE id=:OPPid];
Map<String,Object> msg = new Map<String,Object>();
msg.put('text', 'The following opportunity has changed:\n' + 'Account Name And Product: ' + Opportunity.Name + '\nType: ' + opportunity.Type + '\nNew Stage: ' + opportunity.StageName + '\nSales Person: ' + opportunity.Owner.FirstName + ' ' + opportunity.Owner.LastName + '\nProbability: ' + opportunity.Probability + '%' + '\nForcasted Close Date: ' + opportunity.CloseDate);
msg.put('mrkdwn', true);
String body = JSON.serialize(msg);
System.enqueueJob(new QueueableSlackCall(slackURL, 'POST', body));
}
public class QueueableSlackCall implements System.Queueable, Database.AllowsCallouts {
private final String url;
private final String method;
private final String body;
public QueueableSlackCall(String url, String method, String body) {
this.url = url;
this.method = method;
this.body = body;
}
public void execute(System.QueueableContext ctx) {
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod(method);
req.setBody(body);
Http http = new Http();
if (!Test.isRunningTest()) { // HTTP callout is not allowed in tests apparently...
HttpResponse res = http.send(req);
}
}
}
}
@isTest
private class SlackOpportunityPublisherTest{
static testMethod void testPostToSlack() {
SlackOpportunityPublisher.Id pubTest = new SlackOpportunityPublisher.Id();
pubTest.opptyName = 'Test opportunity';
pubTest.type = 'Test Type';
pubTest.stage = 'Test stage';
pubtest.probability = 'Test probability';
pubTest.first = 'Test First';
pubTest.last = 'Test Last';
pubTest.close = 'Test Close';
List<SlackOpportunityPublisher.Id> theList = new List<SlackOpportunityPublisher.Id>();
theList.add(pubTest);
SlackOpportunityPublisher.postToSlack(theList);
System.assertEquals(theList, theList); // Can't really test this, just put something that is true
}
}
Any help is appriciated this is the problem Method does not exist or incorrect signature: void postToSlack(List<SlackOpportunityPublisher.Id>) from the type SlackOpportunityPublisher
All Answers
Try below code.
as explained at https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_system.htm. Try remove that line and re-run.
Class: SlackOpportunityPublisherTest
Method: testPostToSlack
System.ListException: List index out of bounds: 0
Class.SlackOpportunityPublisher.postToSlack: line 22, column 1
Class.SlackOpportunityPublisherTest.testPostToSlack: line 14, column 1
@isTest
private class SlackOpportunityPublisherTest{
static testMethod void testPostToSlack() {
SlackOpportunityPublisher.Id pubTest = new SlackOpportunityPublisher.Id();
pubTest.opptyName = 'Test opportunity';
pubTest.type = 'Test Type';
pubTest.stage = 'Test stage';
pubtest.probability = 'Test probability';
pubTest.first = 'Test First';
pubTest.last = 'Test Last';
pubTest.close = 'Test Close';
List<Id> theList = new List<Id>();
Opportunity testOpp = new Opportunity();
testOpp.Name = 'Test';
testOpp.CloseDate = system.today();
testOpp.Type = 'New Customer';
testOpp.StageName = 'Prospecting';
insert testOpp;
theList.add(testOpp.Id);
SlackOpportunityPublisher.postToSlack(theList);
}
}
@isTest
private class SlackOpportunityPublisherTest{
static testMethod void testPostToSlack() {
// SlackOpportunityPublisher.Id pubTest = new SlackOpportunityPublisher.Id();
Opportunity pubTest = new Opportunity();
pubTest.Name= 'Test opportunity';
// pubTest.opptyName = 'Test opportunity';
pubTest.type = 'Test Type';
pubTest.stageName = 'Test stage';
// pubTest.probability = 'Test probability';
pubTest.probability = 10;
// pubTest.first = 'Test First';
// pubTest.last = 'Test Last';
pubTest.closeDate = System.Today();
// List<SlackOpportunityPublisher.Id> oppId = new List<SlackOpportunityPublisher.Id>();
insert pubTest;
List<Id> oppId = new List<Id>();
oppId.add(pubTest.Id);
SlackOpportunityPublisher.postToSlack(oppId);
System.assertEquals(pubTest, pubTest); // Can't really test this, just put something that is true
}
}