如何模拟Java中特定方法的响应?
我有这样的课程
public class Foo {
public String getData(String id) {
JsonObject json = this.somePrivateMethod(id);
String d = // some manipulation
return d;
}
private JsonObject somePrivateMethod(String id) {
}
}
,我正在尝试编写getData
的单元测试 现在,我想用两个jsons模拟此SomePrivateMethod
goodJson = {1: 2}, badJson = {"foo" : "bar"}
如何在Java中模拟该私人方法。
I have a class like this
public class Foo {
public String getData(String id) {
JsonObject json = this.somePrivateMethod(id);
String d = // some manipulation
return d;
}
private JsonObject somePrivateMethod(String id) {
}
}
I am trying to write the unit test for getData
Now, I want to mock this somePrivateMethod
with two jsons
goodJson = {1: 2}, badJson = {"foo" : "bar"}
How do I mock that private method in java.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用Mockito之类的框架来实现模拟。
因此,当您的测试案例运行以及遇到getData()方法时,它会从the()方法响应JSON对象
You can use frameworks like Mockito to achieve the mocking.
So when your test case runs and when it encounters getData() method then it responses the json object from the then() method