2

This is the first time am trying to use reflection for unit testing and I had this doubt.

Class Example {
  public static Map<Something, Something> someMethod()
  {
    int temp = -1;
    //Some implementation which might change the value of temp
    //depending on other cases
    if(temp == -1)
      //Do something and return something
    else
      //return null
  }
}

Now in the above snippet, I can get the initial value of the variable temp using reflection. I wanted to know, if the value of the variable changes while execution of the code how can I get the new value of temp? Am a total newbie to reflection so if this sounds silly please tolerate.

P.S The actual code am testing is not such a simple one. I have a feeling that I cannot unit test the last if condition without using reflection or powermock.

noMAD
  • 7,744
  • 19
  • 56
  • 94

3 Answers3

4

You cannot hook something like a method on a variable which is triggered after a certain event with reflection. What you normally do in unit testing is the following:

  1. Check the initial state of your variable
  2. Do some action
  3. Check whether the state of your variable is still legal

If the actions in 2. are too many (in which case I would understand your question), then you have to write the unit tests more fine-grained.

Besides if you want to check local variables with JUnit it indicates, that your method implementation might be too complex and should be split up in which case you could improve the testing, respectively make it testable at all.

Also take a look at the answers to the question: Is it bad practice to use Reflection in Unit testing?

Community
  • 1
  • 1
Konrad Reiche
  • 27,743
  • 15
  • 106
  • 143
3

You cannot use reflection to access the state of a local variable. Instance variables yes, static variables yes, local variables no.

Also, as @platzhirsch notes, reflection does not allow you to attach triggers to variables of any kind. That kind of thing requires bytecode modification of some kind. However, unit testing doesn't require triggers. Examination of values before and after should be sufficient.

I think your real problem here is that your classes have not been designed to make unit testing easy. Firstly you've got a static method, and they are hard to "mock". Secondly, it appears that the logic you are trying to test is embedded inside a method. It would be much easier if (for example) temp was an instance variable, or the code in the // Some implementation ... was a method that you could mock.


By the way, your use of the term "private" is misleading in this context. Most Java programmers would interpret "private" as meaning a static or instance variable with the private modifier. But your example has temp declared as a local variable, for and the private modifier is not allowed for local variables.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
2

As others have pointed out, you cannot use reflection to access the state of a local variable, nor can you use reflection to notify you if "anything changed".

You can, however, run your code in a debugger (for example, in the Eclipse debugger) and set a "watchpoint" on any variables you wish to monitor:

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • I use Jenkins to build and clover to check for the code coverage, hence I do not think I can do what you said. :) – noMAD Aug 06 '12 at 15:23