• andrewskelton
  • NEWBIE
  • 0 Points
  • Member since 2010

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 4
    Replies

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();
	}
}

 

 

 

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.

I have 2 classes that I am trying to move into production.  1 class is my controller and the other is a purely Test class for testing of the controller.  I am getting the error about Average Test coverage across all classes and triggers is 0%.......

 

The controller class has 0% when I run the Test in the Sandbox and the Test class has 100% coverage.

 

 

Can someone give my ideas or pointers about what I am missing. 

 

Thanks for your help.

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();
	}
}

 

 

 

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.

I have 2 classes that I am trying to move into production.  1 class is my controller and the other is a purely Test class for testing of the controller.  I am getting the error about Average Test coverage across all classes and triggers is 0%.......

 

The controller class has 0% when I run the Test in the Sandbox and the Test class has 100% coverage.

 

 

Can someone give my ideas or pointers about what I am missing. 

 

Thanks for your help.