Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

Mock Configuration Admin

Avatar

Avatar
Level 1
Vabs95
Level 1

Likes

0 likes

Total Posts

5 posts

Correct Reply

0 solutions
View profile

Avatar
Level 1
Vabs95
Level 1

Likes

0 likes

Total Posts

5 posts

Correct Reply

0 solutions
View profile
Vabs95
Level 1

07-10-2019

Hi,

I am writing Unit Test Cases for Sling Models, I have a function that gets Configurations from ConfigurationAdmin.listConfiguration() method. I'm using Junit 5 and Aem Mock for testing. How can I mock "listConfiguration()" method since currently an exception is thrown "java.lang.UnsupportedOperationException";

I tried by using spy and return mocked object whenever this function is called but still I get the same error.

Replies

Avatar

Avatar
Validate 1
MVP
Theo_Pendle
MVP

Likes

238 likes

Total Posts

251 posts

Correct Reply

105 solutions
Top badges earned
Validate 1
Ignite 5
Ignite 3
Ignite 10
Ignite 1
View profile

Avatar
Validate 1
MVP
Theo_Pendle
MVP

Likes

238 likes

Total Posts

251 posts

Correct Reply

105 solutions
Top badges earned
Validate 1
Ignite 5
Ignite 3
Ignite 10
Ignite 1
View profile
Theo_Pendle
MVP

07-10-2019

Can you share your code please? So we don't have to guess

Avatar

Avatar
Give Back
Level 1
bhargavraj
Level 1

Likes

0 likes

Total Posts

2 posts

Correct Reply

0 solutions
Top badges earned
Give Back
View profile

Avatar
Give Back
Level 1
bhargavraj
Level 1

Likes

0 likes

Total Posts

2 posts

Correct Reply

0 solutions
Top badges earned
Give Back
View profile
bhargavraj
Level 1

31-05-2020

bhargavraj_0-1590941131215.png

The ConfigurationAdmin object created throws exception on calling listConfigurations method as documented at https://github.com/apache/sling-org-apache-sling-testing-osgi-mock/blob/master/core/src/main/java/or...

 

How to mock configurations here?

Avatar

Avatar
Give Back
Level 1
henryk87
Level 1

Likes

0 likes

Total Posts

3 posts

Correct Reply

0 solutions
Top badges earned
Give Back
View profile

Avatar
Give Back
Level 1
henryk87
Level 1

Likes

0 likes

Total Posts

3 posts

Correct Reply

0 solutions
Top badges earned
Give Back
View profile
henryk87
Level 1

11-02-2021

Hi @Vabs95 

 

I just ran into this issue as well and created a PR to fix this by providing an implementation for the filtering: https://github.com/apache/sling-org-apache-sling-testing-osgi-mock/pull/7/files

 

Fyi: when an OSGi Mock Service is somehow not doing exactly what is needed / reflects a real world scenario, you can override it by specifying another implementation with a higher service.ranking, so you could have very well created your own implementation and hooked it in there.

 

In case of the MockConfigurationAdmin before the PR I just linked, it is not possible to override exception cases for the listConfigurations-method with Mockito, as it defines no exceptions to be thrown. You can however make a new interface implementation that delegates the method calls to the MockConfigurationAdmin, where you do specify that it throws all the exceptions. 

 

That issue is also fixed in that PR.