Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

How to mock context configuration class

Avatar

Community Advisor

HI,

I am trying to write Junit for a java class which has context aware configuration code something like below:
Main class logic:

ConfigurationBuilder configBuilder =resource.adaptTo(ConfigurationBuilder.class);
  SimpleConfig simpleConfig = configBuilder.name("feature").as(SimpleConfig.class);

 

but smipleConfig value is showing as "null" when I debug it in Junit.

Junit class logic, what I am trying to do:

package org.apache.sling.testing.mock.caconfig;

import static org.apache.sling.testing.mock.caconfig.ContextPlugins.CACONFIG;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.caconfig.ConfigurationBuilder;
import org.apache.sling.testing.mock.caconfig.example.SimpleConfig;
import org.apache.sling.testing.mock.sling.junit.SlingContext;
import org.apache.sling.testing.mock.sling.junit.SlingContextBuilder;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

@SuppressWarnings("null")
public class ContextPluginsTest {

    private static final String CONFIG_NAME = "testConfig";

    @Rule
    public SlingContext context = new SlingContextBuilder()
            .plugin(CACONFIG)
            .build();

    private Resource contextResource;

    @Before
    public void setUp() {
        context.create().resource("/content/site1", "sling:configRef", "/conf/site1");
        contextResource = context.create().resource("/content/site1/page1");

        // register configuration annotation class
        MockContextAwareConfig.registerAnnotationClasses(context, SimpleConfig.class);

        // write config
        MockContextAwareConfig.writeConfiguration(context, contextResource.getPath(), SimpleConfig.class,
                        "stringParam", "value1",
                        "intParam", 123,
                        "boolParam", true);
    }
 
}



1 Accepted Solution

Avatar

Correct answer by
Community Advisor

There are three things in your case:

1. Provide current resource.   Missing in your code

context.currentResource

2. Provide a resource for config.       

context.create().resource

3. Config values

MockContextAwareConfig.writeConfiguration

 

View solution in original post

2 Replies

Avatar

Community Advisor

Hi,

We are able to mock CA Config like below

 

import static io.wcm.testing.mock.wcmio.caconfig.ContextPlugins.WCMIO_CACONFIG;

import static org.apache.sling.testing.mock.caconfig.ContextPlugins.CACONFIG;

import org.apache.sling.testing.mock.caconfig.MockContextAwareConfig;

private AemContext aemContext = new AemContextBuilder().plugin(CACONFIG).plugin(WCMIO_CACONFIG)

            .resourceResolverType(ResourceResolverType.JCR_MOCK).build();

 

in setup method:

aemContext.create().resource("/content/<your-project>/<locale>", "sling:configRef", "/conf/<your-project>");

 

Add all your properties here. LocaleConfig is the java class which reads CA config in backend 

 

MockContextAwareConfig.writeConfiguration(aemContext,
"/content/<your-project>/<locale>", LocaleConfig.class, "currencySymbol", "$");

 

Hope this helps!

Avatar

Correct answer by
Community Advisor

There are three things in your case:

1. Provide current resource.   Missing in your code

context.currentResource

2. Provide a resource for config.       

context.create().resource

3. Config values

MockContextAwareConfig.writeConfiguration