2013-08-14 3 views
0

У нас есть проблема с нашими тестами, что поле UriInfo неправильно введено, когда ресурс завернут в TransactionalProxy. Мы попытались использовать SpringResourceFactory, но это тоже не помогло.RESTeasy InMemoryClient не вводит поле @Context UriInfo в Spring @Transactional Bean

Я попытался извлечь соответствующие классы для этого USECASE:


public class InMemoryClientFactory implements FactoryBean<InMemoryClientExecutor>{ 

    @Inject 
    private SessionResource sessionResource; 

    @Override 
    public InMemoryClientExecutor getObject() throws Exception { 
     Dispatcher dispatcher = MockDispatcherFactory.createDispatcher(); 
     Registry registry = dispatcher.getRegistry(); 
     registry.addSingletonResource(sessionResource); 
     final InMemoryClientExecutor inMemoryClientExecutor = new InMemoryClientExecutor(dispatcher); 
    } 

    @Override 
    public Class getObjectType() { 
     return InMemoryClientExecutor.class; 
    } 

    @Override 
    public boolean isSingleton() { 
     return true; 
    } 
} 

@Path("session") 
public interface SessionResource { 

    @GET 
    @Path("{sessionId}") 
    @Produces({MediaType.APPLICATION_XML}) 
    Response get(@PathParam("sessionId") String sessionId); 

    @DELETE 
    @Path("{sessionId}") 
    Response delete(@PathParam("sessionId") String sessionId); 
} 

@Service 
@Transactional 
public class SessionResourceImpl implements SessionResource { 

    @Context 
    private UriInfo uriInfo; 

    @Override 
    public Response get(String sessionId) { 
     // uriInfo will be null here 
     String url = uriInfo.getBaseUriBuilder().path(SessionResource.class).path(SessionResource.class, "delete").build(sessionId) 
       .toString()); 

     return Response.ok(session).build(); 

    @Override 
    public Response delete(String sessionId) { 
     System.out.println("Deleted Session "+1); 
    } 
} 


@ContextConfiguration(locations = ["classpath:/META-INF/testContext.xml"]) 
@Transactional 
@RunWith(SpringJUnit4ClassRunner.class) 
public class SessionResourceIT { 

    @Inject 
    InMemoryRestClientFactory inMemoryClientFactory; 

    @Inject 
    SessionResource resource; 

    @Test 
    public void test() { 
     SessionResource resource = inMemoryClientFactory.createProxy(SessionResource.class); 

     ClientResponse cr = client.get(sessionId); 
     assertNotNull(cr.getEntity(String.class)); 
    } 
} 

ответ

0

Возможным решением является разворачивать транзакционный прокси-сервер для тестирования, это работает до тех пор, как Сам тест аннотируется с @Transactional. Надеюсь, у кого-то есть лучшее решение, чем это.


public class InMemoryClientFactory implements FactoryBean<InMemoryClientExecutor>{ 

    @Inject 
    private SessionResource sessionResource; 

    @Override 
    public InMemoryClientExecutor getObject() throws Exception { 
     Dispatcher dispatcher = MockDispatcherFactory.createDispatcher(); 
     Registry registry = dispatcher.getRegistry(); 
     registry.addSingletonResource(unwrapProxy(sessionResource)); 
     final InMemoryClientExecutor inMemoryClientExecutor = new InMemoryClientExecutor(dispatcher); 
    } 

    @Override 
    public Class getObjectType() { 
     return InMemoryClientExecutor.class; 
    } 

    @Override 
    public boolean isSingleton() { 
     return true; 
    } 

    private static Object unwrapProxy(Object bean) throws Exception { 
     Object result = bean; 
     /* 
     * If the given object is a proxy, set the return value as the object 
     * being proxied, otherwise return the given object. 
     */ 
     if (AopUtils.isAopProxy(bean) && bean instanceof Advised) { 
      Advised advised = (Advised) bean; 
      result = advised.getTargetSource().getTarget(); 
     } 
     return result; 
    } 
} 
Смежные вопросы