13

I have this code:

class Patient {

  @Inject Syringe syringe;

  @PostConstruct
  void sayThankyouDoc() {

    System.out.println("That hurt like crazy!");

  }

}

@RunWith(MockitoJUnitRunner.class)
class TestCase {

  @Mock
  Syringe siringeMock;

  @InjectMocks
  Patient patient;

  //...

}

I expected Mockito to call PostConstruct, but I had to add:

@Before
public void simulate_post_construct() throws Exception {
    Method postConstruct = Patient.class.getDeclaredMethod("sayThankyouDoc", null);
    postConstruct.setAccessible(true);
    postConstruct.invoke(patient);
}

Is there a better way to do this?

Pool
  • 11,999
  • 14
  • 68
  • 78
gurghet
  • 7,591
  • 4
  • 36
  • 63
  • 1
    A different design that is both more readable and easier to test, is to not use `@PostConstruct` at all, and use constructor injection instead of field injection – geoand Mar 28 '15 at 18:09
  • @geoand but what _is_ constructor injection? – gurghet Mar 28 '15 at 19:08

1 Answers1

10

Although not a direct answer to your question, however I suggest that you move away from field injection and use constructor injection instead (makes code more readable and testable).

Your code would look like:

class Patient {

  private final Syringe syringe;

  @Inject
  public Patient(Syringe syringe) {
    System.out.println("That hurt like crazy!");
  }

}

Then your test would simply be:

@RunWith(MockitoJUnitRunner.class)
class TestCase {

  @Mock
  Syringe siringeMock;

  Patient patient;

  @Before
  public void setup() {
     patient = new Patient(siringeMock);
  }

}

Update

As suggested by Erik-Karl in the comments, you could use @InjectMocks to get rid of the setup method. The solution works because Mockito will use the appropriate constructor injection (as described here). The code would then look like:

@RunWith(MockitoJUnitRunner.class)
class TestCase {

  @Mock
  Syringe siringeMock;

  @InjectMocks
  Patient patient;

}
Community
  • 1
  • 1
geoand
  • 60,071
  • 24
  • 172
  • 190
  • 4
    Not only is it more testable, but you get some thread-safety guarantees by having the fields be final. Note that even with the above code, you would need to manually call your `@PostConstruct` method – NamshubWriter Mar 30 '15 at 04:06
  • 2
    Might be a bit old but I just want to add: InjectMock will inject through constructor if available. So here, you would only have to annotate the Patient with InjectMock and could get rid of the setup method. – Eric-Karl Jan 08 '16 at 16:23
  • @Eric-Karl Very useful! Thanks! I updated the answer with your suggestion – geoand Jan 09 '16 at 13:51
  • someone pls edit the question with the answer: No. – gabriel Jun 29 '22 at 19:01