1

Instantiating JerseyTest with this class

public class NMAAbstractRestTest extends JerseyTest {

  @Override
  protected Application configure() {
    NMAdaptorApplication application = new NMAdaptorApplication();
    return application;
  }

}

NMAdaptorApplication constructor.

public NMAdaptorApplication() {
    log.info("NM-Adaptor-Application is being initilized....");
    register(NMRestExceptionMapper.class);
    register(ShiroFeature.class);
    register(NMMarshallingFeature.class);
    register(new LoggingFeature(java.util.logging.Logger.getLogger("nma"), LoggingFeature.Verbosity.PAYLOAD_ANY));
    packages(true, "com.retailwave.rwos.nma.rest.resource");
    loadProperties();

}

while executing the following test method

@Test
public void makeOrder()
{  
   ...
   Entity entity = Entity.entity(orderContent, MediaType.APPLICATION_JSON);
   WebTarget target = target("customerorders");
   target.path("");
   target.queryParam("cmd", "create");
   target.queryParam("branchCode", "610");
   arget.property(HttpAuthenticationFeature.HTTP_AUTHENTICATION_BASIC_USERNAME, "admin");
   target.property(HttpAuthenticationFeature.HTTP_AUTHENTICATION_BASIC_PASSWORD, "admin");
   Response response = target.request().post(entity);
}

getting the following exception

16:34:23.893 ERROR [grizzly-http-server-0] c.r.r.n.e.NMRestExceptionMapper - Exception caught at Mapper : No SecurityManager accessible to the calling code, either bound to the org.apache.shiro.util.ThreadContext or as a vm static singleton. This is an invalid application configuration. org.apache.shiro.UnavailableSecurityManagerException: No SecurityManager accessible to the calling code, either bound to the org.apache.shiro.util.ThreadContext or as a vm static singleton. This is an invalid application configuration. at org.apache.shiro.SecurityUtils.getSecurityManager(SecurityUtils.java:123) at org.apache.shiro.subject.Subject$Builder.(Subject.java:627)

I guess its due to Shiro filter which I configured in web.xml

 <filter>
    <filter-name>ShiroFilter</filter-name>
    <filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>
 </filter>

<filter-mapping>
    <filter-name>ShiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
 </filter-mapping>

while JUnit test this fails. Is there any option to include web.xml in JerseyTest

vels4j
  • 11,208
  • 5
  • 38
  • 63

1 Answers1

2

I don't know if this will solve the problem, as I don't use Shiro, but if you want to configure things that would otherwise be configured in the web.xml, you need to do a little more work.

First need to run the test in a servlet container. Assuming, you're using Grizzly (note, this is not possible using the in-memory provider), you need to use the GrizzlyWebTestContainerFactory

@Override
protected TestContainerFactory getTestContainerFactory() {
    return new GrizzlyWebTestContainerFactory();
}

Then you need to configure the DeploymentContext. This is where you can make your servlet container configurations

@Override
protected DeploymentContext configureDeployment() {
    // just configure the ResourceConfig here instead of the `configure`.
    final ResourceConfig config = new ResourceConfig();
    return ServletDeploymentContext
            .forServlet(new ServletContainer(config))
            .addFilter(ShiroFilter.class, "ShiroFilter")
            .build();
}

See also:

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Perfect, not resolved yet but now it requires additional shiro configs. Hope I can resolve it. I will update once I'm done. – vels4j Apr 22 '17 at 14:48
  • were you able to find the solution? I have the same issue and I thought the whole purpose of ShiroFeature was to initialize context listener and filter instead of using web.xml – Tono Wiedermann May 01 '20 at 10:22