【发布时间】:2015-10-20 16:46:08
【问题描述】:
我的休息服务请求:
Price[] prices = restTemplate.getForObject("https://sbk02.test.sparebank1.no/sbk/rest/poc1/prices", Price[].class);
我正在尝试模拟它,但与模拟的交互为零。我的测试代码是:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={ "classpath:/spring/engine.xml", "classpath:/spring/beans.xml"})
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class, DirtiesMocksTestContextListener.class})
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class LabbOgLineProcessTest{
@InjectMocks
private PriceService priceServiceMock;
@Mock
private RestTemplate template;
@Before
public void initMocks() throws Exception {
MockitoAnnotations.initMocks(this);
}
@Test
public void complete_AllTasks_success() throws Exception{
when(template.getForObject(eq(PRICES_NAMESPACE), eq(Price[].class))).thenReturn(prices);
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("process");
verify(template, times(1)).getForObject(PRICES_NAMESPACE, Price[].class);
}
}
【问题讨论】:
-
尝试模拟接口
RestOperations而不是类RestTemplate。另外,您确定在 PriceService 中正确注入了模板吗?此外,尝试使用eq(...)或any()与验证的参数。另外,能把PriceService的代码贴出来吗? -
我猜你应该打电话给
priceServiceMock而不是runtimeService
标签: java spring junit mockito resttemplate