2015-06-02 57 views
1

CXF肥皂应用不工作,使用下列版本:CXF请求范围的bean在单元测试(肥皂)

springBootVersion = 1.2.3.RELEASE 
springVersion  = '4.1.6.RELEASE' 
cxfVersion  = '3.1.0' 
junitVersion  = '4.12' 

我有一个弹簧豆与请求范围:

@Component 
@Scope(value=WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS) 
public class RequestScopedClass 

我取从我的CXF端点实现中的ApplicationContext动态获取:

@Component 
@WebService(endpointInterface = "ch.xyz.PaymentServiceInterface") 
public class PaymentServiceImpl implements PaymentServiceInterface 
{ 
    ... 
    RequestScopedClass rsc = appCtxt.getBean(RequestScopedClass.class); 
    rsc.doSomething(); 

我的目标是测试肥皂的前端通过模拟一个连接到侦听器端口的客户端来实现服务,确保执行包含拦截器链(包括我的自定义拦截器)的整个cxf堆栈。 )

String address = "http://0.0.0.0:8080/"; 
myEndpoint = Endpoint.publish(address, new PaymentServiceImpl()); 

运行测试中调用抛出BeanCreationException到rsc.doSomething(:

Error creating bean with name 'scopedTarget.requestScopedEnvironment': Scope 'request' is not active ... 
我设置由包括

org.apache.cxf:cxf-rt-transports-http-jetty 

依赖,并开始在测试设置端点管理该配置

如果我将proxyMode更改为其他三种可能性之一,则在从appCtxt获取bean时已经引发了相同的异常。

考试是由

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes = { 
           ..., 
           RequestScopedClass.class 
          } 
) 
@WebAppConfiguration 

如果应用程序是通过命令行“gradle这个bootrun”和SOAP请求启动由铬邮递员应用完成这一切是好的注解,我得到预期的SOAP响应。

我该怎么做,在单元测试中执行cxf soap服务器时有一个有效的请求范围?

回答

1

同时,我找到了解决办法:

... 
import org.springframework.context.ConfigurableApplicationContext; 

@Autowired 
private ConfigurableApplicationContext myCtxt; 


@Before 
public void setUp() throws Throwable 
{ 
    myCtxt.getBeanFactory().registerScope("session", new CustomScope4Test()); 
    myCtxt.getBeanFactory().registerScope("request", new CustomScope4Test());   
} 

public class CustomScope4Test implements Scope 
{ 

    private final Map<String, Object> beanMap = new HashMap<String, Object>(); 

    /** 
    * @see org.springframework.beans.factory.config.Scope#get(java.lang.String, org.springframework.beans.factory.ObjectFactory) 
    */ 
    public Object get(String name, ObjectFactory<?> factory) 
    { 
     Object bean = beanMap.get(name); 
     if (null == bean) 
     { 
      bean = factory.getObject(); 
      beanMap.put(name, bean); 
     } 
     return bean; 
    } 

    /** 
    * @see org.springframework.beans.factory.config.Scope#getConversationId() 
    */ 
    public String getConversationId() 
    { 
     // not needed 
     return null; 
    } 

    /** 
    * @see org.springframework.beans.factory.config.Scope#registerDestructionCallback(java.lang.String, java.lang.Runnable) 
    */ 
    public void registerDestructionCallback(String arg0, Runnable arg1) 
    { 
     // not needed 
    } 

    /** 
    * @see org.springframework.beans.factory.config.Scope#remove(java.lang.String) 
    */ 
    public Object remove(String obj) 
    { 
     return beanMap.remove(obj); 
    } 

    /** 
    * @see org.springframework.beans.factory.config.Scope#resolveContextualObject(java.lang.String) 
    */ 
    public Object resolveContextualObject(String arg0) 
    { 
     // not needed 
     return null; 
    } 
}