[SalesForce] getting Unexpected token error for test class

Hi I am trying to pass this id through page reference to my controller for the test coerage. but this line below gives me an error saying

unexpected token: '.'

Test Class

 @IsTest

public class TestNotice_Controller {

    private static ApexPages.StandardController cont;
    Opportunity opp = new opportunity(name = 'Cart');
    string id = opp.id; 
    PageReference pg = Page.Notice;
    pg.getParameters().put('id', id);


    private static testMethod void testNew() {

        Test.startTest();
        {   

            // Initialize the controller
            Notice_Controller ln = new Notice_Controller(cont);

        }
        Test.stopTest();
    }

}

I don't understand where is the probelem. According to developer guide I am not doing anything wrong. But I don't understand why I am getting this error.

Best Answer

Your class should look like this. You have a lot of code outside of a function, when it should be inside.

@IsTest
public class TestNotice_Controller {

    private static testMethod void testNew() {
      private ApexPages.StandardController cont;
      Opportunity opp = new opportunity(name = 'Cart');

      string id = opp.id; 
      PageReference pg = Page.Notice;
      pg.getParameters().put('id', id);

      Test.startTest();
          // Initialize the controller
          Notice_Controller ln = new Notice_Controller(cont);                     

      Test.stopTest();
    }

}
Related Topic