Junit for extended core component teaser | Community
Skip to main content
Level 5
April 27, 2023
Solved

Junit for extended core component teaser

  • April 27, 2023
  • 6 replies
  • 2025 views

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

 

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 Sady_Rifat

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.


@aravinds,

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.

6 replies

arunpatidar
Community Advisor
Community Advisor
April 27, 2023
aravindSAuthor
Level 5
April 27, 2023

We tried it is not working,
@Mock
TeaserImpl coreTeaserImpl;

 

here how to mock the values for coreTeaserImpl

Sady_Rifat
Community Advisor
Community Advisor
April 27, 2023

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.

aravindSAuthor
Level 5
April 27, 2023

Mockito cannot mock this class: interface com.adobe.cq.wcm.core.components.models.Teaser.

Sady_Rifat
Community Advisor
Community Advisor
April 27, 2023

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.

 

aravindSAuthor
Level 5
April 27, 2023

 

Sady_Rifat
Community Advisor
Community Advisor
April 28, 2023

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 

aravindSAuthor
Level 5
April 28, 2023

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.