You need to sign in to do that
Don't have an account?
How do I assign a user within my test class??
I need to have a user assigned in order to make this test class actually function. My actual class and trigger work as designed, but the test class fails because it requires a user to be present in order to work. Here is the code I have. I'm very new to apex and coding all together so I'm kinda stuck.
@isTest
public with sharing class UpdateNCMOwnerHandlerTest
{
public static testMethod void testUpdateNCMOwnerHandler()
{
List<Non_Conforming_Material__c> nonConformingMaterials = new List<Non_Conforming_Material__c>();
Non_Conforming_Material__c nonConformingMaterial = new Non_Conforming_Material__c(NCM_Stage__c = 'In Process'); //#1
nonConformingMaterials.add(nonConformingMaterial);
nonConformingMaterial = new Non_Conforming_Material__c(NCM_Stage__c = 'Resolution'); //#2
nonConformingMaterials.add(nonConformingMaterial);
insert nonConformingMaterials;
}
}
@isTest
public with sharing class UpdateNCMOwnerHandlerTest
{
public static testMethod void testUpdateNCMOwnerHandler()
{
List<Non_Conforming_Material__c> nonConformingMaterials = new List<Non_Conforming_Material__c>();
Non_Conforming_Material__c nonConformingMaterial = new Non_Conforming_Material__c(NCM_Stage__c = 'In Process'); //#1
nonConformingMaterials.add(nonConformingMaterial);
nonConformingMaterial = new Non_Conforming_Material__c(NCM_Stage__c = 'Resolution'); //#2
nonConformingMaterials.add(nonConformingMaterial);
insert nonConformingMaterials;
}
}
For ex:
User u3 = [SELECT Id FROM User WHERE UserName='newuser@testorg.com'];
System.runAs(u3) {
All Answers
Use the below mentioned code for user creation, then use Sustem.runas(u) as show below.
Profile p = [select id from profile where name='Standard User'];
User u = new User(alias = 'test123', email='test123@noemail.com',
emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id, country='United States',
Favorite_Color__c='Pretty Pink',
timezonesidkey='America/Los_Angeles', username='test123@noemail.com');
insert u;
System.runas(u){
////////////////// write all the logic within this
}
For ex:
User u3 = [SELECT Id FROM User WHERE UserName='newuser@testorg.com'];
System.runAs(u3) {