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 

Code Coverage Question

Ok, here is my issue.  I have 1 class that I am trying to get into production.   Just for simplicity I have only 1 method that returns a list of users.   With this one class I have a the test method, and I get 31% coverage. 

 

1.  Why is it that when I add more methods code coverage goes down?

 

2.  Why can I have my class with the 1 method and a test class completely empty and still have the same coverage as before?

 

3.  Please explain or point me in the right direction about the code coverage.

 

 

Thanks.

krishnagkrishnag

can u post ur code.

andrewskeltonandrewskelton

//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;
    }
}

krishnagkrishnag

where is the test class you are talking about

andrewskeltonandrewskelton

Oh sorry forgot that part.

 

 

/**
 * 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()
	{
		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);
        
	}
  /*	static testMethod void tSave()
  	{
  		PageReference page = new PageReference('/apex/ContactsGrid_cvf');
  		Test.setCurrentPage(page);
  	}
  */	
}

 

 

 

krishnagkrishnag

at first u need to create a test user in the test class and use his Id.

yogoyogo

Hi,

 

Here is the link to "An Introduction to Apex Code Test Methods" :

http://wiki.developerforce.com/index.php/An_Introduction_to_Apex_Code_Test_Methods

 

Have a nice day !