Highlighted

JUNIT Test case for OSGI Service

Avatar

Avatar

preethamk694748

Avatar

preethamk694748

preethamk694748

12-11-2018

Hi Team,

Need help in writing junit for below osgi service's activate method and get resourceResolver method.

@Component(

label="xyz",

description = "xyz",

immediate=true,

metatype=true,

name="myservice")

@Service("MyService.class)

public class MyServiceImpl implements MyService

{

String path="";

@Activate

public void activate(Map<String, Object) properties)

{

ConfigurationParameters cfp = ConfigurationParameters.of(properties);

path = cfp.getConfigValue("jcrPath", "defaultPath");

}

public ResourceResolver getResourceResolver()

{

Map<String, Object) param = new HashMap<String,Object>();

param.put(ResourceResolverFactory.SUBSERVICE,SERVICE_IDENTIFIER);

ResourceResolver resolver = null;

resolver = resourceResolverFactory.getServiceResourceResolver(param);

return resolver;

}

public String getPath()

{

return path;

}

Replies

Highlighted

Avatar

Avatar

PuzanovsP

MVP

Avatar

PuzanovsP

MVP

PuzanovsP
MVP

12-11-2018

Dear Sir or Madam,

If you posted valid Java code it would help you writing JUnit test.

With this Java code it's impossible to write a JUnit test, as there is no resourceResolverFactory to mock.

Regards,

Peter

Highlighted

Avatar

Avatar

preethamk694748

Avatar

preethamk694748

preethamk694748

12-11-2018

Sorry i forgot to add,

ResourceResolverFactory at class level, I have updated code below,

@Component(

label="xyz",

description = "xyz",

immediate=true,

metatype=true,

name="myservice")

@Service("MyService.class)

public class MyServiceImpl implements MyService

{

@Reference

ResourceResolverFactory  resourceResolverFactory ;

String SERVICE_IDENTIFIER = "myServiceIdentfier";

String path="";

@Activate

public void activate(Map<String, Object) properties)

{

ConfigurationParameters cfp = ConfigurationParameters.of(properties);

path = cfp.getConfigValue("jcrPath", "defaultPath");

}

public ResourceResolver getResourceResolver()

{

Map<String, Object) param = new HashMap<String,Object>();

param.put(ResourceResolverFactory.SUBSERVICE,SERVICE_IDENTIFIER);

ResourceResolver resolver = null;

resolver = resourceResolverFactory.getServiceResourceResolver(param);

return resolver;

}

public String getPath()

{

return path;

}

Highlighted

Avatar

Avatar

smacdonald2008

Total Posts

(val/1000)?string[".0"]}K

Likes

(val/1000)?string[".0"]}K

Correct Answer

(val/1000)?string[".0"]}K

Avatar

smacdonald2008

Total Posts

(val/1000)?string[".0"]}K

Likes

(val/1000)?string[".0"]}K

Correct Answer

(val/1000)?string[".0"]}K
smacdonald2008

12-11-2018

When you create a Maven Archetype project - a test class is set up for you. Have you looked at modifying that code to meet your needs.  We have many articles too that describe how to use code like this:

Map<String, Object) param = new HashMap<String,Object>();

param.put(ResourceResolverFactory.SUBSERVICE,SERVICE_IDENTIFIER);

ResourceResolver resolver = null;

resolver = resourceResolverFactory.getServiceResourceResolver(param);