Mocking/stubbing private variables of a class without getter and setter methods
<p>I recently started working on TDD for exisitng project and faced couple of issues, one of those is mentioned below</p>

<p>I have a private variable to be mocked in test class and the variable looks like below</p>

<pre><code>private Class&lt;XYZ&gt; cls = XYZ.class;
</code></pre>

<p>later this "cls" variable is used as arugment for one of the method as className</p>

<pre><code>private List create(Class className, Object objectTO,   List&lt;String&gt; names)
</code></pre>

<p>I know private variables can be mocked and 
I mocked the private variable in my test case by following below steps</p>

<ol>
<li>Declared a <code>java.lang.reflect.Field;</code></li>
<li><code>Field field = PowerMockito.field(XYZ.class,"cls");</code></li>
<li><code>field.set(XYZ.class, "objectOfXYZClass");</code></li>
</ol>

<p>When i run my test class,i'm getting below error</p>

<pre><code>java.lang.IllegalArgumentException: Can not set java.lang.Class field com.tools.XYZ.cls to java.lang.Class
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:55)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:75)
at java.lang.reflect.Field.set(Field.java:680)
</code></pre>

<p>Please can someone help me out on this and let me know what i'm missing.</p>

<p>PS: I used <code>@preparefortest</code> and mentioned all the required classes in that and running my test class using <code>@runwith(powermockrunner.class)</code></p>