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
SolidLucasSolidLucas 

test class help

hi someone could help me to create a test unity for my class? i'm kinda in trouble to test the both scenarios, i only got 19% from the first constructor.

my class.
 

public with sharing class VLookupListBusiness {
	
	/**
	  * Private variables 
	 **/
	private list<VLookupList__c> allLookups;
	
	private list<SelectOption> parametros;
	
	
	/**
	  * Construtor 
	 **/
	public VLookupListBusiness(String[] contexts) {
		construtorLocal(contexts);
	}
	
	
	/**
	  * Construtor 
	 **/
	public VLookupListBusiness() {
		construtorLocal( new String[]{'ClassificacaoCliente', 'ClassificacaoIntercias', 'TipoCliente', 'SegmentoGerencial', 'CanalVenda',
						'Segmento', 'MetodoEntrega', 'TipoOrdem', 'ClasseContribuinte', 'ClasseContribuinteIN68', 'UtilizacaoGas', 'TipoCobranca',
						'AtividadeEconomica', 'Organization', 'Almoxarifado', 'Parametros'});
						
		parametros = getListOptions('Parametros', 'Chave__c', true);						
	}
	
	

	 
	private list<SelectOption> getListOptions(String pContext, String pKey, boolean hasNothing){
		list<SelectOption> result = new list<SelectOption>();
		  
		if (hasNothing) 
			result.add(new SelectOption('', '-- Nenhum --'));
			
		for (VLookupList__c vlook: allLookups){
			if (vlook.Contexto__c == pContext)
				result.add(new SelectOption(String.valueOf(vlook.get(pKey)), String.valueOf(vlook.get('Name'))));
		}
		
		return result;
	}	 
	 
	private void construtorLocal(String[] contexts) {
		allLookups = new list<VLookupList__c>();
		 
		allLookups = [
			Select Id, Contexto__c, Name , Chave__c, FkChave__c, Attribute1__c
			  from VLookupList__c 
			 where Contexto__c in :contexts
		  order by Contexto__c, Name 
		];
	}
	

	
	/**
	  * Private methods 
	 **/
	
	private String getValueFromKey(list<SelectOption> pList, String pChave){
		String result = ''; 
		for (SelectOption item: pList) {
			if (item.getValue() == pChave) {
				result = item.getLabel();
				break; 
			}
		}
		return result;
	} 
	
	
	/**
	  * Public methods 
	 **/
	 
	public String getVLookupId(String pKey, String pContext){
		String result = null;
		
		for (VLookupList__c vlook: allLookups){
			if (vlook.Contexto__c == pContext)
				if (vlook.Chave__c == pKey)
					result = vlook.Id;
		}
		
		return result;
	} 
	 
	 
	public String getVLookupKey(Id pId){
		String result = '';
		
		for (VLookupList__c vlook: allLookups){
			if (vlook.Id == pId)
				result = vlook.Chave__c;
		}
		
		return result;
	} 

	
	public list<SelectOption> getListOptionsFiltered(String pContext, String pKey, String pFkKey) {
		list<SelectOption> result = new list<SelectOption>();
		result.add(new SelectOption('', '-- Nenhum --'));
		for (VLookupList__c vlook: allLookups){
			if (vlook.Contexto__c == pContext)
				if (vlook.FkChave__c == pFkKey)
					result.add(new SelectOption(String.valueOf(vlook.get(pKey)), String.valueOf(vlook.get('Name'))));
		}
		
		return result;	
	}
	
	
	public String getParameterValue(String pContext, String pParameter) {
	 	String result = null;
	 	String valueFromKey = getValueFromKey(parametros, pParameter);
	 	
	 	if (Functions.isNotEmptyOrNull(valueFromKey)){
	 		result = getVLookupId(valueFromKey, pContext);
	 	}
	 	
	 	return result;
	}	

	
	public list<SelectOption> getListOptions(String pContext){
		return getListOptions( pContext, 'Id', true);
	}
	
	

	public String getValueById(String pId) {
		String result = null;
		
		for (VLookupList__c vlook: allLookups) {
			if (vlook.Id.equals(pId)) {
				result = vlook.Name;
				break;
			}
		}
		
		return result;		
	}
	 
	public String getAttributeValueById(String pId) {
		String result = null;
		
		for (VLookupList__c vlook: allLookups) {
			if (vlook.Id.equals(pId)) {
				result = vlook.Attribute1__c;
				break;
			}
		}
		
		return result;		
	}
	
	public list<SelectOption> getListOptionsNameKey(String pContext, boolean hasNothing){
		list<SelectOption> result = new list<SelectOption>();
		  
		if (hasNothing) 
			result.add(new SelectOption('', '-- Nenhum --'));
			
		for (VLookupList__c vlook: allLookups){
			if (vlook.Contexto__c == pContext)
				result.add(new SelectOption(String.valueOf(vlook.get('Name')), String.valueOf(vlook.get('Chave__c'))));
		}
		
		return result;
	}	 
}

my test class
@isTest
private class vLookupListBusiness_tst {

    static testMethod void myUnitTest() {
       
       //Istancia do controlador
       VLookupListBusiness testList = new VLookupListBusiness();
		
		testPrimeiroCenario();
    }
    
    
    private static void testPrimeiroCenario(){
    	
    VLookupListBusiness testList = new VLookupListBusiness(VLookupList__c);
    	
     		VLookupList__c testLookupList = getLookupList(); 

      	VlookupList__c vlookupList = new VlookupList__c(
      		Chave__c = 'VlookupList__c',
			Contexto__c = 'CustomSearchPage',
			Name = 'Chave__c, Contexto__c'
      	);
		
		insert vlookupList;
		
		VlookupList__c vls = new VlookupList__c();
		List<SelectOption> so = new List<SelectOption>();
		 so.add(new SelectOption('', '-- Nenhum --'));
		 
		 insert vls;
    }
}

 
Best Answer chosen by SolidLucas
pconpcon
Since writing a test class to cover all of the facets of this class is not something that anyone on here will do for you, I can give you some pointers and hopefully get you started.  I would recommend that you do some reading on testing [1] [2] [3] to get a better understanding.  Each of your individual tests should only tests one specific portion of you class (ie a constructor test, sendEmail test, contactSelected test, etc).  You should also have both a postitive (everything works perfectly) and a negative (things are not right) test.

In your case you will want to have at least one test for EACH of your methods in your class.  Many will have multiple tests to test all of the facets of the code that is being executed

Each test should follow the following structure:
  • Setup of test data. This includes creation of any data needed by your class.  Account, Contacts etc
  • Starting the test. This is calling Test.startTest() to reset the governor limits
  • Calling your class / method
  • Stopping the test.This is calling Test.stopTest() to reset the governor limits and allow for any async jobs to finish
  • Asserting that your changes have worked
    • If you have inserted/updated/deleted data, you need to query for the updates
    • Run System.assert, System.assertEquals, System.assertNotEquals to verify that you got the correct data back
If you have any specific problems with your tests, feel free to create a new post with the part of the class you are trying to test and your current test method, and you will more likely get a better response then asking for someone to essentially write an entire test class for you.

[1] http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
[2] http://pcon.github.io/presentations/testing/
[3] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/