Java REST API GET 测试

huangapple go评论76阅读模式
英文:

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 Java REST API GET 测试

英文:

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 Java REST API GET 测试

答案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.

huangapple
  • 本文由 发表于 2020年10月23日 15:00:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64495359.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定