Gustavo Doriguetto created SLING-7182:
-----------------------------------------

             Summary: No OSGi SCR metadata found on test case
                 Key: SLING-7182
                 URL: https://issues.apache.org/jira/browse/SLING-7182
             Project: Sling
          Issue Type: Test
          Components: Testing
    Affects Versions: Testing Sling Mock 2.2.10
            Reporter: Gustavo Doriguetto


I have the following servlet, referencing a "QueryBuilder" service

{code:java}
import com.day.cq.search.PredicateGroup;
import com.day.cq.search.Query;
import com.day.cq.search.QueryBuilder;
import com.day.cq.search.result.Hit;
import com.day.cq.search.result.SearchResult;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@Component(service=Servlet.class,
           property={
                   Constants.SERVICE_DESCRIPTION + "=Simple Demo Servlet",
                   "sling.servlet.methods=" + HttpConstants.METHOD_GET,
                   "sling.servlet.resourceTypes="+ 
"bb/components/structure/page",
                   "sling.servlet.extensions=" + "json"
           })
public class SimpleServlet extends SlingSafeMethodsServlet {

    private static final long serialVersionUid = 1L;

    @Reference
    QueryBuilder queryBuilder;

    @Override
    protected void doGet(final SlingHttpServletRequest req,
            final SlingHttpServletResponse resp) throws ServletException, 
IOException {

        resp.setContentType("application/json");

        Map<String, String> tilesMap = new HashMap<String, String>();

        tilesMap.put("path", req.getResource().getPath());

        Query result = 
queryBuilder.createQuery(PredicateGroup.create(tilesMap), 
req.getResourceResolver().adaptTo(Session.class));

        SearchResult searchResult = result.getResult();

        JSONObject json = new JSONObject();
        try {
            JSONArray jsonArray = new JSONArray();
            for(Hit hit: searchResult.getHits()) {
                JSONObject item = new JSONObject();
                item.put("text", hit.getProperties().get("text", String.class));
                jsonArray.put(item);
            }
            json.put("items", jsonArray);
        } catch (JSONException | RepositoryException e) {
            // Servlet failures should always return an approriate HTTP Status 
code
            resp.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            // If you do not set your own HTML Response content, the OOTB 
HATEOS Response is used
            resp.getWriter().write("ERROR");
            e.printStackTrace();
        }

        PrintWriter out = resp.getWriter();
        out.print(json.toString());

    }
}
{code}

with the following test case:


{code:java}

import com.day.cq.search.QueryBuilder;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.apache.sling.testing.mock.sling.junit.SlingContext;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import javax.servlet.ServletException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;

@RunWith(MockitoJUnitRunner.class)
public class SimpleServletTest {

    @Rule
    public final SlingContext slingContext = new 
SlingContext(ResourceResolverType.RESOURCERESOLVER_MOCK);

    @Mock
    QueryBuilder queryBuilder;

    SimpleServlet simpleServlet;

    @Before
    public void setup() {

        slingContext.request().setMethod(HttpConstants.METHOD_GET);
        slingContext.requestPathInfo().setExtension("json");

        Map<Object, Object> config = new HashMap<Object, Object>();
        config.put("queryBuilder", queryBuilder);

        slingContext.registerInjectActivateService(new SimpleServletTest(), 
config);
        simpleServlet = slingContext.getService(SimpleServlet.class);

    }

    @Test
    public void testSampleServletRequest() throws IOException, ServletException 
{
        simpleServlet.doGet(slingContext.request(),slingContext.response());
        assertEquals(302,slingContext.response().getStatus());
    }

}
{code}

After a run, i'm getting the following error:

org.apache.sling.testing.mock.osgi.NoScrMetadataException: No OSGi SCR metadata 
found for class aa.core.servlets.SimpleServletTest

        at 
org.apache.sling.testing.mock.osgi.OsgiServiceUtil.injectServices(OsgiServiceUtil.java:381)
        at 
org.apache.sling.testing.mock.osgi.MockOsgi.injectServices(MockOsgi.java:148)
        at 
org.apache.sling.testing.mock.osgi.context.OsgiContextImpl.registerInjectActivateService(OsgiContextImpl.java:153)
        at 
org.apache.sling.testing.mock.osgi.context.OsgiContextImpl.registerInjectActivateService(OsgiContextImpl.java:168)
        at aa.core.servlets.SimpleServletTest.setup(SimpleServletTest.java:42)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at 
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
        at 
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at 
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
        at 
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
        at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
        at org.junit.rules.RunRules.evaluate(RunRules.java:20)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
        at 
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
        at 
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at 
org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:68)
        at 
org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:74)
        at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
        at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:161)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
        at 
com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
        at 
com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
        at 
com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
        at 
com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

I'm using OSGI declarative service annotation:
osgi.core: 6.0.0




--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to