Reputation: 584
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 ?
Upvotes: 3
Views: 3877
Reputation: 14959
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" ) );
}
Upvotes: 3
Reputation: 21981
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.
Upvotes: 0
Reputation: 549
You can also mock the call like this:
mockStatic(PropsUtil.class);
when(
PropsUtil.get(PropsKeys.SOCIAL_ACTIVITY_COUNTER_PERIOD_LENGTH)
).thenReturn("1");
Upvotes: 0
Reputation: 584
I used the following method :
In this case, liferay loads all the properties as well as does the spring initializations.
Upvotes: 3
Reputation: 11775
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..
Upvotes: 2
Reputation: 18170
Unless you're testing that values are actually set in portal.properties, just call PropsUtil.set in your test.
Upvotes: 0