Mockito Liferay service testing

血红的双手。 提交于 2019-12-08 09:31:26

问题


I try testing my LocalServiceUtil classes, generated by service builder, with PowerMock but always getting 'null' or '0' from Util's methods.

Test class

@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 class contains

public static java.lang.String getHello() {
            return getService().getHello();
}

and this service working correctly on deployed portlet. What i do wrong?


回答1:


You have forgot to mock the methode:

    @Test
    public void testGetAnswer() throws PortalException, SystemException {
            PowerMockito.mockStatic(EntityLocalServiceUtil.class);
            when(EntityLocalServiceUtil.getHello()).thenReturn("hello"); // <- here
            assertEquals("hello", EntityLocalServiceUtil.getHello());
    }


来源:https://stackoverflow.com/questions/11988715/mockito-liferay-service-testing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!