Expand my Community achievements bar.

SOLVED

java.lang.UnsupportedOperationException in Junit test - content fragment - ContentElement.getValue()

Avatar

Level 2

I am trying to write a Junit for below piece of code and getting this error

java.lang.UnsupportedOperationException
at io.wcm.testing.mock.aem.MockContentFragment_ContentElement_Structured.getValue(MockContentFragment_ContentElement_Structured.java:173)

 

Code snippet

 

private String getAuthoredDate(ContentFragment contentFragment) {
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy");
ContentElement contentElement = contentFragment.getElement("authoredDate");
Calendar date = (Calendar) contentElement.getValue().getValue();
return sdf.format(date.getTime());
}

 

Any Idea how can I mock the ContentElement getvalue and FragmentData getvalue functions?

 

Thanks!

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

Looks like these methods are not supported yet with the wcm.io mocking library (see [1]). I would raise a ticket there and ask about plans to support these methods.

 

 

[1] https://github.com/wcm-io/io.wcm.testing.aem-mock/blob/develop/core/src/main/java/io/wcm/testing/moc...

 

 

View solution in original post

5 Replies

Avatar

Correct answer by
Employee Advisor

Looks like these methods are not supported yet with the wcm.io mocking library (see [1]). I would raise a ticket there and ask about plans to support these methods.

 

 

[1] https://github.com/wcm-io/io.wcm.testing.aem-mock/blob/develop/core/src/main/java/io/wcm/testing/moc...

 

 

Avatar

Level 1

Thanks for pointing this out Jorg, I spent so long trying to understand what was wrong with my code before stumbling on this...

 

Seems like the issue is tracked at https://wcm-io.atlassian.net/browse/WTES-64. Is there a way to +1 it or something to indicate that other people are having this problem and it would be nice if it were prioritized.

 

Things like this can become problematic with AEMaaCS's code coverage requirements. If we have code coverage requirements, seems only fair that we get access to the proper tools that will enable us to actually cover our code.

Avatar

Level 7

is there any solution for that I stuck at same point.

Avatar

Level 4

@Jörg_Hoh For Integers also its not supported. Can you help me understand how can I get it fixed for integer ?

 

Thanks in Advance! 

Avatar

Level 9

Well, if it's not supported by AEM Mocks, you will need to mock them yourself. This is usually done with the Mockito library.

A good practice I use is to create mocks based on interfaces and then register them in the AEM context with a custom builder via:

this.aemContext.registerInjectActivateService(...)

 Since this doesn't look like a service, you need to use the old school mocking for your test.

 

Good luck,

Daniel