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?
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With