Expand my Community achievements bar.

SOLVED

Testing AEM Servlets

Avatar

Level 4

Hello its me again,

I want to test my Servlet but after the test started I got an error message:

Error:(60, 28) java: cannot access org.junit.rules.TestRule

  class file for org.junit.rules.TestRule not found

The test does not start.

I don't have any Idea how to solve this, I am also not very experienced with test for AEM. It would be nice if you can tell me something.

@RunWith(MockitoJUnitRunner.class)

public class RssFeedServletTest {

   @InjectMocks
   private RssFeedServlet rssFeedServlet;
   @Mock
   private SlingHttpServletRequest request;
   @Mock
   private SlingHttpServletResponse response;
   @Mock
   private Page page;
   @Mock
   Resource resource;
   Logger LOGGER;

   @Rule
   public final AemContext context = new AemContext(ResourceResolverType.JCR_MOCK);

   @Before
   public void setUp() throws Exception {

   rssFeedServlet = new RssFeedServlet();
   MockitoAnnotations.initMocks(this);
   mockStatic(LoggerFactory.class);
   LOGGER = mock(Logger.class);
   PowerMockito.when(LoggerFactory.getLogger(any(Class.class))).thenReturn(LOGGER);
   when(request.getServerName()).thenReturn("https://www.test.com");

   Page page = context.pageManager().create("/content/test/de/ueber-uns/presse/press-releases/2017/release1", "release1",
   "/apps/test/foundation/templates/basepage", "release-title1");

   when(resource.getParent().adaptTo(Page.class)).thenReturn(page);

   when(request.getResource()).thenReturn(resource);

   }

   @Test
   public void testDoGetForJson() throws IOException {

   context.requestPathInfo().setSelectorString("rss");
   context.requestPathInfo().setExtension("xml");
   context.currentPage("/content/test/de/ueber-uns/presse/press-releases");

   rssFeedServlet.doGet(context.request(), context.response());


   }

}

Here are my dependencies:

<dependencies>
  <dependency>
  <groupId>org.jacoco</groupId>
  <artifactId>org.jacoco.agent</artifactId>
  <classifier>runtime</classifier>
  <scope>test</scope>
  <version>0.7.9</version>
  </dependency>
  <dependency>
  <groupId>org.sonarsource.java</groupId>
  <artifactId>sonar-jacoco-listeners</artifactId>
  <version>3.8</version>
  <scope>test</scope>
  </dependency>
  <dependency>
  <groupId>org.apache.felix</groupId>
  <artifactId>org.apache.felix.scr</artifactId>
  </dependency>
  <!-- OSGi Dependencies -->
  <dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>servlet-api</artifactId>
  </dependency>
  <dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  </dependency>
  <dependency>
  <groupId>com.test.aem</groupId>
  <artifactId>integration-testing-tools</artifactId>
  </dependency>
  <dependency>
  <groupId>org.apache.sling</groupId>
  <artifactId>org.apache.sling.testing.tools</artifactId>
  </dependency>
  <dependency>
  <groupId>org.apache.sling</groupId>
  <artifactId>org.apache.sling.junit.core</artifactId>
  </dependency>
  <dependency>
  <groupId>org.apache.sling</groupId>
  <artifactId>org.apache.sling.junit.scriptable</artifactId>
  </dependency>
  <dependency>
  <groupId>org.apache.sling</groupId>
  <artifactId>org.apache.sling.junit.remote</artifactId>
  </dependency>
  <dependency>
  <groupId>com.adobe.aem</groupId>
  <artifactId>uber-jar</artifactId>
  <version>6.2.0</version>
  <classifier>apis</classifier>
  <scope>provided</scope>
  </dependency>
  <dependency>
  <groupId>com.test.aem</groupId>
  <artifactId>test-aem-foundation.core</artifactId>
  <version>1.1.43</version>
  <scope>provided</scope>
  </dependency>
  <!-- needed for javax.Inject in AEM6.2 -->
  <dependency>
  <groupId>org.apache.felix</groupId>
  <artifactId>org.apache.felix.scr.annotations</artifactId>
  </dependency>
  <dependency>
  <groupId>biz.aQute</groupId>
  <artifactId>bndlib</artifactId>
  </dependency>
  <dependency>
  <groupId>org.osgi</groupId>
  <artifactId>org.osgi.core</artifactId>
  </dependency>
  <dependency>
  <groupId>org.osgi</groupId>
  <artifactId>org.osgi.compendium</artifactId>
  </dependency>
  <!-- Other Dependencies -->
  <dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  </dependency>
  <dependency>
  <groupId>javax.jcr</groupId>
  <artifactId>jcr</artifactId>
  </dependency>
  <dependency>
  <groupId>javax.inject</groupId>
  <artifactId>javax.inject</artifactId>
  <version>1</version>
  </dependency>
  <dependency>
  <groupId>com.adobe.aem</groupId>
  <artifactId>aem-api</artifactId>
  </dependency>
  <dependency>
  <groupId>org.apache.sling</groupId>
  <artifactId>org.apache.sling.models.api</artifactId>
  </dependency>
  <dependency>
  <groupId>org.apache.sling</groupId>
  <artifactId>org.apache.sling.models.impl</artifactId>
  </dependency>
  <dependency>
  <groupId>com.turbomanage.basic-http-client</groupId>
  <artifactId>http-client-java</artifactId>
  <version>0.89</version>
  </dependency>
  <dependency>
  <groupId>com.mailjet</groupId>
  <artifactId>mailjet-client</artifactId>
  <version>4.0.6-SNAPSHOT</version>
  </dependency>
  <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  </dependency>
  <dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-all</artifactId>
  <version>1.10.19</version>
  </dependency>
  <dependency>
  <groupId>org.powermock</groupId>
  <artifactId>powermock-module-junit4</artifactId>
  <version>1.7.1</version>
  </dependency>
  <dependency>
  <groupId>org.powermock</groupId>
  <artifactId>powermock-api-mockito</artifactId>
  <version>1.7.1</version>
  </dependency>
  <dependency>
  <groupId>junit-addons</groupId>
  <artifactId>junit-addons</artifactId>
  </dependency>
  <dependency>
  <groupId>io.wcm</groupId>
  <artifactId>io.wcm.testing.aem-mock</artifactId>
  </dependency>
  <dependency>
  <groupId>commons-codec</groupId>
  <artifactId>commons-codec</artifactId>
  </dependency>
  <dependency>
  <groupId>commons-logging</groupId>
  <artifactId>commons-logging</artifactId>
  </dependency>
  <dependency>
  <groupId>org.apache.avalon.framework</groupId>
  <artifactId>avalon-framework-api</artifactId>
  </dependency>
  <dependency>
  <groupId>logkit</groupId>
  <artifactId>logkit</artifactId>
  </dependency>
  <dependency>
  <groupId>javax.jms</groupId>
  <artifactId>jms</artifactId>
  </dependency>
  <dependency>
  <groupId>com.day.commons</groupId>
  <artifactId>day-commons-any</artifactId>
  </dependency>
  <dependency>
  <groupId>org.apache.sling</groupId>
  <artifactId>org.apache.sling.testing.sling-mock</artifactId>
  </dependency>
  <dependency>
  <groupId>junit</groupId>
  <artifactId>junit-dep</artifactId>
  </dependency>
  <dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  </dependency>
  <dependency>
  <groupId>io.wcm</groupId>
  <artifactId>io.wcm.testing.junit-commons</artifactId>
  </dependency>
  <dependency>
  <groupId>org.jsoup</groupId>
  <artifactId>jsoup</artifactId>
  </dependency>
  <dependency>
  <groupId>org.jmockit</groupId>
  <artifactId>jmockit</artifactId>
  <version>1.34</version>
  </dependency>
  <dependency>
  <groupId>com.google.code.findbugs</groupId>
  <artifactId>jsr305</artifactId>
  <version>3.0.0</version>
  </dependency>
  <dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>6.8.8</version>
  </dependency>
  <dependency>
  <groupId>org.json</groupId>
  <artifactId>json</artifactId>
  <version>20090211</version>
  </dependency>
  <dependency>
  <groupId>commons-io</groupId>
  <artifactId>commons-io</artifactId>
  </dependency>
  <dependency>
  <groupId>net.tanesha.recaptcha4j</groupId>
  <artifactId>recaptcha4j</artifactId>
  <version>0.0.7</version>
  </dependency>
  <dependency>
  <groupId>org.apache.axis</groupId>
  <artifactId>axis</artifactId>
  <version>1.4</version>
  <scope>compile</scope>
  </dependency>
  <dependency>
  <groupId>org.apache.axis</groupId>
  <artifactId>axis-jaxrpc</artifactId>
  <version>1.4</version>
  <scope>compile</scope>
  </dependency>
  <dependency>
  <groupId>org.apache.axis</groupId>
  <artifactId>axis-saaj</artifactId>
  <version>1.4</version>
  <scope>compile</scope>
  </dependency>
  <dependency>
  <groupId>wsdl4j</groupId>
  <artifactId>wsdl4j</artifactId>
  <version>1.6.2</version>
  <scope>compile</scope>
  </dependency>
  <dependency>
  <groupId>commons-discovery</groupId>
  <artifactId>commons-discovery</artifactId>
  <version>0.2</version>
  <scope>compile</scope>
  </dependency>
  <dependency>
  <groupId>javax.xml</groupId>
  <artifactId>jaxrpc-api</artifactId>
  <version>1.1</version>
  </dependency>
  <dependency>
  <groupId>javax.mail</groupId>
  <artifactId>mail</artifactId>
  <version>1.4.1</version>
  </dependency>
  <dependency>
  <groupId>javax.activation</groupId>
  <artifactId>activation</artifactId>
  <version>1.1</version>
  </dependency>
  <dependency>
  <groupId>commons-lang</groupId>
  <artifactId>commons-lang</artifactId>
  <version>2.6</version>
  </dependency>
  <dependency>
  <groupId>org.apache.solr</groupId>
  <artifactId>solr-solrj</artifactId>
  <version>5.3.0</version>
  </dependency>
  <dependency>
  <groupId>javax.xml.stream</groupId>
  <artifactId>stax-api</artifactId>
  <version>1.0-2</version>
  </dependency>
  <dependency>
  <groupId>com.rometools</groupId>
  <artifactId>rome</artifactId>
  <version>1.8.0</version>
  </dependency>
  <dependency>
  <groupId>com.rometools</groupId>
  <artifactId>rome-utils</artifactId>
  <version>1.8.0</version>
  </dependency>
  <dependency>
  <groupId>org.jdom</groupId>
  <artifactId>jdom2</artifactId>
  <version>2.0.6</version>
  </dependency>
</dependencies>

1 Accepted Solution

Avatar

Correct answer by
Level 4

Okay now it was able to solve the issue.

I just need to add the following lib and deleted the same libs without version number. Can anyone explain whats about the libs without version? Where does the version come from?

<dependency>
  <groupId>io.wcm</groupId>
  <artifactId>io.wcm.testing.aem-mock</artifactId>
  <version>2.2.8</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>junit-addons</groupId>
  <artifactId>junit-addons</artifactId>
  <version>1.4</version>
  <scope>test</scope>
</dependency>

View solution in original post

6 Replies

Avatar

Employee Advisor

I don't find the @TestRule annotation in the code you pasted. I would assume that the error message references a different file. Can you paste this relevant file?

Avatar

Level 4

It is there :  

@Rule
   public final AemContext context = new AemContext(ResourceResolverType.JCR_MOCK);

Avatar

Level 4

Here is another example with the same error:

java: cannot access org.junit.rules.TestRule class file for org.junit.rules.TestRule not found

import com.day.cq.wcm.api.Page;
  import io.wcm.testing.mock.aem.junit.AemContext;
  import org.apache.sling.api.resource.Resource;
  import org.junit.Before;
  import org.junit.Rule;
  import org.junit.Test;

public class ExampleTest {

  @Rule
  public final AemContext context = new AemContext();

  @Before
  public void setUp() throws Exception {

  }

  @Test
  public void testSomething() {

  Resource resource = context.resourceResolver().getResource("/content/sample/en");
   Page page = resource.adaptTo(Page.class);
   // further testing
  }

}

Avatar

Correct answer by
Level 4

Okay now it was able to solve the issue.

I just need to add the following lib and deleted the same libs without version number. Can anyone explain whats about the libs without version? Where does the version come from?

<dependency>
  <groupId>io.wcm</groupId>
  <artifactId>io.wcm.testing.aem-mock</artifactId>
  <version>2.2.8</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>junit-addons</groupId>
  <artifactId>junit-addons</artifactId>
  <version>1.4</version>
  <scope>test</scope>
</dependency>

Avatar

Employee Advisor

Obviously you inherited a version number from some parent pom (just let you display the effective POM).

Jörg