Solved
How to load a json in a MockJcr inside a unit test?
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());
}
}