+ Start a Discussion
andrewskeltonandrewskelton 

Test Coverage 0% across 2 classes - Help

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.

krishnagkrishnag

Try to write the test metod for the controller in the controller itslef instead of writing a seperate class.That more efficient.

bob_buzzardbob_buzzard

It sounds like you test class isn't actually doing anything with the controller if the coverage is 0% - can you post your code?

andrewskeltonandrewskelton

Here is my controller and test code.  My question is does the test class have to create an instance of the controller?  Thanks for all your help.

 

Code:

 

//Controller class
public with sharing class ContactGridController {
    
    //variable declaration
    private ApexPages.StandardController controller {get; set;}
    public List<Contact> searchResults {get;set;}
    public string searchText {get;set;}
    public String selectedUser {get;set;}
    public String Golf = '';
    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 
    public 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;
    }
    
    //get Golf value and return value
    public String getGolf()
    {
        return Golf;
    }
  
    //set method that sets value = Golf
    public void setGolf (String value) 
    {
        this.Golf= value;
    }
    
    // fired when the search button is clicked
    public PageReference Find() 
    {
        try
        {
       		String qrySeed = 'select c.AccountId, c.Name, c.Email, c.ChristmasCards__c, c.GolfTournament__c, ' +
        	'c.ClientAppreciation__c, c.InfoNewsletter__c, OwnerId from Contact c where c.isdeleted = false ';
        	String qryEnd = 'order by c.name limit 1000'; 
       		String qryB, qrySearch;
        	String qry;
      	        
        	if (selectedUser == 'NoValue')
        	{
        	    qryB = '';
        	}
        	else
        	{
        	    qryB = ' and c.OwnerId = \'' + SelectedUser + '\' ' ;
        	}
      
        	if (searchText == '')
        	{
        	    qrySearch = '';
        	}
        	else
        	{
         	   qrySearch = ' and c.Account.Name LIKE \'%'+ searchText+ '%\' ';
       		}     
     
      		qry = qrySeed + qryB + qrySearch + qryEnd;
      		searchResults = Database.query(qry);
        }
        catch (Exception e)
        {
        	System.Debug('Error running query.  Please try your query again.');
        	System.Debug('System Message: ' + e);
        }
        return null;
    }
 
    // fired when the save records button is clicked
    public PageReference save() 
    {
        try 
        {
            update searchResults;
        } 
        Catch (DMLException e) 
        {
            ApexPages.addMessages(e);
        }
        catch (Exception ex)
        {
        	ApexPages.addMessages(ex);
        }
 		return new PageReference('/apex/ContactsGrid_cvf');
    }
 
    // discards changes made to grid
    public PageReference discard()
    {
        return new PageReference('/apex/ContactsGrid_cvf' );
    }
  
    // takes user back to Home tab
    public PageReference cancel() 
    {
        return new PageReference('/home/home.jsp' );
    }
}

 Test Class:

 

/**
 * 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 tLoadContacts()
  	{
  		//create test account to tie to test contacts
  		Account test = new Account(name='TestAccount',ShippingCity='Houston',ShippingCountry = 'USA', ShippingPostalCode = '77477', ShippingState = 'TX', ShippingStreet = '123 Any Way');

		//create 200 test contacts
  		Contact[] contacts = new Contact[0];
  		for (Integer i = 0; i < 200; i++)
  		{
  			contacts.add( new Contact(AccountId=test.id,
  			FirstName = 'tJohn',
  			LastName = 'tJones',
  			Email = 'tJohn.tJones@yahoo.com',
  			GolfTournament__c = false,
  			ChristmasCards__c = false,
  			ClientAppreciation__c = false,
  			InfoNewsletter__c = false) );
  			
  		}
  		//insert contacts
  		try
  		{
  			insert contacts;
  		}
  		catch (System.Exception exContacts)
        {
        	System.Debug('Contacts failed to insert');
        }
  		System.Assert (contacts != null);
  	}
  
  	static testMethod void tPositiveRun()
  	{
  		//get contacts for updating
  		List<Contact> searchResults;
        String qrySeed = 'select c.AccountId, c.Name, c.FirstName, c.LastName, c.Email, c.ChristmasCards__c, c.GolfTournament__c, ' +
           'c.ClientAppreciation__c, c.InfoNewsletter__c, OwnerId from Contact c where c.FirstName like \'tjo%\' and c.isdeleted = false ';
        String qryEnd = 'order by c.name limit 200'; 
        String qry;
        qry = qrySeed + qryEnd;
        
        System.Debug (qry);
        
        try
        {
        	searchResults = Database.query(qry);
        	
        	//make changes to contacts info
  			for(Contact c : searchResults)
            {
                c.GolfTournament__c = true;
                c.ChristmasCards__c = true;
                c.ClientAppreciation__c = true;
                c.InfoNewsletter__c = true;
                if ( c.Email != null )
                {
                    c.Email = c.Email.replace('.com', '.net');
                }
                else
                {
                    c.Email = c.FirstName + '.' + c.LastName + '@yahoo.com' ;
                }
            }  //end of for loop
            
            upsert searchResults;
        }
  		
  		catch(DMLException exDML)
  		{
  			System.Assert( exDML.getMessage().contains('Maximum Limit exceeded'), exDML.getMessage() );
  		}
  		catch (System.Exception ex)
        {
        	System.Assert( ex.getMessage().contains('Maximum Limit exceeded'), ex.getMessage() );
        }
        
  			
  	}//end tPositiveRun
  	
  	static testMethod void tNegativeRun()
  	{
  		//get contacts for updating
  		List<Contact> searchResults;
        String qrySeed = 'select c.AccountId, c.Name, c.FirstName, c.LastName, c.Email, c.ChristmasCards__c, c.GolfTournament__c, ' +
           'c.ClientAppreciation__c, c.InfoNewsletter__c, OwnerId from Contact c where c.FirstName like \'tjo%\' and c.isdeleted = false ';
        String qryEnd = 'order by c.name limit 200'; 
        String qry;
        qry = qrySeed + qryEnd;
        
        System.Debug (qry);
        
        try
        {
        	searchResults = Database.query(qry);
        	
        	//make changes to contacts info
  			for(Contact c : searchResults)
            {
                c.GolfTournament__c = true;
                c.ChristmasCards__c = true;
                c.ClientAppreciation__c = true;
                c.InfoNewsletter__c = true;
                if ( c.Email != null )
                {
                    c.Email = c.Email.replace('.com', '.net');
                }
                else
                {
                    c.Email = c.FirstName + '.' + c.LastName + '@yahoo.com' ;
                }
            }  //end of for loop
            
            upsert searchResults;
        }
  		
  		catch(DMLException exDML)
  		{
  			System.Assert( exDML.getMessage().contains('Maximum Limit exceeded'), exDML.getMessage() );
  		}
  		catch (System.Exception ex)
        {
        	System.Assert( ex.getMessage().contains('Maximum Limit exceeded'), ex.getMessage() );
        }
        
  			
  	}//end tNegativeRun 	
  	
  	static testMethod void tCancel()
  	{
  		PageReference page = new PageReference('/home/home.jsp');
  		Test.setCurrentPage(page);
  	}
  	
  	static testMethod void tDiscard()
  	{
  		PageReference page = new PageReference('/apex/ContactsGrid_cvf');
  		Test.setCurrentPage(page);
  	}
}

 

 

 

crop1645crop1645

The test method most definitely has to create an instance of the controller class.  The testmethod has to 'simulate' what happens when VF performs an action (i.e. set up query string parms,  instantiate/construct a controller class, instantiate public properties, and then execute the controller action. 

 

This is all documented in the VF Developers Guide under Controller testing