3

I have a question in regards to the best structure for latter testing of my code. I'm writing a test coverage code for an apex code I intend to deploy in my production. I'm up to 18% but I'm having some doubts on how to write the correct test block of code for something like this:

public void closePopup() { displaySavedPopup = false; }

The displaySavedPopup is declared at the beginning of the code:

public boolean displaySavedPopup {get; set;}

I can instantiate standard and extension controllers within the test coverage code, all is fine. This is the first lines of the test code:

@isTest

public class CustomObjectControllerTest {

public static testMethod void CustomObjectController(){
PageReference pagRef = Page.MyVisualForcePage;
Test.setCurrentPage(pagRef); 

// Instantiate the standard controller

CustomObject__c v = new CustomObject__c(); 
Apexpages.StandardController sc = new Apexpages.StandardController(v);

// Instantiate the extension

CustomObjectController ext = new CustomObjectController(sc);`

Like I mentioned this looks fine and it works, the code coverage is up to 18%. Please if anyone find anything that I can improve, shoot.

So if I want to test, let's say a variable I do something like:

ext.Variable ='txt'; System.assertEquals('txt', ext.Variable);

So my question is, it is better to build my closePopup() method like this:

public void closePopup(Boolean displaySavedPopup) { displaySavedPopup = false; }

and not declaring public boolean displaySavedPopup {get; set;} at the beginning of my code than:

public void closePopup() { displaySavedPopup = false; }

And declaring public boolean displaySavedPopup {get; set;} at the beginning of the code?

The issue or the second part of my question is that if I try to test closePopup() like this:

ext.displaySavedPopup = false; ext.closePopup(); System.assertEquals(false, ext.closePopup());

I get an incorrect signature System.assertEquals(Boolean, void)

If I try this:

ext.closePopup(false); System.assertEquals(false, ext.closePopup());

I get an incorrect signature:[CustomObjectController].closePopup(Boolean)

So, would this change on my apex code improve my testing coverage latter? :

public void closePopup(Boolean displaySavedPopup) { displaySavedPopup = false; }

Please let me know if I'm missing something here, that I'm sure I'm. Any tips or directions on how to improve my apex code for future testing coverage will be much appreciated.

Hope I have explain myself well, if not, let me know please.
Thanks.

1 Answer 1

1

To answer the first question - the way in which you have declared the variable and the method are fine. Your suggested method where you pass the parameter into the method would not make sense as you are not using the parameter.

To answer your second question - the reason you are getting such an error is because you have the method signature returning void. Both parameters passed in for comparison to the system.assertEquals() method must be of the same type, so you are currently trying to compare whether a boolean type is equal to a void type.

The way in which you would test the method is working would be something like:

//Firstly set the variable to be true as we want to test that the value changes
ext.displaySavedPopup = true;
//Call our method to set the value to false
ext.closePopup();
//Assert that the value is now false
System.assertEquals(false, ext.displaySavePopup);

This should cover the code and test it is operating correctly.

1
  • Thanks so much for the clarification to my questions. I did what you are saying in your reply and go 2% up in my total test coverage code percentage... Thanks, much appreciated. Commented May 8, 2014 at 13:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .