2013-03-20 83 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()); 
    } 
    } 

}