adobe / aem-component-generator

AEM Component Generator is a java project that enables developers to generate the base structure of an AEM component using a JSON configuration file specifying component and dialog properties and other configuration options.
Apache License 2.0
113 stars 46 forks source link

Issue in test case with help of Junit #42

Closed Harshitha942 closed 3 years ago

Harshitha942 commented 4 years ago

Hi ,for the same component above , I am trying to write down a testcase with help of Junit , but there are errors throwing up

image image this is the error im getting

This is my test class. package com.aem.geeks.core.models.impl;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith;

import com.aem.geeks.core.models.Component;

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

@ExtendWith(AemContextExtension.class) class ComponentimplTest { private final AemContext ctx = new AemContext();

@BeforeEach
void setUp() throws Exception {
    ctx.addModelsForClasses(Componentimpl.class);
    ctx.load().json("/com/aem/geeks/core/models/ComponentimplTest.json", "/content");

}

@Test
void testGetFirstName() {

    final String expected = "Jane";

     ctx.currentResource("/content/component");
        Component component = ctx.request().adaptTo(Component.class);

        String actual = component.getFirstName();

        assertEquals(expected, actual);

}

@Test
void testGetLastName() {
    final String expected = "Doe";

     ctx.currentResource("/content/component");
        Component component = ctx.request().adaptTo(Component.class);

        String actual = component.getLastName();

        assertEquals(expected, actual);
}

@Test
void testGetGender() {
    final String expected = "Female";

     ctx.currentResource("/content/component");
        Component component = ctx.request().adaptTo(Component.class);

        String actual = component.getGender();

        assertEquals(expected, actual);
}

@Test
void testGetPhoneNo() {
    fail("Not yet implemented");
}

}

Mock Json class { "component": { "jcr:primaryType": "nt:unstructured", "sling:resourceType": "aemgeeks/components/content/component", "firstname": "Jane", "lastname": "Doe", "gender": "Female"

}

}

Originally posted by @Harshitha942 in https://github.com/adobe/aem-component-generator/issues/41#issuecomment-721549776

Harshitha942 commented 4 years ago

Java Class

package com.aem.geeks.core.models.impl;

import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.models.annotations.Default; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.Via; import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;

import com.aem.geeks.core.models.Component;

@Model(adaptables = SlingHttpServletRequest.class, adapters = Component.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class Componentimpl implements Component {

@ValueMapValue
@Via("Resource")@Default(values = "****")
String fname;

@ValueMapValue
@Default(values = "####")
String lname;

@ValueMapValue
String gender;

@ValueMapValue
Long phno;

@Override
public String getFirstName() {
    return fname;
}

@Override
public String getLastName() {
    return lname;
}

@Override
public String getGender() {
    return gender;
}

@Override
public Long getPhoneNo() {
    return phno;
}

}

Interface package com.aem.geeks.core.models;

public interface Component {

String getFirstName();

String getLastName();

String getGender();

Long getPhoneNo();

}

HitmanInWis commented 4 years ago

I'm not quite sure what your issue is here, but your errors seem to be related to running junit in general as there is nothing in the error message that refers to your test class. Are you able to run junit tests for other classes in your project?

HitmanInWis commented 4 years ago

Im also curious if you have multiple versions of junit on your path. Your test seems to be referring to junit 5 classes, but your error messages are referring to junit 4.

Harshitha942 commented 4 years ago

I tried running Junit for other classes in the same project , but gives the same error I have included Junit 5 in the Java build path , Yes exactly , error is referring to Junit 4 I even tried to have Junit 4 , instead of Junit 5 ,in the build path, but that did not work Also tried to change my version from 4.8.2 to 5. , but no luck

HitmanInWis commented 4 years ago

Try adding the following to your pom.xml

        <!-- for testing we need the new ResourceTypeBasedResourcePicker -->
        <dependency>
            <groupId>org.apache.sling</groupId>
            <artifactId>org.apache.sling.models.impl</artifactId>
            <version>1.4.6</version>
            <scope>test</scope>
        </dependency>
Harshitha942 commented 4 years ago

image Now im getting the above error

HitmanInWis commented 4 years ago

Try all of this in your pom.xml for the testing scope


            <dependency>
                <groupId>org.junit</groupId>
                <artifactId>junit-bom</artifactId>
                <version>5.6.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>io.wcm</groupId>
                <artifactId>io.wcm.testing.aem-mock.junit5</artifactId>
                <version>3.1.0</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.sling</groupId>
                <artifactId>org.apache.sling.testing.sling-mock.junit5</artifactId>
                <version>2.6.2</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.sling</groupId>
                <artifactId>org.apache.sling.testing.osgi-mock.junit5</artifactId>
                <version>2.4.16</version>
                <scope>test</scope>
            </dependency>
        <!-- for testing we need the new ResourceTypeBasedResourcePicker -->
        <dependency>
            <groupId>org.apache.sling</groupId>
            <artifactId>org.apache.sling.models.impl</artifactId>
            <version>1.4.6</version>
            <scope>test</scope>
        </dependency>
      <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.6.0</version>
      </dependency>
      <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.6.0</version>
      </dependency>
      <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-params</artifactId>
        <version>5.6.0</version>
      </dependency>
HitmanInWis commented 3 years ago

Closing this issue as it appears to be related to the user's project setup and not directly caused by the Component Generator (which simply generates a stubbed test).