JUNIT Test case for OSGI Service

Avatar

Avatar
Validate 1
Level 3
preethamk694748
Level 3

Likes

10 likes

Total Posts

32 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 10
Boost 1
View profile

Avatar
Validate 1
Level 3
preethamk694748
Level 3

Likes

10 likes

Total Posts

32 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 10
Boost 1
View profile
preethamk694748
Level 3

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

Avatar

Avatar
Validate 25
MVP
PuzanovsP
MVP

Likes

140 likes

Total Posts

543 posts

Correct reply

165 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Contributor 2
Ignite 10
View profile

Avatar
Validate 25
MVP
PuzanovsP
MVP

Likes

140 likes

Total Posts

543 posts

Correct reply

165 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Contributor 2
Ignite 10
View profile
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

Avatar

Avatar
Validate 1
Level 3
preethamk694748
Level 3

Likes

10 likes

Total Posts

32 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 10
Boost 1
View profile

Avatar
Validate 1
Level 3
preethamk694748
Level 3

Likes

10 likes

Total Posts

32 posts

Correct reply

0 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 10
Boost 1
View profile
preethamk694748
Level 3

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;

}

Avatar

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile
smacdonald2008
Level 10

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