'wrap a spy in a springBoot application class throws java.lang.IllegalStateException: Failed to unwrap proxied object

i'am wrapping a service in a spy , that i put in springBoot application class annotated with @SpringBootApplication, when i'am injecting the service in a test class it does not work, it injects the real service and not the spy , the test fails and i have the following exception : java.lang.IllegalStateException: Failed to unwrap proxied object.

here is my code :

@SpringBootApplication
public class ApiApplication {


    @Bean
    @Primary
    public ApiService myservice(ApiService actual) {
        return Mockito.spy(actual);
    }

and i inject it in test :

public class ApiStepDefs {

    private final MockMvc mockMvc;
    private final ApiService apiServiceCustom;


    public ApiStepDefs(MockMvc mockMvc, ApiService apiServiceCustom) {
        this.mockMvc = mockMvc;
        this.apiServiceCustom = apiServiceCustom;
    }

thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source