Expand my Community achievements bar.

SOLVED

AEM Mockito junit testing for multifield

Avatar

Level 3

 

 

Hi All,
i have  written a multifield in if else statement now,
I need to write JUnits for that Method (getMltifeildlist()) help me with the Testing code for this method 

 

 

 

 

 

Screenshot (201).png

1 Accepted Solution

Avatar

Correct answer by
Community Advisor
3 Replies

Avatar

Community Advisor

@HariDo you can write this as below

 

@BeforeEach
void setUp() throws Exception {
List<LinkComponentImpl> linkcomponentimpl = new ArrayList<>();

linkComponentimplClass = new LinkComponentImpl();
}

/**
* Test.
*/
@Test
void test() {
Assert.assertNotNull(linkComponentimplClass.getMultifieldlist());
}

Avatar

Level 4
Use this code for Example
Main Class: @Override public List<String> getBooks() { if (books != null) { return new ArrayList<String>(books); } else { return Collections.emptyList(); } } Test Class: private final AemContext aemContext=new AemContext(); private Author author; @Test void getBooks() { aemContext.currentResource("/component/author"); author=aemContext.request().adaptTo(Author.class); assertEquals(4,author.getBooks().size()); assertEquals("JAVA",author.getBooks().get(0)); assertEquals("DS",author.getBooks().get(2)); }

Avatar

Correct answer by
Community Advisor