1
我的要求休息服务:嘲讽restTemplate getForObject
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()'与verify中的参数。另外,你可以发布PriceService的代码吗? – ESala
我想你应该调用'priceServiceMock'而不是'runtimeService' – Ruben