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
newbiewithapexnewbiewithapex 

How do I write test class for sort methods?

I have a class which has different sort methods and I need help writing test class for those methods. Here is my main class. 
public class TrialCustomerPortalHomePageController {
   
   User loggedInUser = [Select id,contactId,Contact.AccountId from User where id = :UserInfo.getUserId()];
   
   public List<Asset> MyRegisteredProducts {get; set;}
   public List<Case> MyRecentCases {get; set;}
   public List<Solution> TopSolutions {get; set;}
   public List<Idea> PopularIdeas {get; set;}
   
   public String sortField1 {get; set;}
   public String previousSortField1 {get; set;}
   public String sortField2 {get; set;}
   public String previousSortField2 {get; set;}
   public String sortField3 {get; set;}
   public String previousSortField3 {get; set;}
   public String sortField4 {get; set;}
   public String previousSortField4 {get; set;}
            
   public TrialCustomerPortalHomePageController() {
        MyRegisteredProducts = [select id,Name,SerialNumber,InstallDate,UsageEndDate,Status 
                                from Asset 
                                where ContactId = :loggedInUser.ContactId 
                                order by SerialNumber desc limit 3];
        
        MyRecentCases = [select id,CaseNumber,Subject,Status,LastModifiedDate 
                         from Case 
                         where ContactId = :loggedInUser.ContactId 
                         order by LastModifiedDate desc limit 3];
        
        TopSolutions = [select id,SolutionName,TimesUsed,LastModifiedDate 
                        from Solution 
                        order by TimesUsed desc limit 3];
                              
        PopularIdeas = [select id,Title,Categories,VoteTotal 
                        from Idea 
                        order by VoteTotal desc limit 3];
   }
   
   public void SortProducts(){
        String order = 'asc';
        if(previousSortField1 == sortField1){
            order = 'desc';
            previousSortField1 = null;
        }else{
            previousSortField1 = sortField1;
        }
        superSort.sortList(MyRegisteredProducts,sortField1,order);
    }
    public void SortCases(){
        String order = 'asc';
        if(previousSortField2 == sortField2){
            order = 'desc';
            previousSortField2 = null;
        }else{
            previousSortField2 = sortField2;
        }
        superSort.sortList(MyRecentCases,sortField2,order);
    }
    public void SortSolutions(){
        String order = 'asc';
        if(previousSortField3 == sortField3){
            order = 'desc';
            previousSortField3 = null;
        }else{
            previousSortField3 = sortField3;
        }
        superSort.sortList(TopSolutions,sortField3,order);
    }
    public void SortIdeas(){
        String order = 'asc';
        if(previousSortField4 == sortField4){
            order = 'desc';
            previousSortField4 = null;
        }else{
            previousSortField4 = sortField4;
        }
        superSort.sortList(PopularIdeas,sortField4,order);
    }
    
}
Raj VakatiRaj Vakati
Use this
 
@isTest
public class TrialCustomerPortalHomePageControllerTest {

	@isTest 
	static void testmethod1() {
		
		//Create portal account owner
UserRole portalRole = [Select Id From UserRole Where PortalType = 'None' Limit 1];
Profile profile1 = [Select Id from Profile where name = 'System Administrator'];
User portalAccountOwner1 = new User(
 UserRoleId = portalRole.Id,
 ProfileId = profile1.Id,
 Username = System.now().millisecond() + 'test2@test.com',
    Alias = 'batman',
 Email='bruce.wayne@wayneenterprises.com',
 EmailEncodingKey='UTF-8',
 Firstname='Bruce',
 Lastname='Wayne',
 LanguageLocaleKey='en_US',
 LocaleSidKey='en_US',
 TimeZoneSidKey='America/Chicago'
);
Database.insert(portalAccountOwner1);

//Create account
Account portalAccount1 = new Account(
 Name = 'TestAccount',
 OwnerId = portalAccountOwner1.Id
);
Database.insert(portalAccount1);
     
//Create contact
Contact contact1 = new Contact(
    FirstName = 'Test',
     Lastname = 'McTesty',
 AccountId = portalAccount1.Id,
     Email = System.now().millisecond() + 'test@test.com'
);
Database.insert(contact1);
     
//Create user
Profile portalProfile = [SELECT Id FROM Profile WHERE Name LIKE '%Portal User%' Limit 1];
User user1 = new User(
 Username = System.now().millisecond() + 'test12345@test.com',
 ContactId = contact1.Id,
 ProfileId = portalProfile.Id,
 Alias = 'test123',
 Email = 'test12345@test.com',
 EmailEncodingKey = 'UTF-8',
 LastName = 'McTesty',
 CommunityNickname = 'test12345',
 TimeZoneSidKey = 'America/Los_Angeles',
 LocaleSidKey = 'en_US',
 LanguageLocaleKey = 'en_US'
);
Database.insert(user1);

	System.runas(user1){



Asset ass = new Asset();
ass.Name = 'Test Asset';
ass.ContactId = contact1.Id;
ass.AccountId = ass.Id;
insert ass;

Case cs =new Case(
            AccountId = acc.Id,
            ContactId = contact1.Id,
            Type='My Type',
            Origin='My Origin',
            Status='My Status''
          );
		  insert cs ;
      


Idea myIdea = new Idea();
            myIdea.title = ' Test Idea';
            myIdea.body = ' test idea';
            insert myIdea;
			
			Solution s = new Solution();
			s.Name ='Demo';
			s.SolutionNote='Worning';
			insert s ;
            
            IdeaComment iComm = new IdeaComment(IdeaId = myIdea.Id);
            iComm.CommentBody = ' I like this idea';
            insert iComm;
			
		TrialCustomerPortalHomePageController csp = new TrialCustomerPortalHomePageController();
		csp.SortProducts();
		csp.SortCases();
		csp.SortSolutions();
		csp.SortIdeas();
		
	}	
	   }
    }  
}

 
newbiewithapexnewbiewithapex
I am getting following inert failed error for "Database.insert(portalAccount1);" line.
 METHOD RESULT 
testmethod1 : Fail

 STACK TRACE 
Class.TrialCustomerPortalHomePageControllerTst.testmethod1: line 26, column 1

 MESSAGE 
System.DmlException: Insert failed. First exception on row 0; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): Account, original object: User: []

I was also getting error for "s.Name = 'Demo';" but I commented that line for now 
Raj VakatiRaj Vakati
Do like this 

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dml_non_mix_sobjects_test_methods.htm
public class InsertFutureUser {
    @future
    public static void insertUser() {
     	
		//Create portal account owner
UserRole portalRole = [Select Id From UserRole Where PortalType = 'None' Limit 1];
Profile profile1 = [Select Id from Profile where name = 'System Administrator'];
User portalAccountOwner1 = new User(
 UserRoleId = portalRole.Id,
 ProfileId = profile1.Id,
 Username = System.now().millisecond() + 'test2@test.com',
    Alias = 'batman',
 Email='bruce.wayne@wayneenterprises.com',
 EmailEncodingKey='UTF-8',
 Firstname='Bruce',
 Lastname='Wayne',
 LanguageLocaleKey='en_US',
 LocaleSidKey='en_US',
 TimeZoneSidKey='America/Chicago'
);
Database.insert(portalAccountOwner1);

//Create account
Account portalAccount1 = new Account(
 Name = 'TestAccount',
 OwnerId = portalAccountOwner1.Id
);
Database.insert(portalAccount1);
     
//Create contact
Contact contact1 = new Contact(
    FirstName = 'Test',
     Lastname = 'McTesty',
 AccountId = portalAccount1.Id,
     Email = System.now().millisecond() + 'test@test.com'
);
Database.insert(contact1);
     
//Create user
Profile portalProfile = [SELECT Id FROM Profile WHERE Name LIKE '%Portal User%' Limit 1];
User user1 = new User(
 Username = System.now().millisecond() + 'test12345@test.com',
 ContactId = contact1.Id,
 ProfileId = portalProfile.Id,
 Alias = 'test123',
 Email = 'test12345@test.com',
 EmailEncodingKey = 'UTF-8',
 LastName = 'McTesty',
 CommunityNickname = 'test12345',
 TimeZoneSidKey = 'America/Los_Angeles',
 LocaleSidKey = 'en_US',
 LanguageLocaleKey = 'en_US'
);
Database.insert(user1);
    }
}

@isTest
public class TrialCustomerPortalHomePageControllerTest {

	@isTest 
	static void testmethod1() {
		         InsertFutureUser.insertUser();

				 User  user1 =[Select Id from User limit 1 ] ;

	System.runas(user1){



Asset ass = new Asset();
ass.Name = 'Test Asset';
ass.ContactId = contact1.Id;
ass.AccountId = ass.Id;
insert ass;

Case cs =new Case(
            AccountId = acc.Id,
            ContactId = contact1.Id,
            Type='My Type',
            Origin='My Origin',
            Status='My Status''
          );
		  insert cs ;
      


Idea myIdea = new Idea();
            myIdea.title = ' Test Idea';
            myIdea.body = ' test idea';
            insert myIdea;
			
			Solution s = new Solution();
			s.Name ='Demo';
			s.SolutionNote='Worning';
			insert s ;
            
            IdeaComment iComm = new IdeaComment(IdeaId = myIdea.Id);
            iComm.CommentBody = ' I like this idea';
            insert iComm;
			
		TrialCustomerPortalHomePageController csp = new TrialCustomerPortalHomePageController();
		csp.SortProducts();
		csp.SortCases();
		csp.SortSolutions();
		csp.SortIdeas();
		
	}	
	   }
    }  
}