I am adding cookie to my page in the controller using ApexPages.currentPage().setCookies(new Cookie[]{CookieName});

When am trying to get the code coverage, my test class is not covering the above line.

How to get the code coverage for the above setcookie method in the test class.

  • Are you using test.setCurrentPage(Page.vfpagename) in your unit test? Commented Aug 23, 2012 at 13:41
  • Yes. But it has no effect.
    – Priyanka
    Commented Aug 23, 2012 at 13:49
  • Were you ever able to get this working?
    – Phil B
    Commented Mar 21, 2013 at 15:05

1 Answer 1


I have no issue covering the following:

//name, value, path, maxAge, isSecure
Cookie cookie = new Cookie('name', 'value', null, -1, false);
ApexPages.currentPage().setCookies(new Cookie[]{cookie});

Do you have the test data set up such that the setCookies code will actually get executed? Add in a debug message before it and after it and then see them in the debug log.

Also, I've had the code coverage behave odd at times and I had to click the Compile All Classes link and then the Clear Code Coverage button, before executing the unit test again to get accurate code coverage. You might want to try that.


You must log in to answer this question.

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