Mockito Liferay服务测试
问题描述:
我尝试使用PowerMock测试由服务生成器生成的LocalServiceUtil类,但始终从Util的方法中获取"null"或"0".
I try testing my LocalServiceUtil classes, generated by service builder, with PowerMock but always getting 'null' or '0' from Util's methods.
测试班
@RunWith(PowerMockRunner.class)
@PrepareForTest(EntityLocalServiceUtil.class)
public class EntityTest {
@Test
public void testGetAnswer() throws PortalException, SystemException {
PowerMockito.mockStatic(EntityLocalServiceUtil.class);
assertEquals("hello", EntityLocalServiceUtil.getHello());
}
}
Util类包含
public static java.lang.String getHello() {
return getService().getHello();
}
,并且此服务在已部署的Portlet上正常运行.我做错了什么?
and this service working correctly on deployed portlet. What i do wrong?
答
您忘记了模拟方法:
@Test
public void testGetAnswer() throws PortalException, SystemException {
PowerMockito.mockStatic(EntityLocalServiceUtil.class);
when(EntityLocalServiceUtil.getHello()).thenReturn("hello"); // <- here
assertEquals("hello", EntityLocalServiceUtil.getHello());
}