easyforgood / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

Using @InjectMocks to inject an object created with @InjectMocks doesn't work #370

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Build the attached source
2. Run the unit test in InjectMocksSpyBug.java
3. The test fails as there now appear to be two different instances of bar.

What is the expected output? What do you see instead?
Test should pass because I would expect there to be only one instance of bar.

What version of the product are you using? On what operating system?
1.9.0

Please provide any additional information below.

Original issue reported on code.google.com by james.a....@gmail.com on 30 Aug 2012 at 3:28

Attachments:

GoogleCodeExporter commented 8 years ago
It seems that fields that are annotated with both @InjectMocks and @Spy are not 
proxied on the first pass.

A list is built containing all the mocks that are available for injection, 
including the original copy of the @InjectMocks&@Spy fields.

I think at this point the fields with @InjectMocks&@Spy are Proxied (the proxy 
also seems to refer to a copy of the original field).  But the list is not 
updated with the proxied references.

Now all the mocks are injected based on the list, so the original fields are 
injected, but the fields available to the unit test class are the Proxied 
copies.

Original comment by james.a....@gmail.com on 31 Aug 2012 at 1:45

GoogleCodeExporter commented 8 years ago
Yes it was made this way. As mockito is trying to do things automatically. Some 
combination don't work n edge case, such as the one you are using.

Mockito is not an injection framework, so I don't plan to work on what you are 
asking. But you can clone a repo and improve the injection code.

Actually I would be more interested in allowing mockito injection stuff to be 
easily extended by users.

Cheers,
Brice

Original comment by brice.du...@gmail.com on 3 Sep 2012 at 9:51

GoogleCodeExporter commented 8 years ago

Original comment by brice.du...@gmail.com on 3 Sep 2012 at 9:56

GoogleCodeExporter commented 8 years ago

Original comment by brice.du...@gmail.com on 3 Sep 2012 at 10:00