Junit for extended core component teaser | Community
Skip to main content
Level 5
Apr 27, 2023 10:19 AM UTC
Solved

Junit for extended core component teaser

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.
Sady_Rifat
Sady_RifatAccepted solution
New Participant
Apr 28, 2023 12:17 PM UTC

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.

9 replies

aravindSAuthor
Level 5
Apr 27, 2023 2:33 PM UTC

 

Translate
Sady_Rifat
New Participant
Apr 28, 2023 5:32 AM UTC

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 

Translate
aravindSAuthor
Level 5
Apr 28, 2023 6:21 AM UTC

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.

Translate
Sady_Rifat
New Participant
Apr 27, 2023 12:05 PM UTC

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.

 

Translate
aravindSAuthor
Level 5
Apr 27, 2023 11:46 AM UTC

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

Translate
Sady_Rifat
New Participant
Apr 27, 2023 11:26 AM UTC

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.

Translate
aravindSAuthor
Level 5
Apr 27, 2023 11:08 AM UTC

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

 

here how to mock the values for coreTeaserImpl

Translate