mlinhard / mockito

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

@InjectMocks behaving differently with Java 6 and 7 #353

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
When a class have two field that have the same type (or the same erasure), and 
the test class declares two mocks for each fields, and one mock field is named 
after the field of the tested class.

Ex: 

// tested class have two field with same type
public class FooService {
  Map<String, String> stringString = new HashMap<String, String>();
  Map<String, Integer> stringInteger = new HashMap<String, Integer>();
}

@RunWith(MockitoJUnitRunner.class)
public class FooServiceTest {
  @Mock Map<String, String> mockStringString; // can only be matched with type
  @Mock Map<String, Integer> stringInteger; // can be matched by name to FooService.stringInteger
  @InjectMocks FooService fooService;

  @Test public void checkInjection() {
    assertNotNull(fooService.stringString);
  }
}

Depending on the order of the fields, some mocks might not be injected. This is 
what happens in this case when run on JDK 6 and JDK 7. The issue lies in the 
new sort algorythm used in Arrays.sort() and consequently in Collections.sort().

In this situation you should always name all your field mocks accordingly to 
the fields of tested class.

Original issue reported on code.google.com by brice.du...@gmail.com on 29 Jun 2012 at 7:26

GoogleCodeExporter commented 9 years ago
By the way this issue is brought by Andy from SO : 
http://stackoverflow.com/questions/10755890/injectmocks-behaving-differently-wit
h-java-6-and-7

Original comment by brice.du...@gmail.com on 29 Jun 2012 at 7:31

GoogleCodeExporter commented 9 years ago

Original comment by brice.du...@gmail.com on 29 Jun 2012 at 7:45

GoogleCodeExporter commented 9 years ago
Oh and by the way the "big id" is 
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6804124

Original comment by brice.du...@gmail.com on 29 Jun 2012 at 9:26

GoogleCodeExporter commented 9 years ago
Internal field order should now be consistent across JDKs. Also added a second 
pass to better handle some edge case, where the developper didn't name all the 
mocks after the properties or fields.

revision f419a03d6f0dc11426213210442100691a264f3b

Original comment by brice.du...@gmail.com on 1 Jul 2012 at 10:33

GoogleCodeExporter commented 9 years ago

Original comment by brice.du...@gmail.com on 1 Jul 2012 at 10:33

GoogleCodeExporter commented 9 years ago

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

GoogleCodeExporter commented 9 years ago

Original comment by szcze...@gmail.com on 6 Oct 2012 at 7:38