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