How to resolve error non static method cannot be referenced from a static context?
Here is the Test class:
@isTest private with sharing class Genereratortest { @TestSetup static void createPayloadtest() { List<_Site__c> studySites = new List<_Study_Site__c>{ TestDataFactory.createStudySite('test','Data'), }; Test.startTest(); SSUDataJSONGenerator.createPayload(studySites,'INSERT'); Test.stopTest(); }
} }
When I run this test class I am getting below error
The nonstatic method cannot be referenced from a static context: String SSUDataJSONGenerator.createPayload(List
Not sure what’s going wrong with the test class, please suggest a possible solution.
There is one simple way of solving the non-static variable cannot be referenced from a static context error. Address the non-static variable with the object name. In a simple way, we have to create an object of the class to refer to a non-static variable from a static context.
As written, you've made it so you have to construct an instance of your class:
Test.startTest();
SSUDataJSONGenerator generator = new SSUDataJSONGenerator();
generator.createPayload(studySites,'INSERT');
Test.stopTest();
If you didn't mean to add this complexity, change your method to static:
public static String createPayload(List sobjrecords, String operation) {
You also need to use @isTest to denote a unit test method. @testSetup is only for creating test data (if necessary).
@isTest
class SSUDataJSONGeneratorTest {
@isTest
static void createPayloadtest() {
List studySites = new List{
TestDataFactory.createStudySite('test','Data'),
};
Test.startTest();
SSUDataJSONGenerator.createPayload(studySites,'INSERT');
Test.stopTest();
}
}