Ich habe diesen Code:Mockito und CDI Bean Injektion, ruft @InjectMocks @PostConstruct?
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 Mockito PostConstruct nennen erwartet, aber ich hatte hinzuzufügen:
@Before
public void simulate_post_construct() throws Exception {
Method postConstruct = Patient.class.getDeclaredMethod("sayThankyouDoc", null);
postConstruct.setAccessible(true);
postConstruct.invoke(patient);
}
Gibt es einen besseren Weg, dies zu tun?
ein anderes Design, das sowohl besser lesbar und einfacher zu testen ist, ist überhaupt nicht '@ PostConstruct' zu verwenden, und verwendet Konstruktor Injektion statt Feldeinkopplung – geoand
@geoand aber Was ist eine Konstruktoreinspritzung? – gurghet
Siehe meine Antwort unter – geoand