英文:
Java REST API GET test
问题
im wondering how to test GET given below. I don't have much experience with testing so would be glad if some 1 could show me proper approach or comment what should i do better.
@Path("/some")
public class SomeApi {
@Inject
SomeLogic someLogic;
@GET
@Produces({"application/json;charset=utf-8","application/json"})
@RolesAllowed("ek_external")
public Response getSome(@QueryParam("id") Long id, @QueryParam("name") String name, @Min(0) @DefaultValue("0") @QueryParam("offset") Integer offset, @Min(1) @Max(50) @DefaultValue("20") @QueryParam("limit") Integer limit, @Context SecurityContext securityContext) {
return someLogic.getSome(id, name, offset, limit, securityContext);
}
}
This is my GET. Im not sure how to handle all these QueryParams and annotated args.
Im trying something like this
@QuarkusTest
public class SomeApiTest {
@Test
public void testGetSome() {
given()
.when().get("/some")
.then()
.statusCode(200)
.body()
}
}
i ll be glad for showing me which way to go
英文:
im wondering how to test GET given below. I don't have much experience with testing so would be glad if some 1 could show me proper approach or comment what should i do better.
@Path("/some")
public class SomeApi {
@Inject
SomeLogic someLogic;
@GET
@Produces({"application/json;charset=utf-8","application/json"})
@RolesAllowed("ek_external")
public Response getSome(@QueryParam("id") Long id, @QueryParam("name") String name, @Min(0) @DefaultValue("0") @QueryParam("offset") Integer offset, @Min(1) @Max(50) @DefaultValue("20") @QueryParam("limit") Integer limit, @Context SecurityContext securityContext) {
return someLogic.getSome(id, name, offset, limit, securityContext);
}
}
This is my GET. Im not sure how to handle all these QueryParams and annotated args.
Im trying something like this
@QuarkusTest
public class SomeApiTest {
@Test
public void testGetSome() {
given()
.when().get("/some")
.then()
.statusCode(200)
.body()
}
}
i ll be glad for showing me which way to go
答案1
得分: 2
文档中的示例在这里:https://quarkus.io/guides/getting-started-testing#recap-of-http-based-testing-in-jvm-mode 表明唯一可能缺失的是设置主体... ...(200).body(is(someBody))
。
这里提供的示例:https://quarkus.io/guides/getting-started-testing#restassured 也看起来相关。
还要确保提供 @TestConfiguration
,这样当您对 SomeLogic
类进行 @Inject
时,它不会是 null
。
英文:
The example in the documentation here: https://quarkus.io/guides/getting-started-testing#recap-of-http-based-testing-in-jvm-mode suggests the only thing missing might be setting the body... ...(200).body(is(someBody))
.
The example given here: https://quarkus.io/guides/getting-started-testing#restassured also looks relevant.
Also ensure you provide @TestConfiguration
so that when you @Inject
the class for SomeLogic
, it is not null
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论