Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

Mock Configuration Admin

Avatar

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

Avatar

Level 10

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

Avatar

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?

Avatar

Level 1

Hi @Vabs95-ttn 

 

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.