I only define MockitoExtension. The code inside createMyService() uses org.springframework.beans.BeanUtils. My question is that why it works. Because I didn't use SpringExtension nor SpringBootTest.
@ExtendWith(MockitoExtension.class)
class MyServiceTest {
@InjectMocks
MyService myService;
@Mock
MyRepository myRepository;
@Test
public void createMyTest() throws BusinessException {
myService.createMyService();
}
}
void createMyService(){
BeanUtils.copyProperties(createdDocumentType, documentTypeCreateResponse);
}
BeanUtils
is a standard Java class that contains only static methods. It is not defined as a Spring bean that has to be injected and you're calling a static method of the class, so Spring context and it's mechanisms are not at all involved. The only requirement for this class to work, as any other Java class, is that it needs to be present on the classpath, which is apparently the case. If you're using for example Maven, it takes care of putting dependencies on the project's classpath.
If you were to use a class instance from any Spring library, you could also do that if you created the instance yourself. Using SpringExtension
/SpringBootTest
automates this process as during Spring context creation and startup the beans are configured automatically by Spring. Still - they're just Java classes that can be used in any way, Spring only automates the whole process and prepares the configuration and instances for us.
Further reading: