ssacher-tgm / mockito

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

Initialization of @InjectMocks and @Spy fields if possible #209

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
As of today when using @InjectMocks, the annotated field must already be 
initialized for the mocks to be injected.

What I'm proposing with the attached patch, is that Mockito can initialize the 
field, if the type allow it. ie it is not an inner class or a local class as 
they need the holder's reference and of course it is not an abstract class nor 
an interface.
However static member classes can be instantiated.

The zero-arg constructor can be private. So Mockito create and inject mocks on 
your object without compromising the "mutable safe api".

Of course if the field is already initialized, the field is left untouched.

Original issue reported on code.google.com by brice.du...@gmail.com on 7 Aug 2010 at 5:36

Attachments:

GoogleCodeExporter commented 8 years ago
For documentation, this patch will allow to write tests like that

@RunWith(MockitoJUnitRunner.class)
public class TheTest {
  @InjectMocks private TheTested tested;
  @Mock private Map mapToBeInjected;
  @Mock private List listToBeInjected;

  @Test
  public void testSomeBehaviour() {}
}

while in the present version, the @InjectMocks field must be initialized.

As for the "mutable safe API" of your tested class, it means you can actually 
write something like. So you don't break the encapsulation and the mutability 
of your object.

For example following the Joshua Bloch builder style:

public class TheTested {
  private Map map;
  private List list;
  private TheTested() {} // for instanciation in mockito (or other frameworks such as hibernate, depending on your configuration)

  private TheTested(Map map, List list) { ... }

  public void someBehaviour() { ... }
  public void someOtherBehaviour() { ... }

  public static Builder builder() { return new Builder(); }

  public Builder {
    private Map map;
    private List list;
    public Builder withThatMap(Map str) { ... }
    public Builder withThatList(List list) { ... }
    public TheTested build() { return new TheTested(map, list); }
  }
}

Original comment by brice.du...@gmail.com on 12 Aug 2010 at 9:29

GoogleCodeExporter commented 8 years ago
Hey,

Wanted to install the patch but it seems FieldInitializer is missing. Wanna 
update the patch?

Cheers :)
Szczepan

Original comment by szcze...@gmail.com on 13 Aug 2010 at 11:26

GoogleCodeExporter commented 8 years ago
Ouch, indeed.

I'm trying IntelliJ right now, and I missed the new classes.

Well here's the fixed patch.

Sorry ;)
Brice

Original comment by brice.du...@gmail.com on 13 Aug 2010 at 5:27

Attachments:

GoogleCodeExporter commented 8 years ago
Hello back,

I wanted to try the Mercurial clones, but google got only server errors for me.

Anyway here's an updated patch, with a few more unit test, and it also allow 
mockito to instantiate spies if the type have a no-arg constructor.

Original comment by brice.du...@gmail.com on 17 Oct 2010 at 6:30

Attachments:

GoogleCodeExporter commented 8 years ago
Brice I updated the title of this issue - please correct me if it's not right

Original comment by szcze...@gmail.com on 18 Oct 2010 at 12:13

GoogleCodeExporter commented 8 years ago

Original comment by brice.du...@gmail.com on 18 Oct 2010 at 12:32

GoogleCodeExporter commented 8 years ago

Original comment by brice.du...@gmail.com on 18 Oct 2010 at 5:44

GoogleCodeExporter commented 8 years ago

Original comment by szcze...@gmail.com on 29 Oct 2010 at 7:50

GoogleCodeExporter commented 8 years ago

Original comment by szcze...@gmail.com on 3 Jul 2011 at 12:43

GoogleCodeExporter commented 8 years ago

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