0

我尝试使用 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 上正常工作。我做错了什么?

4

1 回答 1

1

您忘记了模拟方法:

    @Test
    public void testGetAnswer() throws PortalException, SystemException {
            PowerMockito.mockStatic(EntityLocalServiceUtil.class);
            when(EntityLocalServiceUtil.getHello()).thenReturn("hello"); // <- here
            assertEquals("hello", EntityLocalServiceUtil.getHello());
    }
于 2012-08-16T14:09:52.453 回答