我尝试使用PowerMock测试由服务生成器生成的LocalServiceUtil类,但始终从Util的方法获取“ null”或“ 0”。
测试班
@RunWith(PowerMockRunner.class)
@PrepareForTest(EntityLocalServiceUtil.class)
public class EntityTest {
@Test
public void testGetAnswer() throws PortalException, SystemException {
PowerMockito.mockStatic(EntityLocalServiceUtil.class);
assertEquals("hello", EntityLocalServiceUtil.getHello());
}
}
实用程序类包含
public static java.lang.String getHello() {
return getService().getHello();
}
并且此服务在已部署的Portlet上正常运行。我做错了什么?
最佳答案
您忘记了模拟方法:
@Test
public void testGetAnswer() throws PortalException, SystemException {
PowerMockito.mockStatic(EntityLocalServiceUtil.class);
when(EntityLocalServiceUtil.getHello()).thenReturn("hello"); // <- here
assertEquals("hello", EntityLocalServiceUtil.getHello());
}