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 SlingSettingsService run modes in Junit5?

Avatar

Level 2

I am trying to Mock SlingSettingsService with run modes in Junit5.

 

In my h.java file, in Activate method I have.

srun = slingSettingsService.getRunModes().contains("runMode");

How do I mock this in my htest.java file? This is for Junit5.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@baw_gov,

you can do something like this:

 

@ExtendWith(AemContextExtension.class)
public class ExampleTest {

    private final AemContext context = new AemContext();

    Set<String> mockRunModes = new TreeSet<String>();

    @Mock
    private SlingSettingsService slingSettingsService;
    
    @Before
    public void before() {
        when(slingSettingsService.getRunModes()).thenReturn(mockRunModes);
    }

    @Test
    public void testSomething() {
        mockRunModes.add("publish");
        Resource resource = context.resourceResolver().getResource("/content/sample/en");
        Page page = resource.adaptTo(MyClass.class);
        // further testing
    }
}

 

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

@baw_gov,

you can do something like this:

 

@ExtendWith(AemContextExtension.class)
public class ExampleTest {

    private final AemContext context = new AemContext();

    Set<String> mockRunModes = new TreeSet<String>();

    @Mock
    private SlingSettingsService slingSettingsService;
    
    @Before
    public void before() {
        when(slingSettingsService.getRunModes()).thenReturn(mockRunModes);
    }

    @Test
    public void testSomething() {
        mockRunModes.add("publish");
        Resource resource = context.resourceResolver().getResource("/content/sample/en");
        Page page = resource.adaptTo(MyClass.class);
        // further testing
    }
}