AEM junits using aem mocks | Community
Skip to main content
Level 3
October 13, 2022
Solved

AEM junits using aem mocks

  • October 13, 2022
  • 4 replies
  • 3023 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by girishb83316758

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>

4 replies

Kiran_Vedantam
Community Advisor
Community Advisor
October 13, 2022

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/adobe/acs/commons/mcp/impl/processes/asset/UrlAssetImportTest.java

 

Hope this helps!

 

Thanks,

Kiran Vedantam

Saravanan_Dharmaraj
Community Advisor
Community Advisor
October 13, 2022
Lokesh_Vajrala
Community Advisor
Community Advisor
October 13, 2022

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");
    }
}
Level 3
October 13, 2022
Can you help with the dependencies you have added??
Lokesh_Vajrala
Community Advisor
Community Advisor
October 13, 2022

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. 

girishb83316758AuthorAccepted solution
Level 3
December 30, 2022

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>