2017-04-22 67 views
1

實例化JerseyTest與此類JerseyTest與四郎

public class NMAAbstractRestTest extends JerseyTest { 

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

} 

NMAdaptorApplication構造。

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(); 

} 

在執行以下的試驗方法

@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); 
} 

得到以下例外

16:34:23.893 ERROR [Grizzly的HTTP服務器 - 0] crrneNMRestExceptionMapper - 捕獲到異常在Mapper中:沒有SecurityManager可以被調用代碼訪問,或者綁定到org.apache.shiro.util.ThreadContext或者作爲一個vm靜態單例。這是一個無效的應用程序配置。 org.apache.shiro.UnavailableSecurityManagerException:調用代碼無法訪問SecurityManager,無論是綁定到org.apache.shiro.util.ThreadContext還是作爲vm靜態單例。這是一個無效的應用程序配置。 在org.apache.shiro.SecurityUtils.getSecurityManager(SecurityUtils.java:123) 在org.apache.shiro.subject.Subject $生成器。(Subject.java:627)

我猜它因我在web.xml中配置的Shiro過濾器

<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 failed。是否有任何選項在JerseyTest中包含web.xml

回答

2

我不知道這是否會解決問題,因爲我不使用Shiro,但是如果你想配置的東西,否則會在網絡上配置.xml,你需要做更多的工作。

首先需要在servlet容器中運行測試。假設,你使用的灰熊(注意,這是不可能的使用內存供應商),你需要使用GrizzlyWebTestContainerFactory

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

然後,你需要配置DeploymentContext。在這裏,你可以讓你的servlet容器配置

@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(); 
} 

參見:

+0

完美,沒有解決但如今卻需要額外四郎CONFIGS。希望我能解決它。一旦完成,我會更新。 – vels4j