I am working on a testclass but get the error that my method cant be referenced from a non static context. I have a class where I insert a list of contacts followed by a @future method. I get the error on : obj.saveContacts(conts);
Class
public without sharing class AddController {
@AuraEnabled
public static void saveContacts(List<Contact> listContact){
Insert listContact;
Map<Id, Contact> contactMap = new Map<Id, Contact>(listContact);
createUser(contactMap.keySet());
system.debug('listContact '+ listContact);
User currentUser = [SELECT Id, Contact.AccountId FROM User WHERE Id =: UserInfo.getUserId() LIMIT 1];
for(Contact con : listContact){
con.AccountId = currentUser.Contact.AccountId;
}
update listContact;
system.debug('currentUser '+ currentUser);
List<Relatie__c> rel= new List<Relatie__c>();
UserRole ur = [SELECT Id FROM UserRole Where name = 'Partner user'];
for(Contact con : listContact){
Relatie__c rc = new Relatie__c(Account__c = currentUser.contact.AccountId, Contactpersoon__c = con.id ) ;
system.debug('rc '+ rc);
rel.add(rc);
}
insert rel;
system.debug('rel '+ rel);
}
@future public static void createUser(Set<Id> contactIds) {
Profile profileId = [SELECT Id FROM Profile WHERE Name = 'Community' LIMIT 1];
UserRole ur = [SELECT Id FROM UserRole Where name = 'Partner user'];
List<User> uList= new List<User>();
Contact[] contactList = [SELECT id, firstname, lastname, email FROM Contact WHERE Id = :contactIds];
for(Contact con: contactList){
string alias = con.firstName.substring(0,1) + con.lastName.substring(0,1);
user u = New user(userName = con.firstName+'.'+con.lastName+'@test.nl' ,
firstName = con.firstName,
lastName = con.lastName,
alias = alias,
email = con.email,
communityNickName = alias,
timeZoneSidKey = 'Europe/Amsterdam',
LocaleSidKey = 'nl_NL',
EmailEncodingKey = 'ISO-8859-1',
LanguageLocaleKey = 'nl_NL',
ContactID = con.id,
ProfileId = profileId.id//,
//UserRoleId = ur.id
);
uList.add(u); }
insert uList;
system.debug('uList '+uList);
}
}
Testclass
@isTest
private class AddControllerTest{
@testSetup
static void setupTestData(){
Profile pf= [Select Id from profile where Name='System Administrator'];
String orgId=UserInfo.getOrganizationId();
String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000));
String uniqueName=orgId+dateString+RandomId;
User u=new User(firstname = 'ABC',
lastName = 'XYZ',
email = uniqueName + '@test' + orgId + '.org',
Username = uniqueName + '@test' + orgId + '.org',
EmailEncodingKey = 'ISO-8859-1',
Alias = uniqueName.substring(18, 23),
TimeZoneSidKey = 'Europe/Amsterdam',
LocaleSidKey = 'nl_NL',
LanguageLocaleKey = 'nl_NL',
ProfileId = pf.Id
);
insert u;
Account acc = new Account(Name='TestAccount');
insert acc;
list<Contact> conts=new list<Contact>();
for(Integer i=0;i<50;i++){
contact con=new contact();
con.LastName = 'Contact'+i;
con.AccountId = acc.id;
conts.add(con);
}
insert conts;
System.Test.startTest();
Addcontroller obj = new Addcontroller();
obj.saveContacts(conts);
System.AssertNotEquals(Null,)
System.test.stopTest();
}
}