function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jeff BomarJeff 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
Best Answer chosen by Jeff Bomar
Ricky_ThedaRicky_Theda
Try this code:
 
@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);
        
    }
}

 

All Answers

Ricky_ThedaRicky_Theda
Your postToSlack method have parameter Id, but in the test class you are passing List of SlackOpportunityPublisher.Id try passing Opportunity Id at postToSlack method. Something like this:
List<Id> OppId = new List<Id>();
Oppid.add(Opportunity.Id);

postToSlack(OppId);

 
Jeff BomarJeff Bomar
I tried this and am still having trouble does not exist ls @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 Oppid = new List(); ls.add(pubTest); SlackOpportunityPublisher.postToSlack(Oppid); System.assertEquals(ls, ls); // Can't really test this, just put something that is true } }
Ricky_ThedaRicky_Theda
Hi Jeff,
Try below code.
@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> oppId = new List<Id>();
        
        SlackOpportunityPublisher.postToSlack(oppId);
        System.assertEquals(theList, theList); // Can't really test this, just put something that is true
    }
}

 
Jeff BomarJeff Bomar
now i get Variable does not exist: theList what do you think i should use that would return true @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 oppId = new List(); SlackOpportunityPublisher.postToSlack(oppId); System.assertEquals(theList, theList); // Can't really test this, just put something that is true } }
Ricky_ThedaRicky_Theda
hmm... actually to cover all your test code you don't need to use system assert. Use system assert only when you need to check or comparing something.
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.
 
assertEquals(expected, actual, msg)
Ricky_ThedaRicky_Theda
Try this code:
 
@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);
        
    }
}

 
This was selected as the best answer
Jeff BomarJeff Bomar
i tried this and got this (Thanks again for all your help)
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);
        
    }
}
Jeff BomarJeff Bomar
Thanks for all your help i fixed it by doing this and was able to Deploy with no issues 

@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
    }
}