Liferay Junit-Mockito Challenge

I am trying to test my liferay portlet plugin code with JUNIT and Mockito. I am currently mocking the implementation of services in order to return data mockup and test functionality.

The problem I'm running into is checking the code that accepts properties like: PropsUtil.get ("someKey") But when I run it as a standalone JUNIT test, PropsUtil does not read any of the property files. Is there a way to make the test read from the liferay properties file (portal * .properties) without changing the source code?

+3
source share
6 answers

I used the following method:

  • My TestClass extends BaseServiceTestCase (available in liferay src)
  • portal-test.properties ( ).
  • .

liferay , spring.

+3

PowerMock mock PropsUtil.get() . --java- .

+2

Properties Props:

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 );
    }
}

@BeforeClass :

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

, portal.properties, PropsUtil.set .

0

InitUtil.init(), , ...

spring, liferay . , maven : liferay SDK maven. , , , spring.configs spring xml ( + spring, ) Init.initWithSpring(); liferay, spring beans, spring.configs. liferay. .

0

:

mockStatic(PropsUtil.class);

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

All Articles