Re: [powermock] How to mock this.getClass().getDeclaredFields

2,506 views
Skip to first unread message

Johan Haleby

unread,
Oct 1, 2012, 4:32:46 AM10/1/12
to powe...@googlegroups.com
Hi, 

I would strongly advise not to mock classes in Java lang reflection. While it's theatrically possible with PowerMock (if you really want to know I'll tell you :)) but it's not the way to go about. In your case I would probably not even write a unit test with mocking if you're working with legacy code. I would rather write a higher-level integration test and stub boundary methods if you cannot use dependency injection. I'm not really sure why PowerMock intercepts the call to "getDeclaredFields" though, what version of PowerMock are you using?

Also you could consider if you really need to mock the call to findBy(..)? Maybe it would be ok to simply stub it: 
stub(method(Disk.class), "findBy")).toReturn(myDiskInstance);

Regards,
/Johan

On Mon, Oct 1, 2012 at 9:53 AM, Roger Gilliar <roger....@googlemail.com> wrote:
Hi,

I just started to use PowerMock and seems that I finally found the solution to test some old legacy code that implements the ActiveRecord pattern using static finder methods.

Example:

I have the following constructor in a Base class:

protected AbstractDBO() {
  final Field[] fields = this.getClass().getDeclaredFields();
  if (fields != null) {
     for (Field field : fields) {
          oldValueMap.put(field.getName(), null);
     }
  }
}

The Disk classed inherits from AbstractDBO and implements a static finder Method (

public static Disk findBy(DiskId id) {
  return byId(Disk.class, id.getValue());
}

The static mock is defined like this:

PowerMock.mockStaticNice(DBO_Disk.class);
expect(DBO_Disk.findBy(new DiskId(BigDecimal.ONE))).andReturn(newDiskObject);

The goal should be to mock all calls to the static finder methods.

But I get: java.lang.IllegalStateException: missing behavior definition for the preceding method call getDeclaredFields()

Any ideas what I'm doing wrong ?
Regards
  Roger

--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To view this discussion on the web visit https://groups.google.com/d/msg/powermock/-/DJZPtGWRCeIJ.
To post to this group, send email to powe...@googlegroups.com.
To unsubscribe from this group, send email to powermock+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/powermock?hl=en.

Roger Gilliar

unread,
Oct 1, 2012, 5:09:29 AM10/1/12
to powe...@googlegroups.com
Hi,

stubing works perfectly and it's exactly what I need here. 

BTW: I'm using the latest release.

Thanks four your help.

Regards
  Roger

Johan Haleby

unread,
Oct 1, 2012, 5:43:33 AM10/1/12
to powe...@googlegroups.com
Ok glad you get it working. Though I don't think PowerMock ought to intercept calls to java lang reflect by default so you may have run into a bug as well.

Regards,
/Johan

--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To view this discussion on the web visit https://groups.google.com/d/msg/powermock/-/YruxYSs3DkwJ.
Reply all
Reply to author
Forward
0 new messages