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
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Mock Configuration Admin

Vabs95
Level 1
Level 1

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.

3 Replies
Theo_Pendle
Community Advisor
Community Advisor

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

bhargavraj
Level 1
Level 1

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?

henryk87
Level 1
Level 1

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.