Unitils

PartialMock objects still not properly constructed

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 2.2
  • Fix Version/s: 3.0
  • Component/s: mock
  1. unitils_test.zip
    (4 kB)
    James Davis
    29/Sep/09 2:59 AM

Activity

Hide
James Davis added a comment - 29/Sep/09 2: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.
Show
James Davis added a comment - 29/Sep/09 2: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.
Hide
James Davis added a comment - 29/Sep/09 3:01 AM
I tried this with and without the constructors on the object being mocked.
Show
James Davis added a comment - 29/Sep/09 3:01 AM I tried this with and without the constructors on the object being mocked.
Hide
James Davis added a comment - 29/Sep/09 3:02 AM
I'm currently using eclipse 3.5, java 1.6 and unitils 2.4
Show
James Davis added a comment - 29/Sep/09 3:02 AM I'm currently using eclipse 3.5, java 1.6 and unitils 2.4
Hide
Tim Ducheyne added a comment - 29/Sep/09 8: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





Show
Tim Ducheyne added a comment - 29/Sep/09 8: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
Hide
James Davis added a comment - 29/Sep/09 5: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?
Show
James Davis added a comment - 29/Sep/09 5: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?
Hide
Tim Ducheyne added a comment - 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
Show
Tim Ducheyne added a comment - 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

People

Dates

  • Created:
    29/Sep/09 2:58 AM
    Updated:
    11/Oct/09 11:07 AM
    Resolved:
    11/Oct/09 11:07 AM