0

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='[email protected]',
            emailencodingkey='UTF-8', firstname='fake', lastname='Testing', languagelocalekey='en_US',
            localesidkey='en_US', profileid = p.Id,
            timezonesidkey='America/Los_Angeles', username='[email protected]');
        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();
    }

}
1
  • To add, the debug outputs: "USER NAME: null FIRST NAME: LPO LAST NAME: Storage"
    – Matt
    Commented Jan 5, 2016 at 16:37

1 Answer 1

3
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='[email protected]',
            emailencodingkey='UTF-8', firstname='fake', lastname='Testing', languagelocalekey='en_US',
            localesidkey='en_US', profileid = p.Id,
            timezonesidkey='America/Los_Angeles', username='[email protected]');
        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

5
  • Thanks Ratan. So I query the Owner.Name in my Controller. I added a debug to the Controller after the query and it still shows it as null. agmt1 = [Select ID, Owner.Name From Apttus__APTS_Agreement__c Where ID=:agmtid LIMIT 1]; own = agmt1.owner.name; system.debug('USER NAME: ' + own);
    – Matt
    Commented Jan 5, 2016 at 16:43
  • Do I need to query the user in the test class in order for it to be available?
    – Matt
    Commented Jan 5, 2016 at 16:44
  • @Matt yes you need to query system.debug([SELECT Name from User Where Id =: u.Id]);
    – Ratan Paul
    Commented Jan 5, 2016 at 16:47
  • I was able to get a user.name by creating a new user (u2) and then querying the u1 user and including Name. However, the issue seems to be with the Owner.Name field not populating in the record being used. I've added the following code, but all debugs seem to show Owner.Name as null. Apttus__APTS_Agreement__c agmt2 = new Apttus__APTS_Agreement__c(); agmt2 = [SELECT Owner.Name, OwnerId From Apttus__APTS_Agreement__c Where id=:agmt1.id]; string ownername = [SELECT Owner.Name FROM Apttus__APTS_Agreement__c Where id=:agmt1.id].Owner.Name; update agmt2;
    – Matt
    Commented Jan 5, 2016 at 17:26
  • @Matt not sure but use system.runAs(u){ insert agmt1}
    – Ratan Paul
    Commented Jan 5, 2016 at 17:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .