JerseyTest with shiro

≡放荡痞女 提交于 2019-12-11 05:56:25

问题


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


回答1:


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:

  • Javadoc for ServletDeploymentContext.
  • Source code test examples


来源:https://stackoverflow.com/questions/43558564/jerseytest-with-shiro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!