Expand my Community achievements bar.

SOLVED

Junit for extended core component teaser

Avatar

Level 4

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

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@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.

Sady_Rifat_0-1682684108090.png

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.

View solution in original post

9 Replies

Avatar

Level 4

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

 

here how to mock the values for coreTeaserImpl

Avatar

Community Advisor

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.

Avatar

Level 4

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

Avatar

Community Advisor

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.

Sady_Rifat_0-1682597110267.png

 

Avatar

Community Advisor

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 

Avatar

Level 4

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.

Avatar

Correct answer by
Community Advisor

@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.

Sady_Rifat_0-1682684108090.png

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.