我尝试使用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());
}