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

Key: UNI-124
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Tim Ducheyne
Reporter: Tim Ducheyne
Votes: 1
Watchers: 1
Operations

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

PartialMock objects not properly constructed

Created: 24/Apr/09 07:28 AM   Updated: 20/Jul/09 06:09 PM
Component/s: mock
Affects Version/s: 2.2
Fix Version/s: 3.0


 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 - 24/Apr/09 10:03 PM
Looking at the Objenesis docs, there are two strategies to instantiate objects.

There are many different strategies that Objenesis uses for instantiating objects based on the JVM vendor, JVM version, SecurityManager and type of class being instantiated.

We have defined that two different kinds of instantiation are required:

    * Stardard - No constructor will be called
    * Serializable compliant - Acts like an object instantiated by java standard serialization. It means that the constructor of the first non-serializable parent class will be called. However, readResolve is not called and we never check if the object is serializable.


So, having an attribute that determines if it uses the standard or Serializable compliant might fix this.

Tim Ducheyne - 20/Jul/09 06:09 PM
I've implemented it as follows:

if there is a default constructor (may be private), it will be called to construct the object.
If not, a warning is logged and the instance is created using objenesis without calling any constructor and initialization

Initializing the class without calling a constructor seems to be a very difficult thing to do.
In a future release we could add some code that uses ASM to add a constructor to the class and then call that one,
but that is not something that is easy to write

For now you will need to add a no-args (can be private) constructor. Of course this is not needed when there are no constructors. In that case java automatically creates a default constructor.