function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Deniz AtaşDeniz Ataş 

I can't create User's Account and can't get User.AccountId in Test Class

@isTest
private class TestCtrTurnoverEntryCmp {
    
    @isTest
    static void testTurnoverEntry(){
        
        UserRole portalRole = [Select Id From UserRole Where PortalType = 'None' Limit 1];
        Profile profile1 = [Select Id from Profile where name = 'System Administrator'];
        User newUser = new User(
            UserRoleId = portalRole.Id,
            ProfileId = profile1.id,
            Username = 'test@managemall.com',
            email = 'test@managemall.com',
            emailencodingkey = 'UTF-8',
            localesidkey = 'en_US',
            languagelocalekey = 'en_US',
            timezonesidkey = 'America/Los_Angeles',
            alias ='nuser',
            lastname ='sahin'
        );
        insert newUser;
        
        system.runAs(newUser) 
        {
            System.debug('Current User: ' + UserInfo.getUserName());
            
            Account acc = new Account(Name = 'portalAccount', OwnerId = newUser.Id);
            insert acc;
            
            Contact con = new Contact (
                AccountId = acc.id,
                LastName = 'portalContact'
            );
            insert con;
            
            User user2 = [SELECT AccountId FROM User WHERE Username =: 
            UserInfo.getUserName()];
            System.debug('user1: ' + user1.AccountId); // this return null

            Test.startTest();
           ......
            Test.stopTest();
        }

    }
}

 
Deniz AtaşDeniz Ataş
@isTest
private class TestCtrTurnoverEntryCmp {
    
    @isTest
    static void testTurnoverEntry(){
        
        UserRole portalRole = [Select Id From UserRole Where PortalType = 'None' Limit 1];
        Profile profile1 = [Select Id from Profile where name = 'System Administrator'];
        User newUser = new User(
            UserRoleId = portalRole.Id,
            ProfileId = profile1.id,
            Username = 'test@managemall.com',
            email = 'test@managemall.com',
            emailencodingkey = 'UTF-8',
            localesidkey = 'en_US',
            languagelocalekey = 'en_US',
            timezonesidkey = 'America/Los_Angeles',
            alias ='nuser',
            lastname ='sahin'
        );
        insert newUser;
        
        system.runAs(newUser) 
        {
            System.debug('Current User: ' + UserInfo.getUserName());
            
            Account acc = new Account(Name = 'portalAccount', OwnerId = newUser.Id);
            insert acc;
            
            Contact con = new Contact (
                AccountId = acc.id,
                LastName = 'portalContact'
            );
            insert con;
            
            User user = [SELECT AccountId FROM User WHERE Username =: 
            UserInfo.getUserName()];
            System.debug('user1: ' + user.AccountId); // this return null

            Test.startTest();
           ......
            Test.stopTest();
        }

    }
}
i wrote wrong my code. New version like this.
 
Boss CoffeeBoss Coffee
AccountId will be null for Salesforce users. AccountId and ContactId are used for Customer Portal users.