In AEM, there is a Java USE class wherein following code is present getWcmMode().isEdit()
Now, I am struggling to mock this object using mockito in Test java class. Is there any way we can do that?
In AEM, there is a Java USE class wherein following code is present getWcmMode().isEdit()
Now, I am struggling to mock this object using mockito in Test java class. Is there any way we can do that?
getWcmMode()
is a final method in WCMUsePojo
, mockito does not support mocking final methods by default.
you will have to enable it by creating a file named org.mockito.plugins.MockMaker
in classpath (put it in the test resources/mockito-extensions folder) and put the following single line
mock-maker-inline
then you can use when
to specify function return values as usual-
@Test
public void testSomeComponetnInNOTEDITMode() {
//setup wcmmode
SightlyWCMMode fakeDisabledMode = mock(SightlyWCMMode.class);
when(fakeDisabledMode.isEdit()).thenReturn(false);
//ComponentUseClass extends WCMUsePojo
ComponentUseClass fakeComponent = mock(ComponentUseClass.class);
when(fakeComponent.getWcmMode()).thenReturn(fakeDisabledMode);
assertFalse(fakeComponent.getWcmMode().isEdit());
//do some more not Edit mode testing on fakeComponent.
}