+ Start a Discussion
Soundar Raj PonpandiSoundar Raj Ponpandi 

How to cover a test class for this simple controller

Dears,

I would like to cover a test class for this simple code , But i am facing a following Error.
 
Error: Compile Error: Static method cannot be referenced from a non static context: String GD_TradeLicenseRunningBanner.fetchUser() at line 39 column 15



public class GD_TradeLicenseRunningBanner {
   
    @AuraEnabled 
    public static String fetchUser(){        User oUser = [select id,Name,TimeZoneSidKey,contactId,Username,Alias,Country,Email,FirstName,LastName,IsActive,IsPortalEnabled, Contact.accountId   FROM User Where id =: userinfo.getuserId() ];        account acc = [select id,name,GD_General_Info__c from account where id=: oUser.contact.accountId Limit 1];
        return acc.GD_General_Info__c;
    }


}


@Test CLass
 
@isTest
Public class GD_TradeLicenseRunningBannerTest{

@testSetup
    static void testSetupMethod(){
    system.debug('Running Into the test class');
        profile pr = [SELECT Id,Name FROM Profile WHERE Name = 'GD PP Sales Rep'];
        User usr = new User(Alias = GD_TestDataFactory.generateUniqueText(), 
                          Email='test@test.com', 
                          EmailEncodingKey='UTF-8', 
                          LastName='Test', 
                          LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US', 
                          ProfileId = pr.Id, 
                          TimeZoneSidKey='America/Los_Angeles', 
                          UserName=GD_TestDataFactory.generateRandomUserName());
        Insert usr;
        
        
        account acc = new account();
        acc.name='test';
        insert acc;
        
        contact con = new contact(firstname='testName', lastName='testName', accountId = acc.id);
        insert con;
        
        Opportunity Opp = new Opportunity(Name = 'Test Opp', CloseDate = Date.today());

        
        System.Test.startTest();
        GD_TradeLicenseRunningBanner trade = new GD_TradeLicenseRunningBanner();
        trade.fetchUser();
        System.Test.stopTest();
    }
}



Thanks in advance.
Soundar.
Best Answer chosen by Soundar Raj Ponpandi
Shamsher SinghShamsher Singh

Replace 

GD_TradeLicenseRunningBanner trade = new GD_TradeLicenseRunningBanner();
trade.fetchUser();

with

GD_TradeLicenseRunningBanner.fetchUser();

Please mark the answer if it helps.

All Answers

Shamsher SinghShamsher Singh

Replace 

GD_TradeLicenseRunningBanner trade = new GD_TradeLicenseRunningBanner();
trade.fetchUser();

with

GD_TradeLicenseRunningBanner.fetchUser();

Please mark the answer if it helps.
This was selected as the best answer
Soundar Raj PonpandiSoundar Raj Ponpandi
Dear Shamsher Singh,

Thanks for your quick response!

Now it's not showing any error ,but still class is not covering.

But still the class is showing 0% code coverage.

Regards,
Soundar.
 
Shamsher SinghShamsher Singh
Please enable log and see if the test records are getting created or not. Check if portal is enabled.
Soundar Raj PonpandiSoundar Raj Ponpandi
Yes the test records are created in the log.

But still code coverage is 0%