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
peterpptpeterppt 

Trigger Test Coverage : Error message Only top-level class methods can be declared static

I have copied some trigger test code:

 

@isTest
 private class EventTester {
 
    static testMethod void myUnitTest() {
         //COMMENT: First, prepare Dummy Account for relating to Event
         Account acct1 = new Account(name='test account1',Type='Customer');
         insert acct1;
 
        //COMMENT: First, prepare Dummy Event
         Event EventToCreate = new Event(WhatId=acct1.Id,Subject='TESTING',StartDateTime=Today());
            insert EventToCreate;
 
//COMMENT: Inserting the event with a start date value should have caused your trigger to fire which means you can move on to the verification steps
 //Cont'd: however, if you have a trigger based on updating or something, be aware that you would perform that action before verification and AFTER
 //Cont'd: inserting your dummy records inside of the test class.
        
//COMMENT:  Verify correct action using System asserts (these make sure your trigger did what it is supposed to)
     //COMMENT:  first, we locate the Event that has been updated (as opposed to the event that was inserted, because the trigger has changed that origianl event
         Event EvtUpd = [select UsableDueDate__c, StartDateTime from Event where id = :EventToCreate.Id];
     //COMMENT:  then we assert that the field we updated equals the field it was updated from
         System.assert(EvtUpd.UsableDueDate__c == EventToCreate.StartDateTime);
     }
 }

 

But when I try and un it I get the above error.

 

Any help much appreciated !  Thanks.

harsha__charsha__c

Make the Class to public 

 

Top-level type must have public or global visibility..!

 

 

If this makes your solution, then mark it as solution for other's reference..!

 

peterpptpeterppt

Hi Thanks for responding.  Yes I think you are right, it does want to be a public class but simply changing the class to public does not solve my problem if, as I have been doing, I run the code in the normal code window.  But I do solve it if I select Classes from the Setup Entity Type section of the developer console, give the class a name and then paste in exactly the same code.  If I then  select  tests and then select the  test class I've just created run it it runs perfectly.  So what I think I've learned is that you can't create test classes (or all classes ?)  in the normal developer window.  Please correct me if I'm wrong.

 

Here is the code

@isTest
public class PeterTest2 {
     static testMethod void Testx () {
         
        Event objEvent = new Event();
        objEvent.Subject = 'Test666';
        objEvent.OwnerId = '005b0000000M1pvAAC';
        objEvent.StartDateTime = datetime.newInstance(2012, 10, 1, 12, 30, 0);
        objEvent.EndDateTime = datetime.newInstance(2012, 10, 1, 13, 30, 0);
        //objEvent.createdbyid = '005b0000000M1pvAAC';
        objEvent.Public_Name__c = 'Hennock';

        try {
    
        insert objEvent;
        } catch (DmlException e) {
        // Process exception here     
        } 
         
}
 

     }

 

 

Now all I've got to do is sort out the coverage... ! 

Thanks again.

harsha__charsha__c

Hi peter

 

Notable point..!

 

Thanks for sharing