I know about these two options:
- Use @SpyBean annotation from spring-boot-test as the only annotation
@Autowired
@InjectMocks
private ProductController productController;
@SpyBean
private ProductService productServiceSpy;
- Use Java reflection to “autowire” the spy object, e.g. ReflectionTestUtils
@Autowired
private ProductController productController;
@Autowired
private ProductService productService;
@Before
public void setUp() {
ProductService productServiceSpy = Mockito.spy(productService);
ReflectionTestUtils.setField(productController, "productService", productServiceSpy);
}