Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Junit5

Avatar

Level 1

Hi team,

 

I am new to AEM and JUNIT 5, Below is my sling model... Can someone help me write Test class for below Sling Model please:

 

@Slf4j

@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)

public class ExpFragmentPathGetter {

 

@inject

private String resourceType;

 

@inject

private String path;

 

@SlingObject

private ResourceResolver resourceResolver;

 

public String getExpFragmentPath() {

if (StringUtils.isBlank(this.path) || StringUtils.isBlank(this.resourceType)) {

return null;

}

Resource source = resourceResolver.getResource(this.path);

if (Resources.isNonExistingResource(source)) {

// log.error("\nResource not found with this path: {}", path);

return null;

}

Resource mainResource = ModelsHelper.getChildrenResource(source, this.resourceType);

if (Resources.isNonExistingResource(mainResource)) {

//log.error("\nResource type not found: {}", resourceType);

return null;

}

return mainResource.getPath();

}

 

}

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @ktnr ,

Sure, here's an example JUnit 5 test class for your Sling Model:

 

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.models.factory.ModelFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

import io.wcm.testing.mock.aem.junit5.AemContext;
import io.wcm.testing.mock.aem.junit5.AemContextExtension;

@ExtendWith({AemContextExtension.class, MockitoExtension.class})
@MockitoSettings(strictness = Strictness.LENIENT)
class ExpFragmentPathGetterTest {

private final AemContext context = new AemContext();

@Mock
private ResourceResolver resourceResolver;

@Mock
private ModelFactory modelFactory;

private ExpFragmentPathGetter slingModel;

@BeforeEach
void setUp() {
context.registerService(ResourceResolverFactory.class, mock(ResourceResolverFactory.class));
context.registerService(ModelFactory.class, modelFactory);
context.registerInjectActivateService(new ExpFragmentPathGetter());

slingModel = context.request().adaptTo(ExpFragmentPathGetter.class);
}

@Test
void testGetExpFragmentPath() {
// Set up mock resources
Resource source = mock(Resource.class);
Resource mainResource = mock(Resource.class);

// Set up mock behavior
when(resourceResolver.getResource("/path/to/source")).thenReturn(source);
when(source.getChild("resourceType")).thenReturn(mainResource);
when(mainResource.getPath()).thenReturn("/path/to/mainResource");

// Set up sling model properties
slingModel.path = "/path/to/source";
slingModel.resourceType = "resourceType";
slingModel.resourceResolver = resourceResolver;

// Test the method
String result = slingModel.getExpFragmentPath();
assertEquals("/path/to/mainResource", result);
}

}
```

This test class uses the AEM Mocks framework to set up a mock AEM context and mock resources for testing the Sling Model. The `@BeforeEach` method sets up the Sling Model and injects the necessary dependencies. The `@Test` method tests the `getExpFragmentPath()` method by setting up mock behavior for the `ResourceResolver` and calling the method with the appropriate parameters. The `assertEquals()` method checks that the expected result is returned.

Note that this is just an example and you may need to modify the test class to fit your specific use case. It's also recommended to consult with experienced AEM developers or solution architects to ensure that your test class is implemented correctly and follows best practices.

View solution in original post

2 Replies

Avatar

Community Advisor

@ktnr 

 

Since you are new, I would recommend you please give it a try yourself.

 

Sharing few links 

https://www.youtube.com/watch?v=ZWsHhTI8h6Y&ab_channel=AEMGEEKS

https://sourcedcode.com/blog/aem/junit/aem-junit5-with-io-wcm-testing-mock-aem-junit5-aemcontextexte... 

 

Please use WCM Content IO AemContext for the test cases as much as possible.

 

If you have any specific queries, please let us know.


Aanchal Sikka

Avatar

Correct answer by
Community Advisor

Hi @ktnr ,

Sure, here's an example JUnit 5 test class for your Sling Model:

 

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.models.factory.ModelFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

import io.wcm.testing.mock.aem.junit5.AemContext;
import io.wcm.testing.mock.aem.junit5.AemContextExtension;

@ExtendWith({AemContextExtension.class, MockitoExtension.class})
@MockitoSettings(strictness = Strictness.LENIENT)
class ExpFragmentPathGetterTest {

private final AemContext context = new AemContext();

@Mock
private ResourceResolver resourceResolver;

@Mock
private ModelFactory modelFactory;

private ExpFragmentPathGetter slingModel;

@BeforeEach
void setUp() {
context.registerService(ResourceResolverFactory.class, mock(ResourceResolverFactory.class));
context.registerService(ModelFactory.class, modelFactory);
context.registerInjectActivateService(new ExpFragmentPathGetter());

slingModel = context.request().adaptTo(ExpFragmentPathGetter.class);
}

@Test
void testGetExpFragmentPath() {
// Set up mock resources
Resource source = mock(Resource.class);
Resource mainResource = mock(Resource.class);

// Set up mock behavior
when(resourceResolver.getResource("/path/to/source")).thenReturn(source);
when(source.getChild("resourceType")).thenReturn(mainResource);
when(mainResource.getPath()).thenReturn("/path/to/mainResource");

// Set up sling model properties
slingModel.path = "/path/to/source";
slingModel.resourceType = "resourceType";
slingModel.resourceResolver = resourceResolver;

// Test the method
String result = slingModel.getExpFragmentPath();
assertEquals("/path/to/mainResource", result);
}

}
```

This test class uses the AEM Mocks framework to set up a mock AEM context and mock resources for testing the Sling Model. The `@BeforeEach` method sets up the Sling Model and injects the necessary dependencies. The `@Test` method tests the `getExpFragmentPath()` method by setting up mock behavior for the `ResourceResolver` and calling the method with the appropriate parameters. The `assertEquals()` method checks that the expected result is returned.

Note that this is just an example and you may need to modify the test class to fit your specific use case. It's also recommended to consult with experienced AEM developers or solution architects to ensure that your test class is implemented correctly and follows best practices.