[SalesForce] Test Class – Created User, but User.Name = null

I have been trying to create a test class for a Visualforce page controller and have run into a slight issue. I have total coverage except for a section of the controller that looks at the Owner.Name. When I use a system.debug in my test class, it appears that even though I am creating a test user in the test class with firstname and lastname filled out, the Name field is appearing null.

Can anyone help with this? Let me know if you need to see the controller as well.

Test Class:

@isTest
public class AttachmentUploadController_Test {

    private static  testMethod void AttachmentUploadController_Test()
    {

        Profile p = [select id from profile where name='Standard User'];
        User u = new User(alias = 'standt', email='standarduser6@testorg.com',
            emailencodingkey='UTF-8', firstname='fake', lastname='Testing', languagelocalekey='en_US',
            localesidkey='en_US', profileid = p.Id,
            timezonesidkey='America/Los_Angeles', username='standarduser6@testorg.com');
        insert u;

        Apttus__APTS_Agreement__c agmt1 = new Apttus__APTS_Agreement__c();        
        agmt1.Ownerid = u.id;
        insert agmt1;

        Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');

        test.starttest();

        PageReference pgRef = Page.Attach_File_Upload;
        Test.setCurrentPageReference (pgRef);
        ApexPages.currentPage().getParameters().put('Id',agmt1.id);

        AttachmentUploadControllerDemo attch = new AttachmentUploadControllerDemo(new ApexPages.StandardController(agmt1));  
        System.assertEquals(true, attch.preattached);
        attch.unAttach();
        attch.upload();
        AttachmentUploadControllerDemo.attachmentName = 'Test';
        AttachmentUploadControllerDemo.attachmentBody = bodyBlob;
        attch.upload();

        u.firstname='LPO';
        u.lastname='Storage';
        update u;
        system.debug('USER NAME:  ' + u.name + '   FIRST NAME:  ' + u.firstname + '   LAST NAME:  ' + u.lastname);
        AttachmentUploadControllerDemo attch2 = new AttachmentUploadControllerDemo(new ApexPages.StandardController(agmt1));
        attch2.upload();
        test.stopTest();
    }

}

Best Answer

update u;
system.debug('USER NAME:  ' + u.name + '   FIRST NAME:  ' + u.firstname + '   LAST NAME:  ' + u.lastname);

in order to get name query on User

system.debug([SELECT Name from User Where Id =: u.Id]);

Reason

User u = new User(alias = 'standt', email='standarduser6@testorg.com',
            emailencodingkey='UTF-8', firstname='fake', lastname='Testing', languagelocalekey='en_US',
            localesidkey='en_US', profileid = p.Id,
            timezonesidkey='America/Los_Angeles', username='standarduser6@testorg.com');
        insert u;

When you created user you didn't mentioned Name field(Everyone knows Salesforce update that for us).

So we need to query to get name


Also If we need to set the owner Id and name then use

system.runAs(u){ 
      insert agmt1;
}

So agmt1 will be insert into u(user) context and that user will be owner of agmt1 record