【问题标题】:Inject Spring bean within RESTEasy Resource at Test time在测试时在 RESTEasy 资源中注入 Spring bean
【发布时间】:2013-03-20 10:22:53
【问题描述】:

在单元/集成测试中,我尝试使用 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();
}

}

【问题讨论】:

    标签: rest junit resteasy spring-test


    【解决方案1】:

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

    SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(myPojo);
    

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

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题,我已经以与 James 类似的方式解决了:

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

      【讨论】:

        猜你喜欢
        • 2012-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-27
        • 2022-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多