1

在单元/集成测试中,我尝试使用 RESTEasy 嵌入式服务器TJWSEmbeddedJaxrsServer或为了通过资源调用进行POJOResourceFactory模拟以进行测试。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" />

简单资源.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();
}

}
4

2 回答 2

2

我以前有过这个,因为 RESTEasy 工厂创建 POJO 而不是 Spring,所以它们不会被连接起来,这可以在完整的容器中解决,但在测试中不太容易。解决此问题的最佳方法是在工厂创建 POJO 后获取它的句柄,然后执行类似于此的操作:

SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(myPojo);

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

于 2013-03-20T10:36:29.123 回答
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());
    }
    }

}
于 2016-09-29T08:26:43.240 回答