有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在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 ?
    }




}

共 (1) 个答案

  1. # 1 楼答案

    我猜你把integrationunit tests搞混了。Rest assured用于集成测试,因此它们测试应用程序服务器是否正确响应定义的请求。你可以定义一个。json文件,您可以将其与实际响应匹配,以检查应用程序服务器的序列化是否正常工作

    this为例

    如果想测试AdRestService中的方法,可以编写一个通用单元测试,不需要使用rest-assured