开发者

Liferay Junit-Mockito testing

开发者 https://www.devze.com 2023-02-15 02:33 出处:网络
I am trying to test my liferay portlet plugin code using JUNIT and Mockito. Currently I am mocking the service implementations to return mock data and test the functionalities.

I am trying to test my liferay portlet plugin code using JUNIT and Mockito. Currently I am mocking the service implementations to return mock data and test the functionalities.

The problem I 开发者_如何学运维am facing is, I need to test some code which takes properties as : PropsUtil.get("someKey") But when i run it as a standalone JUNIT test, PropsUtil is not reading from any of the properties file. Is there any way I can make the test read from the liferay properties (portal*.properties) file without changing the source code ?


I used the following method :

  • My TestClass extends BaseServiceTestCase (available in liferay src)
  • Keep portal-test.properties inside test folder (with the test values).
  • Run the test case.

In this case, liferay loads all the properties as well as does the spring initializations.


As the last resort you could use PowerMock and mock PropsUtil.get() method call. Eventually it's a plain-old-java-singleton and code with singletons is not that easy to test..


You can create a Properties based implementation of the Props interface:

private static class MockProps implements Props {
    private Properties properties = new Properties();

    MockProps addProperty( String key, String value ) {
        properties.setProperty( key, value );
        return this;
    }

    @Override
    public boolean contains( String key ) {
        return properties.containsKey( key );
    }

    @Override
    public String get( String key ) {
        return properties.getProperty( key );
    }

    @Override
    public String get( String key, Filter filter ) {
        throw new UnsupportedOperationException( "not needed by mock" );
    }

    @Override
    public String[] getArray( String key ) {
        throw new UnsupportedOperationException( "not needed by mock" );
    }

    @Override
    public String[] getArray( String key, Filter filter ) {
        throw new UnsupportedOperationException( "not needed by mock" );
    }

    @Override
    public Properties getProperties() {
        return properties;
    }

    @Override
    public Properties getProperties( String prefix, boolean removePrefix ) {
        return PropertiesUtil.getProperties( properties, prefix, removePrefix );
    }
}

Then use an @BeforeClass to configure it:

@BeforeClass
public static void init() {
    PropsUtil.setProps( new MockProps()
            .addProperty( "key1", "silly" )
            .addProperty( "key2", "silly again" ) );
}


Unless you're testing that values are actually set in portal.properties, just call PropsUtil.set in your test.


You can also mock the call like this:

mockStatic(PropsUtil.class);

when(
  PropsUtil.get(PropsKeys.SOCIAL_ACTIVITY_COUNTER_PERIOD_LENGTH)
).thenReturn("1");


you need to call InitUtil.init() which initializes the basic infrastructure, properties including ...

If you wanted to go further and boot up even the spring infrastructure, you'd need to have liferay libraries on classpath. I'm explaining how to do that in maven environment in this blog post : how to use liferay third-party libraries in maven plugin SDK. If you do so, then all you need to do is to setup spring.configs with portal spring xml definitions (infrastructure ones + those with spring services that you need to use) and call Init.initWithSpring(); that takes care of booting up liferay portal and it uses those spring beans that you mix up in spring.configs. You also would need to modify liferay properties a little. But it really depends on the use case.

0

精彩评论

暂无评论...
验证码 换一张
取 消