-4
   public class ExampleClass {
      public static void publishCaseUpdateNotifications(List<Case> cases) {
    List<Case_Updated__e> notifications = new List<Case_Updated__e>();
    for (Case c: cases) {
        notifications.add(new Case_Updated__e(
            Case__c = (String)c.Id,
            CreatedByName__c = UserInfo.getName(),
            CaseNumber__c = c.CaseNumber
        ));
    }


    List<Database.SaveResult> results = EventBus.publish(notifications);

    for (Database.SaveResult result : results) {
        if (!result.isSuccess()) {
            for (Database.Error error : result.getErrors()) {
                System.debug('Error returned: ' +
                    error.getStatusCode() +' - '+
                    error.getMessage());
            }
        }
      }
     }
   }

My Test Class: getting error

 @isTest
  public class ExampleTestClass {

  @isTest
  static void testClassforExample(){

       Case testcase = new Case(Subject ='Test',);

    list<Case_Updated__e> caseUpdate = 
    ExampleClass.publishCaseUpdateNotifications(testcase );
      }  
   }

My Error is Method does not exist or incorrect signature: void publishCaseUpdateNotifications(Case) from the type ExampleTestClass

Please suggest me how to resolve this error.

   trigger Cases on Case (after insert) {
if(Trigger.isAfter) {
    System.debug('I will be replaced shortly!');
}
 }

Do we need test class for above trigger. Please suggest me too

closed as off-topic by Sebastian Kessel, glls, battery.cord, Jayant Das, Vijay Ganji Jun 10 at 6:39

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on problems in code you've written must describe the specific problem and include valid code to reproduce it. For help writing short, self-contained syntactically-valid examples, see: SSCCE.org" – Sebastian Kessel, glls, battery.cord, Jayant Das, Vijay Ganji
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    your method takes a list of cases, you are passing a single case. These are not the same type of object. – TemporaryFix Jun 7 at 14:12
  • In that case, do we need to pass list of cases over there.? – Ajay Jun 7 at 14:17
0

in your Test Class had couple of issues.

  1. your actual classpublishCaseUpdateNotifications method expects list so you should pass Lis<Case> instead of case
  2. your actual class method using saved list of cases not just instance of case.

3.your actual class method publishCaseUpdateNotifications returns nothing(void) but you added list<Case_Updated__e> caseUpdate = in your test class method. so you should take out.

suggestion: i strongly recommend to go through this apex test class for improve your programming skills.

Actual class:

  public class ExampleClass {
  public static void publishCaseUpdateNotifications(List<Case> cases) {
@testVisible private static List<Case_Updated__e> notifications = new List<Case_Updated__e>();
for (Case c: cases) {
    notifications.add(new Case_Updated__e(
        Case__c = (String)c.Id,
        CreatedByName__c = UserInfo.getName(),
        CaseNumber__c = c.CaseNumber
    ));
}


List<Database.SaveResult> results = EventBus.publish(notifications);

for (Database.SaveResult result : results) {
    if (!result.isSuccess()) {
        for (Database.Error error : result.getErrors()) {
            System.debug('Error returned: ' +
                error.getStatusCode() +' - '+
                error.getMessage());
        }
    }
  }
 }

}

    @isTest
       public class ExampleTestClass {
      @testSetup static void setup(){
           Case testcase = new Case(Subject ='Test');
           insert testcase;
    }

    @isTest
    static void testClassforExample(){

      List<Case> lsttestcases = [SELECT Id,CaseNumber FROM Case];
       Test.startTest();
       ExampleClass.publishCaseUpdateNotifications(lsttestcases );
       Test.stopTest();
     List<Case_Updated__c> casUp = [Select Id From Case_Updated__c];
    System.assertEquals(1, ExampleClass.notifications.size());
      }  
   }  
  • Is it required to use two methods @testSetup static void setup() and static void testClassforExample(){? possible to do in one method ? – Ajay Jun 7 at 14:48
  • you can both insert the case and call the actual class method testClassforExample in one method, but i gave best practice solution. – sdandamud1 Jun 7 at 14:51
  • While using you are code, am getting error like this : Static method cannot be referenced from a non static context: void – Ajay Jun 7 at 14:55
  • Edited, Please check – sdandamud1 Jun 7 at 15:02
  • Its working but need to use list<Case_Updated__e> caseUpdate. If i use list<Case_Updated__e>with Error : Illegal assignment from void to List<Case_Updated__e> Test.startTest(); List<Case_Updated__e> casUp= ExampleClass.publishCaseUpdateNotifications(lsttestcases ); Test.stopTest(); System.assertEquals(1, casUp.size()); – Ajay Jun 7 at 15:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.