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
Ronaldo CostaRonaldo Costa 

Test class coverage, help please!

Hello, 

 

I have only 35% code coverage on the class below and I've been trying to increase it for a while but no success. Would someone please help me? :)

 

It has been developed to fix the 10000 soql limit, because my client has more than 100 fields in his page, as you can see there are one Edit VS page and a VIEW VS page.

 

public class Contracts_AVAC {
	
    public A_C_Availability__c q1 {get; private set;}
    public A_C_Availability__c q2 {get; private set;}
    public A_C_Availability__c q3 {get; private set;}
	
    public Contracts_AVAC (ApexPages.StandardController stdController) {
       
               ID qid = stdController.getRecord().Id;


//GENERAL FIELDS 

		q1 = [select A_C_Availability__c.Name, A_C_Availability__c.Status__c, A_C_Availability__c.Serial_Number__c

					from A_C_Availability__c where id = :qid limit 1];

//MORE FIELDS
				
		q2 = [select A_C_Availability__c.PInterior__c, A_C_Availability__c.POptional__c

					from A_C_Availability__c where id = :qid limit 1];

//MORE FIELDS
				
		q3 = [select A_C_Availability__c.LOptional__c, A_C_Availability__c.LOptionalF__c,					

					from A_C_Availability__c where id = :qid limit 1];
   }

		public PageReference save() {
			update q1;
			update q2;
			update q3;
			String changepage = '/apex/Contracts_View_AVAC?id='+ApexPages.currentPage().getParameters().get('id');
			PageReference pageref = new Pagereference(changepage);
			pageref.setRedirect(true);
			return pageref;  
		}
	
			
		public PageReference NewAVAC() {

		A_C_Availability__c NewAVAC = new A_C_Availability__c (name = 'New AVAC');
		insert NewAVAC;
	
// Load Edit page

		String changepage2 = '/apex/Contracts_New_AVAC?id='+NewAVAC.Id;
		PageReference pageref2 = new Pagereference(changepage2);
		pageref2.setRedirect(true);
        return pageref2;
		}	
				

}

 Test method:

 

public static testMethod void testContracts_AVAC() {

		// Make test AVAC	
				A_C_Availability__c testAVAC = new A_C_Availability__c (Name = 'Test AVAC');
				insert testAVAC;
		
		// Get ID from new AVAC	
				PageReference pageRef = new PageReference('/apex/Contracts_New_AVAC');
				ApexPages.currentPage().getParameters().put('id', testAVAC.id);
				Test.setCurrentPage(pageRef);
		
		// Open Custom Controller	
				ApexPages.StandardController avacController = new ApexPages.StandardController(testAVAC);
		
		// Open extension
				Contracts_AVAC qe1 = new Contracts_AVAC(avacController);
		
		// Create Test Object
				A_C_Availability__c TestAVAC1 = qe1.q1;
		
		// Check fields
				System.assertEquals('Test AVAC', TestAVAC1.Name);
	}

 

Thank you so much,

 

Ronaldo.

Best Answer chosen by Admin (Salesforce Developers) 
Ronaldo CostaRonaldo Costa
Here is the solution if anybody needs it, happy to share.

// Save function
qe.mysave();
// NewAVAC function
PageReference t = qe.NewAVAC();

All Answers

clouddev@surashriclouddev@surashri

Hi,

 

You are not calling you save() & NewAVAC() function. You should call them like

 

qe1.save() - first test case

qe1.NewAVAC() - second test case.

 

I am just telling you missing link. Not seen your code in deep. Also read apex code ref available on salesforce website for more details

 

Thanks,

 

Ronaldo CostaRonaldo Costa

Thank you, I will keep trying to increase it!

 

Ronaldo.

Ronaldo CostaRonaldo Costa
Here is the solution if anybody needs it, happy to share.

// Save function
qe.mysave();
// NewAVAC function
PageReference t = qe.NewAVAC();
This was selected as the best answer
Ronaldo CostaRonaldo Costa
*reached 100% coverage now.