How to write junit for below code | Community
Skip to main content
djohn98390536
Level 4
August 19, 2024
Solved

How to write junit for below code

  • August 19, 2024
  • 2 replies
  • 672 views

ValueMap vm = assetResource.getValueMap();
String version = vm.get("jcr:lastModifiedVersion", String.class);
boolean isNewAsset = vm.containsKey("newAsset");
if (!isNewAsset) {
Resource rr= resourceResolver.getResource(path);
ModifiableValueMap mvm = rr.adaptTo(ModifiableValueMap.class);
mvm.put(newversion,version + 1));

} else if (isNewAsset) {
Resource rr= resourceResolver.getResource(path);
ModifiableValueMap mvm= rr.adaptTo(ModifiableValueMap.class);
mvm.put(newversion,version + 2));
}

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by konstantyn_diachenko

Hi @djohn98390536,

 

if you are interesting in the AEM Unit test, you can use my code below. However, I would suggest you to refactor your piece of code firstly, because it could lead to NullPointerException.

Refactored code (ResourceResolver#getResource, Resource#adaptTo can return null):

import org.apache.sling.api.resource.ModifiableValueMap; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.api.resource.ValueMap; import java.util.Optional; public class Foo { public void barNew(Resource assetResource, String newversion, String path) { ResourceResolver resourceResolver = assetResource.getResourceResolver(); ValueMap vm = assetResource.getValueMap(); String version = vm.get("jcr:lastModifiedVersion", String.class); boolean isNewAsset = vm.containsKey("newAsset"); String newVersion = isNewAsset ? version + 2 : version + 1; Optional.ofNullable(resourceResolver.getResource(path)) .map(resource -> resource.adaptTo(ModifiableValueMap.class)) .ifPresent(mvm -> mvm.put(newversion, newVersion)); } }

AEM Unit test:

import com.day.cq.dam.api.Asset; import io.wcm.testing.mock.aem.junit5.AemContext; import io.wcm.testing.mock.aem.junit5.AemContextExtension; import org.apache.sling.api.resource.ModifiableValueMap; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ValueMap; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import java.io.ByteArrayInputStream; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; @ExtendWith(AemContextExtension.class) class FooTest { private AemContext aemContext = new AemContext(); private Foo foo; private Resource assetResource; @BeforeEach void setUp() { foo = new Foo(); Asset asset = aemContext.create().asset("/conten/dam/foo.json", new ByteArrayInputStream("{}".getBytes()), "application/json"); assetResource = asset.adaptTo(Resource.class); } @Test void testBar_notNewAsset() { ModifiableValueMap modifiableValueMap = assetResource.adaptTo(ModifiableValueMap.class); modifiableValueMap.put("jcr:lastModifiedVersion", "123"); String path = "/conten/dam/foo.json"; foo.barNew(assetResource, "new-version-property", path); ValueMap valueMap = aemContext.resourceResolver().getResource(path).getValueMap(); assertEquals("1231", valueMap.get("new-version-property")); } @Test void testBar_newAsset() { ModifiableValueMap modifiableValueMap = assetResource.adaptTo(ModifiableValueMap.class); modifiableValueMap.put("jcr:lastModifiedVersion", "123"); modifiableValueMap.put("newAsset", true); String path = "/conten/dam/foo.json"; foo.barNew(assetResource, "new-version-property", path); ValueMap valueMap = aemContext.resourceResolver().getResource(path).getValueMap(); assertEquals("1232", valueMap.get("new-version-property")); } @Test void testBar_setToNotExistingPath() { ModifiableValueMap modifiableValueMap = assetResource.adaptTo(ModifiableValueMap.class); modifiableValueMap.put("jcr:lastModifiedVersion", "123"); modifiableValueMap.put("newAsset", true); String path = "/conten/dam/not-existing-path.json"; foo.barNew(assetResource, "new-version-property", path); assertNull(aemContext.resourceResolver().getResource(path)); } @Test void testBar_setToExistingPath() { ModifiableValueMap modifiableValueMap = assetResource.adaptTo(ModifiableValueMap.class); modifiableValueMap.put("jcr:lastModifiedVersion", "123"); modifiableValueMap.put("newAsset", true); String path = "/conten/dam/not-existing-path.json"; aemContext.create().resource(path); foo.barNew(assetResource, "new-version-property", path); ValueMap valueMap = aemContext.resourceResolver().getResource(path).getValueMap(); assertEquals("1232", valueMap.get("new-version-property")); } }

 If tests are failing, please add the following Maven dependency:

<dependency> <groupId>xerces</groupId> <artifactId>xercesImpl</artifactId> <version>2.11.0</version> <scope>test</scope> </dependency>

 

2 replies

AMANATH_ULLAH
Community Advisor
Community Advisor
August 20, 2024

@djohn98390536 

 

Below is the junit code

 

import static org.mockito.Mockito.*;
import static org.junit.Assert.*;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

public class AssetTest {

@Mock
private Resource assetResource;

@Mock
private ResourceResolver resourceResolver;

@Mock
private ValueMap valueMap;

@Mock
private Resource resource;

@Mock
private ModifiableValueMap modifiableValueMap;

@InjectMocks
private AssetService assetService; // Assuming the code belongs to a service class named AssetService

private final String path = "/content/dam/sample";
private final String versionKey = "jcr:lastModifiedVersion";
private final String newVersionKey = "newVersion";
private final String version = "1.0";

@Before
public void setup() {
MockitoAnnotations.initMocks(this);
when(assetResource.getValueMap()).thenReturn(valueMap);
when(resourceResolver.getResource(path)).thenReturn(resource);
when(resource.adaptTo(ModifiableValueMap.class)).thenReturn(modifiableValueMap);
}

@Test
public void testExistingAsset() {
// Setup
when(valueMap.get(versionKey, String.class)).thenReturn(version);
when(valueMap.containsKey("newAsset")).thenReturn(false);

// Execute
assetService.updateAsset(assetResource, resourceResolver, path);

// Verify
verify(modifiableValueMap).put(newVersionKey, "1.01");
}

@Test
public void testNewAsset() {
// Setup
when(valueMap.get(versionKey, String.class)).thenReturn(version);
when(valueMap.containsKey("newAsset")).thenReturn(true);

// Execute
assetService.updateAsset(assetResource, resourceResolver, path);

// Verify
verify(modifiableValueMap).put(newVersionKey, "1.02");
}
}

Amanath Ullah
konstantyn_diachenko
Community Advisor
konstantyn_diachenkoCommunity AdvisorAccepted solution
Community Advisor
August 20, 2024

Hi @djohn98390536,

 

if you are interesting in the AEM Unit test, you can use my code below. However, I would suggest you to refactor your piece of code firstly, because it could lead to NullPointerException.

Refactored code (ResourceResolver#getResource, Resource#adaptTo can return null):

import org.apache.sling.api.resource.ModifiableValueMap; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.api.resource.ValueMap; import java.util.Optional; public class Foo { public void barNew(Resource assetResource, String newversion, String path) { ResourceResolver resourceResolver = assetResource.getResourceResolver(); ValueMap vm = assetResource.getValueMap(); String version = vm.get("jcr:lastModifiedVersion", String.class); boolean isNewAsset = vm.containsKey("newAsset"); String newVersion = isNewAsset ? version + 2 : version + 1; Optional.ofNullable(resourceResolver.getResource(path)) .map(resource -> resource.adaptTo(ModifiableValueMap.class)) .ifPresent(mvm -> mvm.put(newversion, newVersion)); } }

AEM Unit test:

import com.day.cq.dam.api.Asset; import io.wcm.testing.mock.aem.junit5.AemContext; import io.wcm.testing.mock.aem.junit5.AemContextExtension; import org.apache.sling.api.resource.ModifiableValueMap; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ValueMap; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import java.io.ByteArrayInputStream; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; @ExtendWith(AemContextExtension.class) class FooTest { private AemContext aemContext = new AemContext(); private Foo foo; private Resource assetResource; @BeforeEach void setUp() { foo = new Foo(); Asset asset = aemContext.create().asset("/conten/dam/foo.json", new ByteArrayInputStream("{}".getBytes()), "application/json"); assetResource = asset.adaptTo(Resource.class); } @Test void testBar_notNewAsset() { ModifiableValueMap modifiableValueMap = assetResource.adaptTo(ModifiableValueMap.class); modifiableValueMap.put("jcr:lastModifiedVersion", "123"); String path = "/conten/dam/foo.json"; foo.barNew(assetResource, "new-version-property", path); ValueMap valueMap = aemContext.resourceResolver().getResource(path).getValueMap(); assertEquals("1231", valueMap.get("new-version-property")); } @Test void testBar_newAsset() { ModifiableValueMap modifiableValueMap = assetResource.adaptTo(ModifiableValueMap.class); modifiableValueMap.put("jcr:lastModifiedVersion", "123"); modifiableValueMap.put("newAsset", true); String path = "/conten/dam/foo.json"; foo.barNew(assetResource, "new-version-property", path); ValueMap valueMap = aemContext.resourceResolver().getResource(path).getValueMap(); assertEquals("1232", valueMap.get("new-version-property")); } @Test void testBar_setToNotExistingPath() { ModifiableValueMap modifiableValueMap = assetResource.adaptTo(ModifiableValueMap.class); modifiableValueMap.put("jcr:lastModifiedVersion", "123"); modifiableValueMap.put("newAsset", true); String path = "/conten/dam/not-existing-path.json"; foo.barNew(assetResource, "new-version-property", path); assertNull(aemContext.resourceResolver().getResource(path)); } @Test void testBar_setToExistingPath() { ModifiableValueMap modifiableValueMap = assetResource.adaptTo(ModifiableValueMap.class); modifiableValueMap.put("jcr:lastModifiedVersion", "123"); modifiableValueMap.put("newAsset", true); String path = "/conten/dam/not-existing-path.json"; aemContext.create().resource(path); foo.barNew(assetResource, "new-version-property", path); ValueMap valueMap = aemContext.resourceResolver().getResource(path).getValueMap(); assertEquals("1232", valueMap.get("new-version-property")); } }

 If tests are failing, please add the following Maven dependency:

<dependency> <groupId>xerces</groupId> <artifactId>xercesImpl</artifactId> <version>2.11.0</version> <scope>test</scope> </dependency>

 

Kostiantyn Diachenko, Community Advisor, Certified Senior AEM Developer, creator of free AEM VLT Tool, maintainer of AEM Tools plugin.