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 set runmodes for the sling servlet unit test - Junit5?

Avatar

Avatar
Boost 1
Level 1
EugeneF1
Level 1

Like

1 like

Total Posts

3 posts

Correct Reply

0 solutions
Top badges earned
Boost 1
View profile

Avatar
Boost 1
Level 1
EugeneF1
Level 1

Like

1 like

Total Posts

3 posts

Correct Reply

0 solutions
Top badges earned
Boost 1
View profile
EugeneF1
Level 1

22-04-2021

I'm using SlingSettingsService to get runmides in the sling servlet. 

 

Any idea how to set runmodes in the test file uisng Junit5 so the servlet could read them when you run doGet(req, res) test?

 

Thanks,

Eugene 

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Establish
MVP
Ravi_Pampana
MVP

Likes

183 likes

Total Posts

233 posts

Correct Reply

70 solutions
Top badges earned
Establish
Contributor
Shape 1
Ignite 5
Ignite 3
View profile

Avatar
Establish
MVP
Ravi_Pampana
MVP

Likes

183 likes

Total Posts

233 posts

Correct Reply

70 solutions
Top badges earned
Establish
Contributor
Shape 1
Ignite 5
Ignite 3
View profile
Ravi_Pampana
MVP

22-04-2021

Hi,

 

We can use below code to set the run modes

 

@Mock

private SlingSettingsService settingsService;

 

Register the service

context.registerService(SlingSettingsService.class, settingsService);

 

Then set the runmodes required for the test cases as below

 

        Set<String> runModes = new HashSet<>();

        runModes.add("author");

        when(settingsService.getRunModes()).thenReturn(runModes);

 

Hope this helps!

 

 

Answers (1)

Answers (1)

Avatar

Avatar
Boost 1
Level 1
EugeneF1
Level 1

Like

1 like

Total Posts

3 posts

Correct Reply

0 solutions
Top badges earned
Boost 1
View profile

Avatar
Boost 1
Level 1
EugeneF1
Level 1

Like

1 like

Total Posts

3 posts

Correct Reply

0 solutions
Top badges earned
Boost 1
View profile
EugeneF1
Level 1

23-04-2021

I've come up with the working solution 🙂

Runmodes are passed to the servlet. Here's the example:

 

private DataUpdate servlet;
@Mock
private MockSlingHttpServletRequest req;
@Mock
private SlingSettingsService settingsService;
@Mock
private SlingSettingsService settingsService;

@BeforeEach
void setUp() {
servlet = context.registerInjectActivateService(new DataUpdate());
}
@Test
void doGet() throws ServletException, IOException {
context.runMode("author");
req = context.request();
res = context.response();
servlet.doGet(req, res);
}