내 커뮤니티 업적 표시줄을 확대합니다.

Submissions are now open for the 2026 Adobe Experience Maker Awards.

Mark Solution

활동이 없어 이 대화는 잠겼습니다. 새 게시물을 작성해 주세요.

해결됨

How to set runmodes for the sling servlet unit test - Junit5?

Avatar

Level 2

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 

1 채택된 해결책 개

Avatar

정확한 답변 작성자:
Community Advisor

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!

 

 

원본 게시물의 솔루션 보기

3 답변 개

Avatar

정확한 답변 작성자:
Community Advisor

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!

 

 

Avatar

Level 2
Your code didn't work for me. I was getting Nullpointer on context.registerService(SlingSettingsService.class,settingsService);

Avatar

Level 2

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