-
Example project: https://github.com/hantsy/quarkus-sandbox/tree/master/restclient-kotlin-co There is a controller like this: @Path("/api")
@RequestScoped
class PostController() {
@Inject
@field:RestClient
lateinit var client: PostResourceClient
@GET
@Produces(MediaType.APPLICATION_JSON)
suspend fun getPosts(
@QueryParam("q")
q: String?,
@QueryParam("offset")
@DefaultValue("0")
offset: Int,
@QueryParam("limit")
@DefaultValue("10")
limit: Int
): Response = coroutineScope {
val posts = async { client.getAllPosts(q, offset, limit) }
val count = async { client.countAllPosts(q) }
ok(Page(posts.await(), count.await())).build()
}
} In the controller test, I use mockk to mock the client bean here, @OptIn(ExperimentalCoroutinesApi::class)
@QuarkusTest
class PostControllerTest {
@InjectMock
@field:RestClient
lateinit var client: PostResourceClient
@Test
fun getPosts() = runTest {
coEvery { client.getAllPosts(any(), any(), any()) } returns listOf(
Post(id = UUID.randomUUID().toString(), title = "foo", content = "bar", createdAt = LocalDateTime.now()),
Post(id = UUID.randomUUID().toString(), title = "foo2", content = "bar2", createdAt = LocalDateTime.now())
)
coEvery { client.countAllPosts(any()) } returns 15L
//@formatter:off
given()
.accept(ContentType.JSON)
.`when`()
.get("/api")
.then()
.log().all()
.statusCode(200)
.body("count", equalTo(15))
//@formatter:on
coVerify(exactly = 1) { client.getAllPosts(any(), any(), any()) }
coVerify(exactly = 1) { client.countAllPosts(any()) }
}
} Run the test, and got the following exception. HTTP/1.1 415 Unsupported Media Type
content-length: 0
java.lang.AssertionError: 1 expectation failed.
Expected status code <200> but was <415>.
at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486)
at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:73)
at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:108)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:57)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:263)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:277)
at io.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure.validate(ResponseSpecificationImpl.groovy:512)
at io.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure$validate$1.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:139)
at io.restassured.internal.ResponseSpecificationImpl.validateResponseIfRequired(ResponseSpecificationImpl.groovy:696)
at io.restassured.internal.ResponseSpecificationImpl.this$2$validateResponseIfRequired(ResponseSpecificationImpl.groovy)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at org.codehaus.groovy.runtime.callsite.PlainObjectMetaMethodSite.doInvoke(PlainObjectMetaMethodSite.java:43)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:198)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:62)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:171)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:185)
at io.restassured.internal.ResponseSpecificationImpl.statusCode(ResponseSpecificationImpl.groovy:135)
at io.restassured.specification.ResponseSpecification$statusCode$0.callCurrent(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:171)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:185)
at io.restassured.internal.ResponseSpecificationImpl.statusCode(ResponseSpecificationImpl.groovy:143)
at io.restassured.internal.ValidatableResponseOptionsImpl.statusCode(ValidatableResponseOptionsImpl.java:89)
at com.example.demo.PostControllerTest$getPosts$1.invokeSuspend(PostControllerTest.kt:39)
at com.example.demo.PostControllerTest$getPosts$1.invoke(PostControllerTest.kt)
at com.example.demo.PostControllerTest$getPosts$1.invoke(PostControllerTest.kt)
at kotlinx.coroutines.test.TestBuildersKt__TestBuildersKt$runTest$2$1$1.invokeSuspend(TestBuilders.kt:316)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104)
at kotlinx.coroutines.test.TestDispatcher.processEvent$kotlinx_coroutines_test(TestDispatcher.kt:24)
at kotlinx.coroutines.test.TestCoroutineScheduler.tryRunNextTaskUnless$kotlinx_coroutines_test(TestCoroutineScheduler.kt:99)
at kotlinx.coroutines.test.TestBuildersKt__TestBuildersKt$runTest$2$1$workRunner$1.invokeSuspend(TestBuilders.kt:322)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104)
at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:277)
at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:95)
at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:69)
at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:48)
at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
at kotlinx.coroutines.test.TestBuildersJvmKt.createTestResult(TestBuildersJvm.kt:10)
at kotlinx.coroutines.test.TestBuildersKt__TestBuildersKt.runTest-8Mi8wO0(TestBuilders.kt:310)
at kotlinx.coroutines.test.TestBuildersKt.runTest-8Mi8wO0(Unknown Source)
at kotlinx.coroutines.test.TestBuildersKt__TestBuildersKt.runTest-8Mi8wO0(TestBuilders.kt:168)
at kotlinx.coroutines.test.TestBuildersKt.runTest-8Mi8wO0(Unknown Source)
at kotlinx.coroutines.test.TestBuildersKt__TestBuildersKt.runTest-8Mi8wO0$default(TestBuilders.kt:160)
at kotlinx.coroutines.test.TestBuildersKt.runTest-8Mi8wO0$default(Unknown Source)
at com.example.demo.PostControllerTest.getPosts(PostControllerTest.kt:25)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at io.quarkus.test.junit.QuarkusTestExtension.runExtensionMethod(QuarkusTestExtension.java:1018)
at io.quarkus.test.junit.QuarkusTestExtension.interceptTestMethod(QuarkusTestExtension.java:832)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
2024-04-22 20:14:52,397 INFO [io.quarkus] (main) restclient-kotlin-co stopped in 0.236s
Process finished with exit code -1 |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
/cc @geoand (kotlin) |
Beta Was this translation helpful? Give feedback.
-
Quick workaround:
Or
The longer answer is that the conditional dependency feature does not work for the renamed artifacts. |
Beta Was this translation helpful? Give feedback.
-
Use the new naming make it work now. |
Beta Was this translation helpful? Give feedback.
Quick workaround:
quarkus-resteasy-reactive*
toquarkus-rest*
Or
quarkus-resteasy-reactive-kotlin
The longer answer is that the conditional dependency feature does not work for the renamed artifacts.
@aloubyansky I am not sure if this is fixable in any way and if it is not we should definitely document this corner case (cc @gsmet)