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
AndrewFisher_TGPAndrewFisher_TGP 

Help with Test class needed

Could someone help me write a test class for this apex code please?

public with sharing class ProfileTabUserController {
    // Purpose: Custom Chatter profile page
    private ApexPages.StandardController c;

    // Getter methods you can call from your Visualforce page, e.g. {! viewingMyProfile }
    public User subjectUser { get; set; }
    public boolean viewingMyProfile { get; set; } // Whether or not I’m viewing my profile
    public String viewerID { get; set; } // UID string for the viewing user
    public String subjectID { get; set; } // UID string for the subject user (being viewed)

    // Constructor method for the controller
    public ProfileTabUserController(ApexPages.StandardController stdController) {
        c = stdController;
        subjectID = getTargetSFDCUID();

        // If we're operating inside a tab running inside of a profile...
        if (subjectID != null) {
            // Inject the sfdc.userId URL parameter value into the id param
            // so the std User controller loads the right User record
            ApexPages.currentPage().getParameters().put('id', subjectID);
        }

        // Load the User record for the user whose profile we’re viewing
        this.subjectUser = (User)stdController.getRecord();
        Id viewer = Id.valueOf(UserInfo.getUserId());
        Id subject = Id.valueOf(subjectID);
        viewingMyProfile = (viewer == subject);
        viewerID = UserInfo.getUserId();
    }

    // Fetches URL parameter passed into a profile tab indicating which user is being viewed
    private String getTargetSFDCUID() {
        return ApexPages.currentPage().getParameters().get('sfdc.userId');
    }
    // Overrides StandardController save method to force reload of current page afterwards
    public PageReference save() {
        c.save();
        return ApexPages.currentPage();
    }

    // Overrides StandardController cancel method to force page reload
    public PageReference cancel() {
        c.cancel();
        return ApexPages.currentPage();
    }
}
Nayana KNayana K
@isTest
private class TestRunAs 
{
    public static testMethod void testRunAs() 
	{
        // Setup test data
        // This code runs as the system user
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        List<User> lstUser = new List<User>{new User(Alias = 'standt1', Email='standarduser1@testorg.com', 
														EmailEncodingKey='UTF-8', LastName='Testing1', LanguageLocaleKey='en_US', 
														LocaleSidKey='en_US', ProfileId = p.Id, 
														TimeZoneSidKey='America/Los_Angeles', UserName='standarduser1@testorg.com),
											new User(Alias = 'standt2', Email='standarduser2@testorg.com', 
											EmailEncodingKey='UTF-8', LastName='Testing1', LanguageLocaleKey='en_US', 
											LocaleSidKey='en_US', ProfileId = p.Id, 
											TimeZoneSidKey='America/Los_Angeles', UserName='standarduser1@testorg.com)
			);
			
		insert lstUser;
		
        System.runAs(lstUser[0]) 
		{
			// replace YOURPAGENAME with actual page name
			PageReference pageRef = Page.YOURPAGENAME;
			
			Test.setCurrentPage(pageRef);
			ApexPages.currentPage().getParameters().put('sfdc.userId', lstUser[1].Id);
				
			// create a new Contact standard controller by passing it the account record
			ApexPages.StandardController sc = new ApexPages.StandardController(lstUser[0]);
			
			// now pass it to the extension
			ProfileTabUserController stdController = new ProfileTabUserController(sc);
			stdController.save();
			
			// verify if current page is loaded again with Id = lstUser[1].Id (subject id)
			system.assertEquals(String.valueOf(lstUser[1].Id), ApexPages.currentPage().getParameters().get('id'));
			
			// now pass it to the extension
			stdController = new ProfileTabUserController(sc);
			stdController.cancel();
			
			// verify if current page is loaded again with Id = lstUser[0].Id (subject id)
			system.assertEquals(String.valueOf(lstUser[1].Id), ApexPages.currentPage().getParameters().get('id'));
        }
    }
}

The above code may work.
AndrewFisher_TGPAndrewFisher_TGP
thanks, this helps - there are a few problems but I can amend
Nayana KNayana K
Please mark this as solved if my answer helped. If not, please post the answer if you solved by yourself and mark  that as best answer.It helps others if anyone facing same issue.
AndrewFisher_TGPAndrewFisher_TGP
No this didnt work - any further help would be great
Nayana KNayana K
Can you please let me know what the error says ?