How to load a json in a MockJcr inside a unit test? | Community
Skip to main content
Level 3
February 15, 2023
Solved

How to load a json in a MockJcr inside a unit test?

  • February 15, 2023
  • 2 replies
  • 3898 views

How could you load a json with test data into a MockJcr inside a Java Unit test? 

 

I know it is possible to load a mock json into an AemContext, e,g, 

 

 

@ExtendWith(AemContextExtension.class) @ExtendWith(MockitoExtension.class) class MyTest { final AemContext context = new AemContext(); @Mock private Session session; private final String PROPERTY = "myproperty"; private final String[] ARGS={"myvalue"}; @BeforeEach void setup() { context.registerAdapter(WorkflowSession.class, Session.class, session); context.load().json("/pages/MyPage.json", "/content/page"); } public void myWorkflowViaSling() throws WorkflowException { String[] values = context.resourceResolver().getResource("/content/page/jcr:content").getValueMap().get(PROPERTY, ARG); assertEquals(ARGS,values); } }

 

 

But is there a way to load a mock json into a MockJcr?

 

 

class MyWorkflowTest { private final String PROPERTY = "myproperty"; private final String ARGS={"myvalue"}; public void myWorkflowViaJCR() throws Exception { Session mockSession = MockJcr.newSession(); // Here, I am adding nodes 1 by 1 and setting their properties, where it would be simpler to load a json mockSession.getRootNode().addNode("jcr:content").setProperty(PROPERTY, ARGS); Value[] valueMap = mockSession.getNode("/" + "jcr:content").getProperty(PROPERTY).getValues(); assertEquals(ARGS[0],valueMap[0].getString()); } }

 

 

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 jeremylanssiers

I found a working solution to load JSON data into a mock JCR.

 

@ExtendWith(AemContextExtension.class) @ExtendWith(MockitoExtension.class) class MyTest { private final AemContext jcrContext = new AemContext(ResourceResolverType.JCR_MOCK); private final Session jcrSession = jcrContext.resourceResolver().adaptTo(Session.class); private final String PROPERTY = "myproperty"; private final String ARGS={"myvalue"}; @2785667 public void myTestViaJCR() throws Exception { assertNotNull(jcrSession); jcrContext.registerAdapter(WorkflowSession.class, Session.class, jcrSession); jcrContext.load().json("/pages/MyPage.json", "/content/page"); Node root = jcrSession.getRootNode(); Node newNode = root.addNode("content").addNode("page").addNode("jcr:content"); newNode.setProperty(PROPERTY, ARGS); jcrSession.save(); Node node = jcrSession.getNode("/" + "content" + "/" + "page" + "/" + "jcr:content"); assertNotNull(node); Property property = node.getProperty(PROPERTY); assertNotNull(property); Value[] valueMap = property.getValues(); assertNotNull(valueMap); assertEquals(ARGS[0],valueMap[0].getString()); } }

 

2 replies

BrianKasingli
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
February 15, 2023

Why can't you use the AemContextExtension?

Level 3
February 15, 2023

I can. But the road less travled is more interesting. 🙂 
Also, I needed this approach to be able to test a workflow.

Nitin_laad
Community Advisor
Community Advisor
February 15, 2023

Hi @jeremylanssiers - check this, if it helps

@Rule public final AemContext context = new AemContext(); @Test public void testLoadJsonFile() throws RepositoryException, JSONException { // Add test node to Mock JCR Session session = context.resourceResolver().adaptTo(Session.class); Node node = session.getRootNode().addNode("content/myresource", "nt:unstructured"); node.setProperty("myproperty", "myvalue"); session.save(); InputStream is = getClass().getResourceAsStream("/mytest.json"); JSONObject jsonObject = new JSONObject(new JSONTokener(is)); context.load().json(jsonObject, "/content/myresource"); Node testNode = session.getNode("/content/myresource"); assertNotNull(testNode); } }
jeremylanssiersAuthorAccepted solution
Level 3
February 16, 2023

I found a working solution to load JSON data into a mock JCR.

 

@ExtendWith(AemContextExtension.class) @ExtendWith(MockitoExtension.class) class MyTest { private final AemContext jcrContext = new AemContext(ResourceResolverType.JCR_MOCK); private final Session jcrSession = jcrContext.resourceResolver().adaptTo(Session.class); private final String PROPERTY = "myproperty"; private final String ARGS={"myvalue"}; @2785667 public void myTestViaJCR() throws Exception { assertNotNull(jcrSession); jcrContext.registerAdapter(WorkflowSession.class, Session.class, jcrSession); jcrContext.load().json("/pages/MyPage.json", "/content/page"); Node root = jcrSession.getRootNode(); Node newNode = root.addNode("content").addNode("page").addNode("jcr:content"); newNode.setProperty(PROPERTY, ARGS); jcrSession.save(); Node node = jcrSession.getNode("/" + "content" + "/" + "page" + "/" + "jcr:content"); assertNotNull(node); Property property = node.getProperty(PROPERTY); assertNotNull(property); Value[] valueMap = property.getValues(); assertNotNull(valueMap); assertEquals(ARGS[0],valueMap[0].getString()); } }