dhamini-poornachandra / mockito

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

Mock Injection should take generics in annotated fields into account #402

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

public class MyClass {

  private Provider<A> providerA;
  private Provider<B> providerB;
...
}

@RunWith(MockitoJUnitRunner.class) 
public class MyClassTest {

  @InjectMocks
  private MyClass cut = new MyClass();
  @Mock
  private Provider<A> providerA;
  @Mock
  private Provider<B> providerB;
...
}

What is the expected output? What do you see instead?
expected:
 providerA -> providerA
 providerB -> providerB

observed:
 something arbitrary depending on the ordering of org.mockito.internal.configuration.injection.PropertyAndSetterInjection.orderedInstanceFieldsFrom(Class<?>)

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

1.9.5

Please provide any additional information below.

I am aware of 
http://docs.mockito.googlecode.com/hg/latest/org/mockito/InjectMocks.html. 
However, the complete type information can be obtained from the @Mock-field, so 
the type-erasure-problems can be mitigated.

Original issue reported on code.google.com by frank.sc...@gmail.com on 2 Dec 2012 at 9:53

GoogleCodeExporter commented 8 years ago
Yeah it might improved, the master has now some utility code that can gather 
much more type information embedded in the class. Though it will require some 
more work to make it work in the injection system and annotation based mocks.

However you are free to try for yourself. (https://github.com/mockito/mockito)

Cheers,
Brice

Original comment by brice.du...@gmail.com on 2 Dec 2012 at 10:00

GoogleCodeExporter commented 8 years ago
Ahh, I was not quite right. The problem will only occur if the test class looks 
like this (i.e., only one Provider<?>-injection):

@RunWith(MockitoJUnitRunner.class) 
public class MyClassTest {

  @InjectMocks
  private MyClass cut = new MyClass();
  @Mock
  private Provider<A> providerA;
...
}

With two Provider<?> mocks, the NameBasedCandidateFilter will take over.

Original comment by frank.sc...@gmail.com on 2 Dec 2012 at 10:02

GoogleCodeExporter commented 8 years ago
Yeah naming is important. Still your original idea is valid. That's definately 
something that will be improved, but I don't really know when.

Original comment by brice.du...@gmail.com on 2 Dec 2012 at 10:25

GoogleCodeExporter commented 8 years ago

Original comment by brice.du...@gmail.com on 4 Dec 2012 at 3:15

GoogleCodeExporter commented 8 years ago

Original comment by brice.du...@gmail.com on 4 Dec 2012 at 3:15