在Powermock中有whenNew方法。
我是Jmockit的新手。我正在将我的应用程序从Powermock迁移到Jmockit。
你能告诉我在Jmockit中whenNew的等价物是什么吗?或者如何在Jmockit中实现相同的东西?我真的需要解决这个问题才能继续前进。
场景是new RestTemplate()在一个类的不同方法中被本地使用,...with whenNew,我们可以赋值给mocked resttemplate值,但不能,我正在努力模拟相同的值。有什么建议吗?
发布于 2018-12-28 17:21:00
在您的例子中,我认为您要做的是模拟RestTemplate的一个新实例的构造。
你应该看看JMockit的文档,因为它做得很好,而且很多问题已经在那里得到了回答(在你的例子中是here)。
@Test
public void test(@Mocked RestTemplate restTemplateMock) {
new Expectations() {{
new RestTemplate(); result = restTemplateMock;
restTemplateMock.method(); result = "something";
}};
// Code under test:
new RestTemplate().method(); // will return "something"
}根据你需要传递的参数使用必要的构造函数,我认为这应该会对你有所帮助。
https://stackoverflow.com/questions/53947274
复制相似问题