Mock Configuration Admin | Community
Skip to main content
New Participant
Oct 7, 2019 9:59 AM UTC

Mock Configuration Admin

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

3 replies

New Participant
Feb 11, 2021 7:23 PM UTC

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. 

Translate
New Participant
May 31, 2020 4:08 PM UTC

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/org/apache/sling/testing/mock/osgi/MockConfigurationAdmin.java

 

How to mock configurations here?

Translate
Theo_Pendle
Level 8
Oct 7, 2019 2:55 PM UTC

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

Translate