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
SFDC16SFDC16 

Test class for conditional constructor

Test class for the conditional constructor.

for example '

public class Test()
{
          public List <Dashboard>  fetchdashboard{get;set;}

      Test()
      {
           List<user> s=[select id,name from user]
         if(s.name='xyz')
         {
                 fetchdashboard=[select id name from  Dashboard where                     name like '%test12323%']
         }
        else if()
        {
        }
     }
}

Test class for above code
Best Answer chosen by SFDC16
Raj VakatiRaj Vakati
try this
@isTest
public class TestEx{

    static testMethod void validateAccount() {
    
    Test.startTest();
         
       Profile profile1 = [Select Id from Profile where name = 'System Administrator'];
       System.debug('What is the profile id ' + profile1);
       UserRole portalRole = [Select Id From UserRole Where PortalType = 'None' Limit 1];
       date tDate = date.today();
       date uDate = Date.today().addDays(30);
        
         User u = new User(
            UserRoleId = portalRole.Id,
            ProfileId = profile1.Id,
            Username = 'xyz@sdasdasdasd.com',
            Alias = 'batman',
            Email='xyz@kapladsasdasdan.com',
            EmailEncodingKey='UTF-8',
            Firstname='Bruce',
            Lastname='Wayne',
            LanguageLocaleKey='en_US',
            LocaleSidKey='en_US',
            TimeZoneSidKey='America/Chicago');
            insert u;
            System.debug ('Here is the user id ' + u.id);
        
        User u1 = [SELECT id from User WHERE username = 'mc.kapqa@kaplan.com.kapqa' LIMIT 1];
       
        System.runas(u1) {
			Test t = new Test();
		}
        
           
                
    Test.stopTest();  
     }
   } 
    static testMethod void testex2() {
    
    Test.startTest();
         
      
			Test t = new Test();
		 Test.stopTest();  
     }
   } 
 }

 

All Answers

Raj VakatiRaj Vakati
Change code as below 
 
public class Test
{
   public List <Dashboard>  fetchdashboard{get;set;}

    public Test()
      {
		User s=[select id,name from user where id=:UserInfo.getUserId()]
		if(s.name='xyz')
         {
                 fetchdashboard=[select id name from  Dashboard where              name like '%test12323%']
         }
        else 
        {
        }
     }
}

Test Class
 
@isTest
public class TestEx{

    static testMethod void validateAccount() {
    
    Test.startTest();
         
       Profile profile1 = [Select Id from Profile where name = 'System Administrator'];
       System.debug('What is the profile id ' + profile1);
       UserRole portalRole = [Select Id From UserRole Where PortalType = 'None' Limit 1];
       date tDate = date.today();
       date uDate = Date.today().addDays(30);
        
         User u = new User(
            UserRoleId = portalRole.Id,
            ProfileId = profile1.Id,
            Username = 'xyz@sdasdasdasd.com',
            Alias = 'batman',
            Email='xyz@kapladsasdasdan.com',
            EmailEncodingKey='UTF-8',
            Firstname='Bruce',
            Lastname='Wayne',
            LanguageLocaleKey='en_US',
            LocaleSidKey='en_US',
            TimeZoneSidKey='America/Chicago');
            insert u;
            System.debug ('Here is the user id ' + u.id);
        
        User u1 = [SELECT id from User WHERE username = 'mc.kapqa@kaplan.com.kapqa' LIMIT 1];
       
        System.runas(u1) {
			Test t = new Test();
		}
        
           
                
    Test.stopTest();  
     }
   } 
 }

 
SFDC16SFDC16
Hello, thanks for reply my code coverage is 65% fetchdashboard=[select id name from Dashboard where name like '%test12323%'] Now I am getting error on the above line in all If else condition.
Raj VakatiRaj Vakati
try this
@isTest
public class TestEx{

    static testMethod void validateAccount() {
    
    Test.startTest();
         
       Profile profile1 = [Select Id from Profile where name = 'System Administrator'];
       System.debug('What is the profile id ' + profile1);
       UserRole portalRole = [Select Id From UserRole Where PortalType = 'None' Limit 1];
       date tDate = date.today();
       date uDate = Date.today().addDays(30);
        
         User u = new User(
            UserRoleId = portalRole.Id,
            ProfileId = profile1.Id,
            Username = 'xyz@sdasdasdasd.com',
            Alias = 'batman',
            Email='xyz@kapladsasdasdan.com',
            EmailEncodingKey='UTF-8',
            Firstname='Bruce',
            Lastname='Wayne',
            LanguageLocaleKey='en_US',
            LocaleSidKey='en_US',
            TimeZoneSidKey='America/Chicago');
            insert u;
            System.debug ('Here is the user id ' + u.id);
        
        User u1 = [SELECT id from User WHERE username = 'mc.kapqa@kaplan.com.kapqa' LIMIT 1];
       
        System.runas(u1) {
			Test t = new Test();
		}
        
           
                
    Test.stopTest();  
     }
   } 
    static testMethod void testex2() {
    
    Test.startTest();
         
      
			Test t = new Test();
		 Test.stopTest();  
     }
   } 
 }

 
This was selected as the best answer
SFDC16SFDC16
It's working correctly... Thanks