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
andrewskeltonandrewskelton 

Completely lost with test coverage.

Below is my controller class and test class.  I am getting 0% coverage in eclipse.  Please explain what and where I am missing code to get my percentage above 75%.  Thanks.

 

//Controller class
public with sharing class ContactGridController {
    
    //variable declaration
    private ApexPages.StandardController controller {get; set;}
    public List<Contact> searchResults {get;set;}
    //public String selectedUser {get;set;}
    public List<SelectOption> userList;
    
    // standard controller - could also just use custom controller
    public ContactGridController(ApexPages.StandardController controller) { }
    public ContactGridController() { }
 
    //get Users method and returns List of Users 
    private List<SelectOption> getUsers() 
    {
    	List<User> usrs = new List<User>();
        try
        {
        	
        	if (userList == null) 
        	{
            	usrs = [select id,firstname,lastname from user order by firstname, lastname ];

            	userList = new List<SelectOption>();
            	userList.add(new SelectOption('NoValue', '-Select One-'));
            
            	for (User u : usrs) 
            	{
            	    userList.add(new SelectOption(u.id, u.firstname + ' ' + u.lastname));
            	}
        	}
    	}
    	catch (Exception e)
    	{
    		String eMessage = 'System Message: ' + e;
    		System.Debug(eMessage);
    		ApexPages.addMessages(e);
    	}
    	return userList;
    }
}

 

/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class tContactGridController {
	
	static testMethod void tUsersList()
	{
		Test.startTest();
		Profile p = [select id from profile where name='Standard User']; 
         User u1 = new User(alias = 'tuser', email='standarduser@testorg.com', 
            emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US', 
            localesidkey='en_US', profileid = p.Id, 
            timezonesidkey='America/Los_Angeles', username='standarduser@yahoo.com');
			
		System.RunAs(u1)
		{
			List<SelectOption> userList;
			List<User> usrs = new List<User>();
        	try
        	{
           		if (userList == null) 
            	{
             		usrs = [select id,firstname,lastname from user order by firstname, lastname ];
	                userList = new List<SelectOption>();
                	userList.add(new SelectOption('NoValue', '-Select One-'));
            
              		for (User u : usrs) 
                	{
                 		userList.add(new SelectOption(u.id, u.firstname + ' ' + u.lastname));
                	}
            	}
        	}
        	catch (Exception e)
        	{
           		String eMessage = 'System Message: ' + e;
            	System.Debug(eMessage);
        	}
        
        	System.Assert(usrs != null);
        	System.Assert(userList != null);
      		
		}//end runAs 
		
		//me for admin testing
		User u2 = [select id from User where alias='askel'];
		System.RunAs(u2)
		{
			List<SelectOption> userList;
			List<User> usrs = new List<User>();
        	try
        	{
           		if (userList == null) 
            	{
             		usrs = [select id,firstname,lastname from user order by firstname, lastname ];
	                userList = new List<SelectOption>();
                	userList.add(new SelectOption('NoValue', '-Select One-'));
            
              		for (User u : usrs) 
                	{
                 		userList.add(new SelectOption(u.id, u.firstname + ' ' + u.lastname));
                	}
            	}
        	}
        	catch (Exception e)
        	{
           		String eMessage = 'System Message: ' + e;
            	System.Debug(eMessage);
        	}
        
        	System.Assert(usrs != null);
        	System.Assert(userList != null);
 		}//end runAs
	    Test.stopTest();
	}
}

 

 

 

BritishBoyinDCBritishBoyinDC

Not quite sure what your code is doing, but typically, if you are testing a controller, you need to create it in the test method, and then execute the methods...

 

So something like this:

 

 

ContactGridController cgc = new ContactGridController();

cgc.getUsers();

 

 

andrewskeltonandrewskelton

When I run the tests from Eclipse I get the following:

 

ContactGridControlleer (Apex Class) -- 19 lines not tested, 0% covered

tContactGridControlleer (Apex Class) -- 0 lines not tested, 100% covered

 

My test class is below.

 

@isTest
private class tContactGridController {
	
	static testMethod void tUsersList()
	{
		List<User> userList = new List<User>();
		Test.startTest();
		Profile p = [select id from profile where name='Standard User']; 
        User u1 = new User(alias = 'tuser', email='standarduser@testorg.com', 
            emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US', 
            localesidkey='en_US', profileid = p.Id, 
            timezonesidkey='America/Los_Angeles', username='standarduser@yahoo.com');
			
		System.RunAs(u1)
		{
			ContactGridController cgc = new ContactGridController();
			userList = cgc.getUsers();

			System.Assert(userList != null);
		}//end runAs 
	    Test.stopTest();
	}
}

 

 

 

BritishBoyinDCBritishBoyinDC

One thing I don't see is an insert for the new user record you create?

 

Try adding  this after the new user line

 

 

insert u1;