Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito and CDI bean injection, does @InjectMocks call @PostConstruct?

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?

like image 875
gurghet Avatar asked Mar 28 '15 15:03

gurghet


1 Answers

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;

}
like image 75
geoand Avatar answered Sep 22 '22 22:09

geoand