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 ?
}
}
解决方案
我猜你是在混淆integration
和unit tests
。放心用于集成测试,因此它们测试您的应用程序服务器是否正确响应您定义的请求。您实际上会定义一个带有预期答案的 .json 文件,并将其与实际响应相匹配,以检查应用程序服务器的序列化是否正常工作。
以此为例。
如果你想测试你AdRestService
的 .
推荐阅读
- swift - 没有在 UITableViewController 中调用 DidSelectRowAt
- c# - 将 PowerPoint 中的对象粘贴到 C# 应用程序中
- c# - 使用 * 的 AssemblyVersion 失败并出现错误“通配符,与确定性不兼容?”
- excel - 将 VBA 更改为自动运行或在移动选项卡时运行
- css - chrome中的Sass不断刷新失败
- c# - .Net 核心字符串哈希使用 SHA256
- xml - 经典的 asp 创建 xml 属性在第二个标签上重复
- java - Web 服务不接受在 Glassfish 上指定命名空间的 xml 元素
- typescript - 使用 @vue/cli-plugin-unit-mocha、TypeScript 和 SFC 为 VueJS 项目生成纽约市覆盖率报告
- r - 在R中的同一个图中排列许多图