首页 > 解决方案 > 在 Java 中放心地测试特定方法

问题描述

我想在一个名为 AdRestService 的类中测试一个特定的方法,该类有一些 @GET、@POST 等。我想从另一个名为 AdRestServiceTest 的测试类中测试它。我想知道如何从 AdRestServiceTest 调用 AdRestService 中的方法以及如何测试返回是否正常。

public class AdRestService {

    @GET
    @Path("{id}")
    @Produces("application/json")
    public Ad get(@PathParam("id") Long adId) {
        return adService.get(adId); // This points to the DB
    }

}

现在放心的测试:

public class AdRestServiceTest {

    @InjectMocks
    private AdRestService adRestService;

    @Test
    public void getAdTest() {
        given().
        when().
            get("website.com/ad").
        then().
            assertThat().
            statusCode(200).
        and().
            contentType(ContentType.JSON).
        and().
            // ???
                // How can I call the method get(@PathParam("id") Long adId) from AdRestService and test if the return is correct ?
    }




}

标签: javatestingrest-assured

解决方案


我猜你是在混淆integrationunit tests。放心用于集成测试,因此它们测试您的应用程序服务器是否正确响应您定义的请求。您实际上会定义一个带有预期答案的 .json 文件,并将其与实际响应相匹配,以检查应用程序服务器的序列化是否正常工作。

以此为例

如果你想测试你AdRestService的 .


推荐阅读