在测试时在RESTEasy资源中注入Spring bean

时间:2013-03-20 10:22:54

标签: rest junit resteasy spring-test

在单元/集成测试中,我正在尝试使用RESTEasy嵌入式服务器TJWSEmbeddedJaxrsServerPOJOResourceFactory来模拟MockHttpRequest.get("/data")资源调用以进行测试。 我的问题是,基于服务器或资源工厂的使用,我无法在我的资源中正常注入一个非空的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 个答案:

答案 0 :(得分:2)

之前我已经有了这个,因为RESTEasy工厂创建了POJO而不是Spring,因此它们没有连线,可以在整个容器中解决,但在测试中不太容易。解决这个问题的最佳方法是在工厂创建POJO后处理它,然后执行类似的操作:

SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(myPojo);

我个人最终让Spring使用RESTEasy-Spring插件创建RESTEasy bean,然后使用Jetty启动我的测试,但不确定这是否适合您。

答案 1 :(得分:0)

我出现了同样的问题,我的解决方式和詹姆斯一样:

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

}