Mock Configuration Admin

Avatar

Avatar

Vabs95

Avatar

Vabs95

Vabs95

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

Theo_Pendle

MVP

Avatar

Theo_Pendle

MVP

Theo_Pendle
MVP

07-10-2019

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

Avatar

Avatar

bhargavraj

Avatar

bhargavraj

bhargavraj

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

henryk87

Avatar

henryk87

henryk87

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.