Issue 403 in mockito: Unable to set internal state on a private field

1,464 views
Skip to first unread message

moc...@googlecode.com

unread,
Dec 4, 2012, 9:50:24 AM12/4/12
to mocki...@googlegroups.com
Status: New
Owner: ----
Labels: Type-Defect Priority-Medium

New issue 403 by sedona...@gmail.com: Unable to set internal state on a
private field
http://code.google.com/p/mockito/issues/detail?id=403

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




moc...@googlecode.com

unread,
Dec 4, 2012, 10:14:44 AM12/4/12
to mocki...@googlegroups.com
Updates:
Status: Invalid

Comment #1 on issue 403 by brice.du...@gmail.com: Unable to set internal
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




moc...@googlecode.com

unread,
Dec 5, 2012, 5:38:15 AM12/5/12
to mocki...@googlegroups.com

Comment #2 on issue 403 by sedona...@gmail.com: Unable to set internal
Hello Brice,
thank you for your respponse. It was really helpfull.
I'm new in JUnit and Mockito

Svetlana

moc...@googlecode.com

unread,
Dec 18, 2014, 6:39:48 PM12/18/14
to mocki...@googlegroups.com

Comment #3 on issue 403 by coolneha...@gmail.com: Unable to set internal
state on a private field
https://code.google.com/p/mockito/issues/detail?id=403

it was really helpful..

--
You received this message because this project is configured to send all
issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings
Reply all
Reply to author
Forward
0 new messages