Describe the bug
I am using InjectMock annotation to mock restClient in Microprofile Rest Client way in my test annotated with @QuarkusTest, content:
@InjectMock
@RestClient
GreetingService greetingService;
in test method:
@Test
public void testHelloEndpoint() {
Mockito.when(greetingService.hello()).thenReturn("hello from server");
Rest client interface looks like this.. (notice I have added @ApplicationScoped)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
@RegisterProvider(value = RestClientLoggingMapper.class, priority = 50)
@Path("/")
@ApplicationScoped
@RegisterRestClient(configKey = "my-configkey-remote")
public interface GreetingService {
@GET
@Path("/hello")
String hello();
}
application.properties have this line which could be relevant:
my-configkey-remote/mp-rest/scope=javax.inject.Singleton
Expected behavior
The mockito should mock Greeting service
Actual behavior
org.junit.jupiter.api.extension.TestInstantiationException: Failed to create test instance
Caused by: java.lang.reflect.InvocationTargetException
Caused by: org.mockito.exceptions.base.MockitoException:
Cannot mock/spy class com.sun.proxy.$Proxy57
Mockito cannot mock/spy because :
- final class
Relates to this issue https://github.com/quarkusio/quarkus/issues/8622 with difference of
this annotation @RegisterRestClient(configKey = "my-configkey-remote")where the configKey is added.. If the annotation is plain, without configKey then this bug does not occur.
Environment (please complete the following information):
I have solved the problem.. It was in configuration - application.properties:
my-configkey-remote/mp-rest/scope=javax.inject.Singleton
which takes precedence before the @ApplicationScoped on the GreetingService interface
Nice catch
Most helpful comment
I have solved the problem.. It was in configuration - application.properties:
my-configkey-remote/mp-rest/scope=javax.inject.Singletonwhich takes precedence before the @ApplicationScoped on the GreetingService interface