How to mock SlingSettingsService run modes in Junit5? | Community
Skip to main content
Level 2
March 29, 2021
Solved

How to mock SlingSettingsService run modes in Junit5?

  • March 29, 2021
  • 2 replies
  • 1966 views

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.

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

@gbaweja,

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 } }

 

2 replies

Bhuwan_B
Community Advisor
Community Advisor
March 29, 2021
BrianKasingli
Community Advisor and Adobe Champion
BrianKasingliCommunity Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
March 30, 2021

@gbaweja,

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 } }