mlinhard / mockito

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

Scan inlined mock on field for dependency injection #260

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Now that we have a getMock call, Mockito should detect them for mock injection.

Original issue reported on code.google.com by brice.du...@gmail.com on 13 May 2011 at 2:13

GoogleCodeExporter commented 9 years ago

Original comment by brice.du...@gmail.com on 13 May 2011 at 2:13

GoogleCodeExporter commented 9 years ago
Here's an example of what this feature could allow.

Suppose we have the following classes : 

public static class AntiCorruptionValidator {
    private PartOfTheDomain diff;
    private PartOfTheDomain original;
    //...
}

public static class PartOfTheDomain {
    public boolean applyDiff(PartOfTheDomain diff) {
        return false;
    }
}

In the test we could write this :

@RunWith(MockitoJUnitRunner.class)
public class AntiCorruptionValidatorTest {
    private PartOfTheDomain diff = mock(PartOfTheDomain.class, withSettings().serializable());
    private PartOfTheDomain original = when(mock(PartOfTheDomain.class).applyDiff(any(PartOfTheDomain.class))).thenReturn(true).getMock();

    @InjectMocks
    private AntiCorruptionValidator antiCorruption;

    @Test
    public void some_test_implying_diff_and_original() throws Exception {
        antiCorruption.applyIfOk();

        verify(original).applyDiff(same(diff));
    }

}

In other word, Mockito will detect mocks in fields and try to inject them if 
possible.

Original comment by brice.du...@gmail.com on 28 Oct 2011 at 2:06

GoogleCodeExporter commented 9 years ago

Original comment by brice.du...@gmail.com on 28 Oct 2011 at 5:20

GoogleCodeExporter commented 9 years ago

Original comment by szcze...@gmail.com on 13 May 2012 at 3:37

GoogleCodeExporter commented 9 years ago

Original comment by szcze...@gmail.com on 3 Jun 2012 at 2:06

GoogleCodeExporter commented 9 years ago

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