Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

AEM-junit for text component using textmodel

Avatar

Level 4

I am building  project with Junit for Text

WHEN I return plain text (Lorem ipsum dolor sit amet.) for getText.

THEN I should see “Lorem ipsum dolor sit amet.” returned for getText().

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

Hey @vinuu123 ,

 

Here is a quick reference : unit-testing-aem for Junit testing.

 

For your usecase, you need to mock the model under the test and command the Junits as and when required like:
If someone is calling a getter from mockModel you can use :

when(mockModel.getSomeProperty).thenReturn("Lorem Ipsum...");

 

Here's a tutorial on how you use asserting : junit-assert-tutorial .

 

Please feel free to reach out if that doesnt help. Thanks.

View solution in original post

4 Replies

Avatar

Community Advisor

Hi @vinuu123,

I believe you are looking for Mockito's - when thenReturn function. You will have to use set as below:

when(textModelObject.getText()).thenReturn("Lorem ipsum dolor sit amet.");

with this you will be able to check using assertEquals as below:

assertEquals("Lorem ipsum dolor sit amet.",object.getText());


Hope this helps!
Jineet

Avatar

Level 4

what is object here(object.getText()) and what is textmodelobject

Avatar

Correct answer by
Employee Advisor

Hey @vinuu123 ,

 

Here is a quick reference : unit-testing-aem for Junit testing.

 

For your usecase, you need to mock the model under the test and command the Junits as and when required like:
If someone is calling a getter from mockModel you can use :

when(mockModel.getSomeProperty).thenReturn("Lorem Ipsum...");

 

Here's a tutorial on how you use asserting : junit-assert-tutorial .

 

Please feel free to reach out if that doesnt help. Thanks.