JUNIT5: What Exactly is org.mockito.junit.jupiter.MockitoExtension and Sling Model Example

org.mockito.junit.jupiter.MockitoExtension is a JUnit 5 extension provided by the Mockito library. It allows you to use the Mockito framework to create and inject mocked objects into your JUnit 5 test classes. This extension provides several features such as annotation-based mock creation, automatic detection of unused stubs, and support for the Mockito API. It enables you to write clean and readable tests by allowing you to use the familiar Mockito API directly in your JUnit 5 tests, without the need to manually instantiate mock objects or use a separate mocking framework. This can make it easier to write and maintain unit tests that use mocked dependencies.

In additional, this post will be relating more to a Sling Model. In this example, we will create a sling model utilizing dependencies of 2 services.


Example Implementation for a Sling Model

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Here is an example implementation of the UnderTestSlingModel class that can be used in the provided JUnit5 test:

java
Copy code
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;

@Model(adaptables = SlingHttpServletRequest.class)
public class UnderTestSlingModel {

    @SlingObject
    private Service1 service1;

    @SlingObject
    private Service2 service2;

    public String getData() {
        return service1.getData() + service2.getData();
    }
}

Example JUNIT5:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoExtension;

import io.wcm.testing.mock.aem.junit5.AemContext;
import io.wcm.testing.mock.aem.junit5.AemContextExtension;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

@ExtendWith({AemContextExtension.class, MockitoExtension.class})
class UnderTestSlingModelTest {

    @Mock
    private Service1 service1;

    @Mock
    private Service2 service2;

    @InjectMocks
    private UnderTestSlingModel underTest;

    private AemContext context = new AemContext(ResourceResolverType.JCR_MOCK);

    @Test
    void test1() {
        // Setting up the mocked services
        context.registerService(service1);
        context.registerService(service2);

        // Creating the underTest sling model
        underTest = context.request().adaptTo(UnderTestSlingModel.class);

        //Testing the mocked services
        when(service1.getData(any())).thenReturn("test1");
        when(service2.getData(any())).thenReturn("test2");

        assertEquals("test1test2", underTest.getData());
    }

    @Test
    void test2() {
        // Setting up the mocked services
        context.registerService(service1);
        context.registerService(service2);

        // Creating the underTest sling model
        underTest = context.request().adaptTo(UnderTestSlingModel.class);

        //Testing the mocked services
        when(service1.getData(any())).thenReturn("test3");
        when(service2.getData(any())).thenReturn("test4");

        assertEquals("test3test4", underTest.getData());
    }
}

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.7.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.7.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>io.wcm</groupId>
    <artifactId>io.wcm.testing.mock.aem</artifactId>
    <version>7.4.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-junit-jupiter</artifactId>
    <version>3.6.28</version>
    <scope>test</scope>
</dependency>

Hello, I am an enthusiastic Adobe Community Advisor and a seasoned Lead AEM Developer. I am currently serving as an AEM Technical Lead at MNPDigital.ca, bringing over a decade of extensive web engineering experience and more than eight years of practical AEM experience to the table. My goal is to give back to the AEM Full Stack Development community by sharing my wealth of knowledge with others. You can connect with me on LinkedIn.

One thought on “JUNIT5: What Exactly is org.mockito.junit.jupiter.MockitoExtension and Sling Model Example

Leave a Reply

Your email address will not be published. Required fields are marked *


Back To Top