Solved! Go to Solution.
Views
Replies
Total Likes
Hi @keshava219 ,
I hope this reference snippet will work.
@Before
public void setUp() throws RepositoryException {
when(request.getResource()).thenReturn(resource);
when(resource.getResourceResolver()).thenReturn(resourceResolver);
when(resourceResolver.adaptTo(TagManager.class)).thenReturn(tagManager);
when(tagManager.resolve("sampleTagPath")).thenReturn(tag);
when(tag.getTitle()).thenReturn("Sample Tag Title");
// Mocking ValueMap
Map<String, Object> map = new HashMap<>();
map.put("cq:division", "sampleTagPath");
map.put("cq:businessArea", "sampleBusinessTagPath");
when(properties.get("cq:division", String.class)).thenReturn("sampleTagPath");
when(properties.get("cq:businessArea", String.class)).thenReturn("sampleBusinessTagPath");
pageModel = new PageModel();
pageModel.currentPage = resource.adaptTo(com.day.cq.wcm.api.Page.class);
pageModel.properties = properties;
pageModel.pageProperties = mock(com.day.cq.wcm.api.Page.class).adaptTo(ValueMap.class);
}
@Test
public void testGetContentLanguage() {
String contentLang = pageModel.getContentLanguage();
assertEquals("Sample Tag Title", contentLang);
}
I have add one sample test case for getContentLanguage() method
Hi @keshava219 ,
I hope this reference snippet will work.
@Before
public void setUp() throws RepositoryException {
when(request.getResource()).thenReturn(resource);
when(resource.getResourceResolver()).thenReturn(resourceResolver);
when(resourceResolver.adaptTo(TagManager.class)).thenReturn(tagManager);
when(tagManager.resolve("sampleTagPath")).thenReturn(tag);
when(tag.getTitle()).thenReturn("Sample Tag Title");
// Mocking ValueMap
Map<String, Object> map = new HashMap<>();
map.put("cq:division", "sampleTagPath");
map.put("cq:businessArea", "sampleBusinessTagPath");
when(properties.get("cq:division", String.class)).thenReturn("sampleTagPath");
when(properties.get("cq:businessArea", String.class)).thenReturn("sampleBusinessTagPath");
pageModel = new PageModel();
pageModel.currentPage = resource.adaptTo(com.day.cq.wcm.api.Page.class);
pageModel.properties = properties;
pageModel.pageProperties = mock(com.day.cq.wcm.api.Page.class).adaptTo(ValueMap.class);
}
@Test
public void testGetContentLanguage() {
String contentLang = pageModel.getContentLanguage();
assertEquals("Sample Tag Title", contentLang);
}
I have add one sample test case for getContentLanguage() method
To achieve 100% code coverage in your JUnit test case, you'll need to create test scenarios that cover various branches(if else), conditions, and methods in your PageModel
class. Here's a basic example of a JUnit test case using the Mockito framework to mock the necessary dependencies:
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
import java.util.HashMap;
import java.util.Map;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class PageModelTest {
@Mock
private Page currentPage;
@Mock
private ValueMap properties;
@Mock
private InheritanceValueMap pageProperties;
@Mock
private SlingHttpServletRequest slingHttpServletRequest;
@InjectMocks
private PageModel pageModel;
@Before
public void setUp() {
when(currentPage.getContentResource()).thenReturn(mock(Resource.class));
when(currentPage.getContentResource().getResourceResolver()).thenReturn(mock(ResourceResolver.class));
}
@test
public void testGetTagTitlesMultiTagInherited() {
// Mocking values for properties.get(propertyName, Value[].class)
Value[] values = new Value[]{mock(Value.class)};
when(pageProperties.getInherited("propertyName", Value[].class)).thenReturn(values);
// Mocking the getTagTitle method
when(pageModel.getTagTitle(any(ResourceResolver.class), anyString())).thenReturn("MockedTagTitle");
// Test the method
String result = pageModel.getTagTitles("propertyName", true, true);
// Assert the result
assertEquals("MockedTagTitle", result);
}
// Add more test cases for different scenarios and methods
@test
public void testGetContentLanguage() {
// Mocking the getTagTitles method
when(pageModel.getTagTitles("cq:contentLang", false, false)).thenReturn("MockedContentLanguage");
// Test the method
String result = pageModel.getContentLanguage();
// Assert the result
assertEquals("MockedContentLanguage", result);
}
@test
public void testGetCountry() {
// Mocking the getTagTitles method
when(pageModel.getTagTitles("cq:country", true, true)).thenReturn("MockedCountry");
// Test the method
String result = pageModel.getCountry();
// Assert the result
assertEquals("MockedCountry", result);
}
}
This is a basic example, and you may need to customize it based on your specific use cases. Ensure that you cover various conditions and edge cases in your test cases to achieve comprehensive code coverage.
In addition to @arunpatidar and @MayurSatav responses, I think the most important thing to understand is that achieving 100% coverage is not always useful, as it often includes covering default behaviors such as testing "getter" and "setter" methods. However, if you truly need to achieve a 100% score, you just need to use the Jacoco plugin to see the percentage of coverage you currently have, then build new test classes and recheck until you reach 100%.
You can check out these articles on how to set up and check your code coverage percentage
https://medium.com/@karlrombauts/setting-up-unit-testing-for-java-in-vs-code-with-maven-3dc75579122f
Hope this helps
Views
Likes
Replies
Views
Likes
Replies