Hi, I have a requirement to create a component to extend from teaser. I'm writing the junit for my custom component.
How to mock teaser java class object in my custom component where i extended the teaser core component.
Thanks in advance
Solved! Go to Solution.
Views
Replies
Total Likes
Not sure how you check this or run this code. See my previous screenshot I set a debugger point on the main Model Java class. That means it actually calls the real method.
Check this again, perfectly fine and shows the coverage percentage on my IDE.
This is not only for this project. I just copy the process from my client project and this is how coverage is made up.
Hope you got my point.
We tried it is not working,
@Mock
TeaserImpl coreTeaserImpl;
here how to mock the values for coreTeaserImpl
Hello @aravindS ,
I tried many ways to write unit-test but nothing works for me. Especially the @Self annotation
@Self
private Teaser coreTeaser;
Then I conclude by this way to write the unit test
import static org.mockito.Mockito.when;
import org.mockito.Mockito;
@ExtendWith({AemContextExtension.class, MockitoExtension.class})
public class TeaserExtdImplTest {
private final AemContext aemContext = new AemContext(ResourceResolverType.JCR_MOCK);
private final TeaserExtd teaserExtd = Mockito.spy(TeaserExtdImpl.class);
@Mock
private Teaser coreTeaser;
@BeforeEach
void init() throws NoSuchFieldException {
PrivateAccessor.setField(teaserExtd, "coreTeaser", coreTeaser);
}
@Test
public void idTest() {
when(coreTeaser.getId()).thenReturn("1234");
assertNotNull(coreTeaser.getId());
}
}
You may try this way. This might help you.
Mockito cannot mock this class: interface com.adobe.cq.wcm.core.components.models.Teaser.
Not sure why you get this problem. Please check this out. I just pushed a code for the Carousel component. https://github.com/Sady-Rifat/my-project/commit/53adef48530751372af147a25527921b3dddc5f2
Maybe you have other issues. This repo might help you for understanding.
Maybe the issue is related to your Java version. Here I am using JDK 11.
Check this discussion: https://stackoverflow.com/questions/41050029/mockito-cannot-mock-this-class
Hi, Thanks for the reply.
I have changed the mockito version, using 5.3.1 now not getting error. But Impl code is not getting coverage. this code runs alone.
Not sure how you check this or run this code. See my previous screenshot I set a debugger point on the main Model Java class. That means it actually calls the real method.
Check this again, perfectly fine and shows the coverage percentage on my IDE.
This is not only for this project. I just copy the process from my client project and this is how coverage is made up.
Hope you got my point.
Views
Likes
Replies
Views
Like
Replies