I have a class

public with sharing class myCurrentController {
    public PageReference myNewPage() {
        return Network.communitiesLanding();

My test class is

public with sharing class myCurrentControllerTest {
    public static PageReference testMyNewPage() {
        return Network.communitiesLanding();

The code here doesn't cover anything. I have tried a few other things, like adding system.assertEquals(myCurrentControllerTest.testMyNewPage(), myCurrentController.myNewPage());

But I get an error message:

Non static method cannot be referenced from a static context.

I am still very new to Apex tests so any help would be greatly appreciated.

  • THis seems to be the start of the thread of your questions: - Adding this comment in each question: You should provide your Visualforce, or at least your requirement. – The answer here was great. but providing some background may have lead this in a different direction. for context this code in later questions turns into a StandardSetController, and I am not sure why. Dec 10, 2022 at 0:18
  • Thanks @ TheArchitecta. There are three classes that I need to provide code coverage for. One is a CaseController, the next is a LandingPage controller and the last is the Case List controller. Dec 10, 2022 at 0:23
  • OK, so you are effectively the test writer and need to Cover those Classes. RIght. so that is where it is going wrong. As the test writer you must understand how the page works. and write tests that are accurate to the requirement, not just get coverage. Dec 10, 2022 at 0:32

1 Answer 1


Rather than replicating the behaviour of your controller class in your test class, you'll need to make sure that you use the controller and test the outcomes.

To do this, you'll need to instantiate a new instance of your controller, and then call the methods via that instance. This will look something like this:

    public with sharing class myCurrentControllerTest {
        public static void testMyNewPage() {

            myCurrentController controller = new myCurrentController();
            PageReference returnedNewPage = controller.myNewPage();

            //Rest of your test logic/assertions here

You might also want to check out some of this resource around Static and Instance (non-static) methods which gives some details about why you're seeing that error: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_static.htm

  • Thanks @Callum MacErlich that worked. I tried looking at some of those resources but I couldn't figure out where my problem applied. I am still too new at this I guess. Dec 9, 2022 at 1:16
  • 1
    No worries @JonathanCrow, we all have to start somewhere! The key point is that Static methods can be called before an instance of a class has been instantiated, just like a lot of the standard methods like UserInfo.getUserId() or System.Assert. An Instance method however relies on an instance of a class existing. Although Static methods are more common, Instance methods are useful if you want to maintain more information within the transaction, like the current page reference for example. Definitely not an easy concept, and it still gets me sometimes, not that I'm the benchmark of course! Dec 9, 2022 at 1:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .