【问题标题】:RestTemplate getForEntity method undefinedRestTemplate getForEntity 方法未定义
【发布时间】:2017-11-02 17:36:03
【问题描述】:

我正在尝试在 Eclipse Luna 中构建一个项目,但在引用 getForEntity 的 Test 类的几行中遇到错误,例如:

verify(mockRestTemplate,times(1)).getForEntity(any(), eq(String.class));

mockRestTemplate 定义为 RestTemplate 类的变量,我已经导入:

import org.springframework.web.client.RestTemplate;

我在 Eclipse 中遇到错误:

方法 getForEntity(Object, Class) 未定义 输入 RestTemplate

此代码不会导致使用同一项目的同事出错,因此我怀疑它是我环境中的本地代码。

非常感谢, 基思。

【问题讨论】:

  • 那么,getForEntity() 的签名是什么?它的第一个参数的类型是什么?是对象吗?
  • 使用 Java 8 编译您的程序或将调用参数化为 anyMockito.<String>any()(或 URI,如果这是您想要的重载)。

标签: java spring


【解决方案1】:

您应该提供明确的参数类型而不是使用any()。尝试根据实际期望更改代码。

verify(mockRestTemplate,times(1)).getForEntity(any(String.class), eq(String.class));

verify(mockRestTemplate,times(1)).getForEntity(any(URI.class), eq(String.class));

【讨论】:

  • 我更改为 any(URI.class) 并且这有效,非常感谢。为什么原始版本只在我的机器上导致错误,而不是在同事的机器上,仍然无法解释,但修改后的代码对我们所有人都有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-17
相关资源
最近更新 更多