Hello , I have requirement to write Junit for workflow , Can anyone please give sample test cases for below workflow?
@component(service = WorkflowProcess.class, property = {
"process.label" + " =sample Test process",
})
public class TestWorkFlow implements WorkflowProcess {
private static final Logger LOGGER = LoggerFactory.getLogger(TestWorkFlow.class);
Session session;
ResourceResolver resourceResolver;
@reference
TestConfiguration config;
@Override
public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap processArguments) {
try {
session = workflowSession.adaptTo(Session.class);
String payloadPath = workItem.getWorkflowData().getPayload().toString();
resourceResolver = workflowSession.adaptTo(ResourceResolver.class);
TestHelper testHelper = new TestHelper();
List<String> TestList = testHelper.getTestFolder(resourceResolver, payloadPath, config);
} catch (Exception exception)
{
LOGGER.error("Exception is :: " + exception);
}
}
}
_________________________________________________________________________________
sample json file ---->
{
"test":
{
"jcr:primaryType":"dam:Asset",
"jcr:createdBy":"admin",
}
}
Thanks in Advance.
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @Neha_Jadhav ,
To create JUnit test cases for the given AEM workflow, you can use mocking frameworks like Mockito to mock AEM-related objects such as WorkItem, WorkflowSession, ResourceResolver, etc. Below are some sample test cases for the TestWorkFlow class:
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
import java.util.HashMap;
import java.util.Map;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.metadata.MetaDataMap;
@RunWith(MockitoJUnitRunner.class)
public class TestWorkFlowTest {
@Mock
private WorkItem workItem;
@Mock
private WorkflowSession workflowSession;
@Mock
private MetaDataMap processArguments;
private TestWorkFlow testWorkFlow;
@Before
public void setup() {
testWorkFlow = new TestWorkFlow();
}
@Test
public void testExecute() throws Exception {
// Mock session
when(workflowSession.adaptTo(Session.class)).thenReturn(mock(Session.class));
// Mock payload path
when(workItem.getWorkflowData().getPayload()).thenReturn("/content/test");
// Mock ResourceResolver
ResourceResolver resourceResolver = mock(ResourceResolver.class);
when(workflowSession.adaptTo(ResourceResolver.class)).thenReturn(resourceResolver);
// Mock TestHelper
TestHelper testHelper = mock(TestHelper.class);
when(testHelper.getTestFolder(resourceResolver, "/content/test", config)).thenReturn(Arrays.asList("test1", "test2"));
// Set config in testWorkFlow
testWorkFlow.setConfig(mock(TestConfiguration.class));
// Execute workflow
testWorkFlow.execute(workItem, workflowSession, processArguments);
// Verify that TestHelper.getTestFolder was called
verify(testHelper).getTestFolder(eq(resourceResolver), eq("/content/test"), any(TestConfiguration.class));
}
@Test
public void testExceptionHandling() throws Exception {
// Mock session
when(workflowSession.adaptTo(Session.class)).thenReturn(mock(Session.class));
// Mock payload path
when(workItem.getWorkflowData().getPayload()).thenReturn("/content/test");
// Mock ResourceResolver
ResourceResolver resourceResolver = mock(ResourceResolver.class);
when(workflowSession.adaptTo(ResourceResolver.class)).thenReturn(resourceResolver);
// Mock TestHelper
TestHelper testHelper = mock(TestHelper.class);
when(testHelper.getTestFolder(resourceResolver, "/content/test", config)).thenThrow(new RuntimeException("Test Exception"));
// Set config in testWorkFlow
testWorkFlow.setConfig(mock(TestConfiguration.class));
// Execute workflow
testWorkFlow.execute(workItem, workflowSession, processArguments);
// Verify that error is logged
verify(TestWorkFlow.LOGGER).error(eq("Exception is :: "), any(Exception.class));
}
}
In these test cases, we are mocking the AEM-related objects and testing the execute method of the TestWorkFlow class. We are verifying that the TestHelper.getTestFolder method is called with the correct arguments and that the logger logs the exception correctly in case of an error.
You may need to adjust the test cases based on the actual behavior of your TestWorkFlow class and its dependencies. Additionally, you may need to mock other dependencies or set up additional mocks depending on your implementation.
I am sharing code snippet. Please adapt it to your needs
import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.exec.WorkflowProcess;
import io.wcm.testing.mock.aem.junit5.AemContextExtension;
import junit.framework.Assert;
import org.apache.commons.lang3.StringUtils;
import org.apache.jackrabbit.JcrConstants;
import org.apache.sling.api.resource.Resource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.junit.jupiter.api.Assertions.*;
@ExtendWith({AemContextExtension.class, MockitoExtension.class})
class UpdateTranslationProcessTest extends DMWorkflowProcessTest {
private static final String TEST_ASSET_WITH_TRANSLATION_METADATA = "translatedImage-with-cqtranslation-metadata";
@InjectMocks
private final WorkflowProcess step = new UpdateTranslationProcess();
@BeforeEach
void setup() {
ctx.load().json(JSON_PATH_CF, TEST_ASSET_ROOTPATH);
}
@Test
void test_execute_processArgs_not_defined() {
String testAsset = TEST_ASSET_ROOTPATH + "/sv/"+TEST_ASSET_WITH_TRANSLATION_METADATA;
initializeWorkflowProcess(StringUtils.EMPTY, testAsset);
// execute workflow process
assertThrows(WorkflowException.class, () -> step.execute(workItem, workflowSession, metaDataMap));
}
}
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.Workflow;
import com.adobe.granite.workflow.exec.WorkflowData;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.adobe.granite.workflow.metadata.SimpleMetaDataMap;
import io.wcm.testing.mock.aem.junit5.AemContext;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.jetbrains.annotations.NotNull;
import org.mockito.Mock;
import static org.mockito.Mockito.lenient;
public abstract class WorkflowProcessTest {
protected static final String TEST_ASSET_ROOTPATH = "/content/dam/image/";
protected static final String TEST_ASSET_WITH_RENDITIONS = TEST_ASSET_ROOTPATH + "/image-with-renditions";
protected static final String TEST_ASSET_WITHOUT_RENDITIONS = TEST_ASSET_ROOTPATH + "/image-without-renditions";
protected static final String TEST_ASSET_WITHOUT_DYNAMIC_MEDIA = TEST_ASSET_ROOTPATH + "/image-no-dm";
protected static final String JSON_PATH_CF = "/removecontentworkflowprocess/images.json";
protected final AemContext ctx = new AemContext(ResourceResolverType.JCR_MOCK);
@Mock
protected WorkItem workItem;
@Mock
protected WorkflowSession workflowSession;
@Mock
protected WorkflowData workflowData;
@Mock
protected Workflow workflow;
protected ResourceResolver resourceResolver;
protected MetaDataMap metaDataMap;
/**
* Start the workflow process. Initializing the properties for the process
*
* @param processArgs Process arguments provided in the workflow model for this process step
* @param payloadPath Path of the resource that is the payload for the workflow
*/
protected void initializeWorkflowProcess(final String processArgs, @NotNull final String payloadPath) {
metaDataMap = new SimpleMetaDataMap();
metaDataMap.put("PROCESS_ARGS", processArgs);
resourceResolver = ctx.resourceResolver();
lenient().when(workflowData.getPayloadType()).thenReturn("JCR_PATH");
lenient().when(workflowData.getPayload()).thenReturn(payloadPath);
lenient().when(workflowSession.adaptTo(ResourceResolver.class)).thenReturn(resourceResolver);
lenient().when(workItem.getWorkflowData()).thenReturn(workflowData);
lenient().when(workItem.getWorkflow()).thenReturn(workflow);
lenient().when(workflow.getWorkflowData()).thenReturn(workflowData);
lenient().when(workflowData.getMetaDataMap()).thenReturn(metaDataMap);
}
}
@Neha_Jadhav Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community
Hi @Neha_Jadhav ,
To create JUnit test cases for the given AEM workflow, you can use mocking frameworks like Mockito to mock AEM-related objects such as WorkItem, WorkflowSession, ResourceResolver, etc. Below are some sample test cases for the TestWorkFlow class:
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
import java.util.HashMap;
import java.util.Map;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.metadata.MetaDataMap;
@RunWith(MockitoJUnitRunner.class)
public class TestWorkFlowTest {
@Mock
private WorkItem workItem;
@Mock
private WorkflowSession workflowSession;
@Mock
private MetaDataMap processArguments;
private TestWorkFlow testWorkFlow;
@Before
public void setup() {
testWorkFlow = new TestWorkFlow();
}
@Test
public void testExecute() throws Exception {
// Mock session
when(workflowSession.adaptTo(Session.class)).thenReturn(mock(Session.class));
// Mock payload path
when(workItem.getWorkflowData().getPayload()).thenReturn("/content/test");
// Mock ResourceResolver
ResourceResolver resourceResolver = mock(ResourceResolver.class);
when(workflowSession.adaptTo(ResourceResolver.class)).thenReturn(resourceResolver);
// Mock TestHelper
TestHelper testHelper = mock(TestHelper.class);
when(testHelper.getTestFolder(resourceResolver, "/content/test", config)).thenReturn(Arrays.asList("test1", "test2"));
// Set config in testWorkFlow
testWorkFlow.setConfig(mock(TestConfiguration.class));
// Execute workflow
testWorkFlow.execute(workItem, workflowSession, processArguments);
// Verify that TestHelper.getTestFolder was called
verify(testHelper).getTestFolder(eq(resourceResolver), eq("/content/test"), any(TestConfiguration.class));
}
@Test
public void testExceptionHandling() throws Exception {
// Mock session
when(workflowSession.adaptTo(Session.class)).thenReturn(mock(Session.class));
// Mock payload path
when(workItem.getWorkflowData().getPayload()).thenReturn("/content/test");
// Mock ResourceResolver
ResourceResolver resourceResolver = mock(ResourceResolver.class);
when(workflowSession.adaptTo(ResourceResolver.class)).thenReturn(resourceResolver);
// Mock TestHelper
TestHelper testHelper = mock(TestHelper.class);
when(testHelper.getTestFolder(resourceResolver, "/content/test", config)).thenThrow(new RuntimeException("Test Exception"));
// Set config in testWorkFlow
testWorkFlow.setConfig(mock(TestConfiguration.class));
// Execute workflow
testWorkFlow.execute(workItem, workflowSession, processArguments);
// Verify that error is logged
verify(TestWorkFlow.LOGGER).error(eq("Exception is :: "), any(Exception.class));
}
}
In these test cases, we are mocking the AEM-related objects and testing the execute method of the TestWorkFlow class. We are verifying that the TestHelper.getTestFolder method is called with the correct arguments and that the logger logs the exception correctly in case of an error.
You may need to adjust the test cases based on the actual behavior of your TestWorkFlow class and its dependencies. Additionally, you may need to mock other dependencies or set up additional mocks depending on your implementation.
@Neha_Jadhav Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.
Views
Replies
Total Likes
Views
Likes
Replies