![](/img/trans.png)
[英]Optimization of rest-assured and gson at rest test with java with cucumber
[英]Test a particular method with rest-assured in 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 ?
}
}
我猜您正在混淆integration
和unit tests
。 Rest-assured用于集成测试,因此他们测试您的应用服务器是否正确响应了定义的请求。 您实际上将定义一个具有预期答案的.json文件,并将其与实际响应相匹配,以检查应用程序服务器的序列化是否正常工作。
以此为例。
如果您要测试AdRestService
的方法,则可以编写一个通用的单元测试,而无需使用保证放心的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.