Expand my Community achievements bar.

SOLVED

AEM junits using aem mocks

Avatar

Level 4

Hi All

 

I'm using aem mocks in my project

Aem mocks version: 5.0.0

Aem mock core version:5.0.0

 

And i have written a junit test class like below

 

Private AemContext aemContext = new AemContext

 

@Before

Public void setup() {

 

Assest asset = aemContext.create().asset("/content/dam/simple.jpg", 240, 280, "image/jpeg")

 

}

 

When I execute the test class.

 

I get the error

 

Java.lang.RuntimeExcsption: No asset manager

at io.wcm.tetsing.mock.builder.contentBuilder.asset()

 

Can some one please help tell me what is the issue??

 @Jörg Hoh

1 Accepted Solution

Avatar

Correct answer by
Level 4

For some reason I don't know why AemContext class is not working with below dependency in AEM6.5.12

<dependency>

<groupId>io.wcm</groupId>

<artifactId>io.wcm.testing.aem-mock.junit5</artifactId>

<scope>test</test>

</dependency>

 

I added junit4 version and I was able to use AemContext class and mock assets in Junits

 

Below dependency worked for me

 

<dependency>

<groupId>io.wcm</groupId>

<artifactId>io.wcm.testing.aem-mock.junit4</artifactId>

<version>4.1.8</version>

</dependency>

View solution in original post

8 Replies

Avatar

Community Advisor

Hi @girishb83316758 

 

As the error says, you need to mock the asset manager like this:

 

@Mock
 

private AssetManager assetManager;

 

Reference link: https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/master/bundle/src/test/java/com/ad...

 

Hope this helps!

 

Thanks,

Kiran Vedantam

Avatar

Community Advisor

You seem to be using the generic AemContext() instead use the `AemContext(ResourceResolverType.JCR_MOCK)`, which sets the ResourceResolver and be able to adaptTo AssetManager object, which is then used when creating the Asset.

 

I have an example here that you can refer to

 

@ExtendWith(AemContextExtension.class)
public class AssetTest {
    AemContext aemContext = new AemContext(ResourceResolverType.JCR_MOCK);
    @BeforeEach
    void setup() {
        asset = aemContext.create().asset("/content/dam/simple.jpg", 240, 280, "image/jpeg");
    }
    private Asset asset;

    @Test
    void testAssetPath() {
        assertEquals(asset.getPath(), "/content/dam/simple.jpg");
    }
}

Avatar

Community Advisor

The two main dependencies you needed are 

<dependency>
     <groupId>io.wcm</groupId>
     <artifactId>io.wcm.testing.aem-mock.junit5</artifactId>
</dependency>
<dependency>
<groupId>junit-addons</groupId> <artifactId>junit-addons</artifactId> </dependency>

You may need other dependencies based on your project setup. 

Avatar

Community Advisor

I'm using a cloud service setup with sdk api, not uber-jar.

Avatar

Correct answer by
Level 4

For some reason I don't know why AemContext class is not working with below dependency in AEM6.5.12

<dependency>

<groupId>io.wcm</groupId>

<artifactId>io.wcm.testing.aem-mock.junit5</artifactId>

<scope>test</test>

</dependency>

 

I added junit4 version and I was able to use AemContext class and mock assets in Junits

 

Below dependency worked for me

 

<dependency>

<groupId>io.wcm</groupId>

<artifactId>io.wcm.testing.aem-mock.junit4</artifactId>

<version>4.1.8</version>

</dependency>