junit (Arquillian REST扩展)为什么测试方法中的webtarget为空?

k97glaaz  于 2022-11-11  发布在  其他
关注(0)|答案(2)|浏览(116)

我尝试使用“ArquillianResteasyResource”在测试中注入WebTarget,但WebTarget的变量仍然为空。

@Test  
@Consumes(MediaType.APPLICATION_JSON)  
public void testWithWT(@ArquillianResteasyResource WebTarget webTarget) {  
    ......}

当我直接注入我的类服务时,一切都工作得很好!

@Test  
@Consumes(MediaType.APPLICATION_JSON)  
public void testWithWT(@ArquillianResteasyResource MyService sv) {  
......}

我的宠物依赖:

<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
  </dependency>
  <dependency>
      <groupId>org.eu.ingwar.tools</groupId>
      <artifactId>arquillian-suite-extension</artifactId>
      <version>1.1.2</version>
      <scope>test</scope>
  </dependency>
  <dependency>
      <groupId>org.jboss.arquillian.junit</groupId>
      <artifactId>arquillian-junit-container</artifactId>
      <scope>test</scope>
  </dependency>
  <dependency>
      <groupId>org.wildfly.arquillian</groupId>
      <artifactId>wildfly-arquillian-container-embedded</artifactId>
      <version>1.0.1.Final</version>
      <scope>test</scope>
  </dependency>
  <dependency>
      <groupId>org.jboss.arquillian.extension</groupId>
      <artifactId>arquillian-rest-client-api</artifactId>
      <version>1.0.0.Alpha3</version>
      <scope>test</scope>
  </dependency>
  <dependency>
      <groupId>org.jboss.arquillian.extension</groupId>
      <artifactId>arquillian-rest-client-impl-3x</artifactId>
      <version>1.0.0.Alpha3</version>
      <scope>test</scope>
  </dependency>
  <dependency>
      <groupId>org.wildfly</groupId>
      <artifactId>wildfly-embedded</artifactId>
      <version>9.0.0.Final</version>
      <scope>test</scope>
  </dependency>

WebTarget有什么问题?
有什么主意吗?
顺祝商祺

piah890a

piah890a1#

这个答案可能有点晚了,但是如果你想在测试中注入webTarget,你必须在测试方法中添加@RunAsClient

@Test  
@Consumes(MediaType.APPLICATION_JSON)
@RunAsClient  
public void testWithWT(@ArquillianResteasyResource WebTarget webTarget) {}
7hiiyaii

7hiiyaii2#

我已经使用rest-extension和一个嵌入的Glassfish示例创建了一个sample project on GitHub here,希望能有所帮助。

相关问题