dhamini-poornachandra / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

Unable to set internal state on a private field #403

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. in class:
class is accessing static field: private static Configuration config; static{ 
initialize config;} config.getProperty(key;
2. in class test:
 mockConfiguration=mock(Configuration.class);
 mockConfiguration.addProperty("key","value");
 Whitebox.setInternalState(myclass.class, "config", mockConfiguration); 
and here RunTimeException was thrown: Unable to set internal state on a private 
field

I was expecting "config" will be substituted with mock, but got an exception

mockito 1.9.0, powermock 1.4.12, os windows 7. installed JRE: jdk160_29

Original issue reported on code.google.com by sedona...@gmail.com on 4 Dec 2012 at 2:50

GoogleCodeExporter commented 8 years ago
Hi,

First I must warn you, using Whitebox, which is an internal tool, is dangerous 
because we can change behavior, signature or even remove it whenever we feel it 
it's necessary. Plus as it's internal it isn't properly documented.

The code shows that it isn't doing what you expect in this line 
`Whitebox.setInternalState(myclass.class, "config", mockConfiguration);`. You'' 
need to pass the MyClass instance, not the class. I believe the stacktrace show 
a Caused By, that probably explains more what's happening.

FieldSetter is more appropriate I believe for what you want to achieve, still 
it's an internal class, and there fore subject to change.

Hope that helps

Cheers,
Brice

Original comment by brice.du...@gmail.com on 4 Dec 2012 at 3:14

GoogleCodeExporter commented 8 years ago
Hello Brice,
thank you for your respponse. It was really helpfull.
I'm new in JUnit and Mockito

Svetlana

Original comment by sedona...@gmail.com on 5 Dec 2012 at 10:38

GoogleCodeExporter commented 8 years ago
it was really helpful.. 

Original comment by coolneha...@gmail.com on 18 Dec 2014 at 11:39