History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: UNI-169
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Tim Ducheyne
Reporter: James Davis
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Unitils

PartialMock objects still not properly constructed

Created: 29/Sep/09 02:58 AM   Updated: 11/Oct/09 11:07 AM
Component/s: mock
Affects Version/s: 2.2
Fix Version/s: 3.0

File Attachments: 1. Zip Archive unitils_test.zip (4 kb)



 Description  « Hide
Partial mock creation does not call constructor and does not initialize fields.
This issue was reported by James Davis
For more info and test case see forum:

https://sourceforge.net/forum/message.php?msg_id=7270235

 All   Comments   Change History      Sort Order:
James Davis - 29/Sep/09 02:59 AM
I've added an eclipse/maven project that can be used to duplicate this.

The NPR we get is because the constructor on the object being mocked is not being called.

James Davis - 29/Sep/09 03:01 AM
I tried this with and without the constructors on the object being mocked.

James Davis - 29/Sep/09 03:02 AM
I'm currently using eclipse 3.5, java 1.6 and unitils 2.4

Tim Ducheyne - 29/Sep/09 08:02 AM
Thanks for taking the time to investigate the problem.
I'll try your test this evening with the new version of unitils.
I refactored a large part of the mocking framework and it should normally also handle the object creation now.

There are some limitations however:

There has to be a default constructor (can be private or can be implicit when there is no constructor defined).
I've searched the bytecode for a way to call the initializers without calling a constructor, but that does not seem to be possible in Java.
I even tried to generate a new class and inject an empty default constructor, but no luck.
So if there is no default constructor, you cannot create an initialized version of a partial mock (a warning is logged)

The new version is comming out soon. I'm doing some last tests and I'm setting up a wiki with some code samples (I'm still looking for more samples and ideas)

http://sourceforge.net/apps/mediawiki/unitils/index.php?title=Main_Page


thanks for the support,
I'll let you know the outcome of the test
Grtz,
Tim






James Davis - 29/Sep/09 05:11 PM
One thing that I did notice is that it still appears to be using cglib for the class generation, I had thought it was supposed to be using objenisis though. Does objenisis use cglib?

Tim Ducheyne - 11/Oct/09 11:07 AM
I've tested the creation of partial mocks in the new release and everything seems to work fine.
There is 1 special case that does not work: non-static innerclasses

public class A {

    public class B {
    }
}

It is not possible to create an initialized instance of B using reflection, since this requires the creation of an instance of A. The partial mock will be created, but the initializer will not have been called (a warning will be logged)
A simple work-around is to declare the inner class as static:

public class A {

    public static class B {
    }
}

grtz,
Tim