2013-03-20 87 views
1

在单元/集成测试中,我试图使用RESTEasy嵌入式服务器TJWSEmbeddedJaxrsServerPOJOResourceFactory来模拟通过MockHttpRequest.get("/data")进行测试的资源调用。 我的问题是,基于服务器或资源工厂的使用,我不能够有一个非空的实例,通常在我的资源内注入的spring bean。在RESTEasy资源中向测试时注入Spring bean

下面是一些澄清的代码,在此先感谢。

Spring应用程序上下文:

<context:annotation-config /> 
<context:component-scan base-package="com.cdcfast.service" /> 
<bean id="simpleResource" class="com.cdcfast.rest.SimpleResource" /> 

SimpleResource.java:

@Component 
@Path("/data") 
@Produces(MediaType.APPLICATION_JSON) 
@Consumes(MediaType.APPLICATION_JSON) 
public class SimpleResource { 

@Autowired 
private SimpleService service; 

@GET 
@Produces(MediaType.APPLICATION_JSON) 
public List<Data> getData() { 
    return MockDataBase.getInstance().getRows(); 
} 

单元测试:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { "classpath*:/test/spring/testApplicationContext.xml" }) 
public class FakeTest { 

private Dispatcher dispatcher; 

@Before 
public void before() { 
    dispatcher = MockDispatcherFactory.createDispatcher(); 
    POJOResourceFactory noDefaults = new POJOResourceFactory(SimpleResource.class); 
    dispatcher.getRegistry().addResourceFactory(noDefaults); 
} 

@Test 
public void aTestThatAlwaysPass() throws URISyntaxException { 
    MockHttpRequest request = MockHttpRequest.get("/data"); 
    MockHttpResponse response = new MockHttpResponse(); 
    dispatcher.invoke(request, response); 
    Assertions.assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_OK); 
    Assertions.assertThat(response.getContentAsString()).isNotNull().isNotEmpty(); 
} 

} 

回答

2

之前我曾经这样做过,因为RESTEasy工厂创建POJO而不是Spring,所以它们没有连接起来,可以在整个容器中使用,但在测试中不太容易。解决这个问题的最好的办法是得到一个处理你的POJO一旦工厂创建它,然后做同样的事情到这一点:

SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(myPojo); 

我个人最后不得不春季使用的RESTEasy弹簧插件创建的RESTEasy豆类和然后使用Jetty启动我的测试,但不确定这是否适合您。

0

我exeprienced同样的问题,以类似的方式i'have解决詹姆斯那样:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { "classpath:spring-context-test.xml" }) 
public class TestMyService { 

    Dispatcher dispatcher; 
    private String username = "user"; 

    @Autowired 
    ApplicationContext context; 

    @Before 
    public void setUp() { 

    MyService g = new MyService(); //rest service with @autowired spring beans 
    context.getAutowireCapableBeanFactory().autowireBean(g); 
    dispatcher = MockDispatcherFactory.createDispatcher(); 
    dispatcher.getRegistry().addSingletonResource(g); 
    } 

    @Test 
    public void TestRest() { 
    MockHttpRequest request; 
    try { 
     request = MockHttpRequest.get("/rest/service").header("LOGON_USER", username); 
     MockHttpResponse response = new MockHttpResponse(); 

     dispatcher.invoke(request, response); 
     assertTrue("Error, unexpected status code: " + response.getStatus(), response.getStatus() == 200); 
     LoggerFactory.getLogger(this.getClass()).info("********** " + response.getContentAsString()); 
    } catch (URISyntaxException e) { 
     Log.error(e.getMessage(), e); 
     fail(e.getMessage()); 
    } 
    } 

}