Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

How to mock SlingSettingsService run modes in Junit5?

Avatar

Avatar
Ignite 1
Level 1
baw_gov
Level 1

Likes

0 likes

Total Posts

12 posts

Correct Reply

0 solutions
Top badges earned
Ignite 1
Give Back
View profile

Avatar
Ignite 1
Level 1
baw_gov
Level 1

Likes

0 likes

Total Posts

12 posts

Correct Reply

0 solutions
Top badges earned
Ignite 1
Give Back
View profile
baw_gov
Level 1

29-03-2021

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.

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

644 likes

Total Posts

605 posts

Correct Reply

239 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

644 likes

Total Posts

605 posts

Correct Reply

239 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
BrianKasingli
MVP

29-03-2021

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

 

Answers (1)

Answers (1)

Avatar

Avatar
Give Back 5
Level 6
Bhuwan_B
Level 6

Likes

132 likes

Total Posts

122 posts

Correct Reply

48 solutions
Top badges earned
Give Back 5
Boost 100
Give Back 3
Affirm 25
Establish
View profile

Avatar
Give Back 5
Level 6
Bhuwan_B
Level 6

Likes

132 likes

Total Posts

122 posts

Correct Reply

48 solutions
Top badges earned
Give Back 5
Boost 100
Give Back 3
Affirm 25
Establish
View profile
Bhuwan_B
Level 6

29-03-2021