繁体   English   中英

请求范围的 bean 在使用 Cucumber 的 Spring 测试中不起作用

[英]Request-scoped beans not working in Spring tests with Cucumber

我有一个基于 Spring 4.3.28 的应用程序(即不是 Spring Boot。),我想将我的集成测试迁移到 Cucumber。

我已经按照本教程进行了调整,并将其改编为普通的 Spring。

到目前为止,我编写的测试工作正常(Spring 上下文已初始化等),但是一旦涉及请求范围的 bean,它们就会停止工作:

Caused by: java.lang.IllegalStateException: No thread-bound request found: Are you
referring to request attributes outside of an actual web request, or processing a 
request outside of the originally receiving thread? If you are actually operating 
within a web request and still receive this message, your code is probably running 
outside of DispatcherServlet/DispatcherPortlet: In this case, use 
RequestContextListener or RequestContextFilter to expose the current request.

我创建了一个尝试重现该问题的小型示例项目

有一个名为 AppConfig 的上下文配置 class:


@Configuration
public class AppConfig {
   @Bean
   @Scope("request“) // when this line is removed, the test succeeds
   public ExampleService exampleService() {
      return new ExampleService();
   }

   @Bean("dependency")
   @Scope("request") // when this line is removed, the test succeeds
   public String dependencyBean() {
      return "dependency bean";
   }
}

ExampleService 是请求范围的,并获得一个由@Autowired 注入的请求范围的 bean:

public class ExampleService {

  @Autowired
  @Qualifier("dependency")
  String dependencyBean;

  public String process() { return "I have a "+dependencyBean; }
}

对于测试,我有一个带有 Spring 注释的超类:

@ContextConfiguration(classes = AppConfig.class)
@CucumberContextConfiguration
@WebAppConfiguration
public class TestBase {

  @Autowired
  public ExampleService underTest;
}

还有一个简单的 Spring 测试运行得很好:

@RunWith(SpringRunner.class)
public class ExampleServicePlainSpringTest extends TestBase {

  @Test
  public void whenProcessingDataThenResultShouldBeReturned() {
    assertThat(this.underTest.process()).isEqualTo("I have a dependency bean");
  }

}

Cucumber 测试由该测试 class 存根执行:

@RunWith(Cucumber.class)
public class ExampleServiceCucumberTest extends TestBase {}

实际的 cucumber 步骤定义在这里:

public class CucumberStepDefinitions extends TestBase {

  private String result;

  @When("I process data")
  public void iProcessData() {
    result = this.underTest.process();
  }

  @Then("the result should be returned")
  public void checkResult() {
    assertThat(result).isEqualTo("I have a dependency bean");
  }
}

Cucumber 的 .feature 文件位于 src/test/resources 目录下,package 名称与步骤定义 class 相同:

Feature: Example

  Scenario: Example service bean returns dependency
    When I process data
    Then the result should be returned

通常当我遇到“no thread-bound request found”错误时,这是因为缺少@WebAppConfiguration注解,或者当我尝试将请求范围的 bean 注入到非请求范围的 bean 中时。 但这里不是这样。 我究竟做错了什么?

我能够弄清楚如何解决它; 更新的代码位于问题中链接的 github 存储库中。

使用SpringRunner时,请求上下文在 ServletTestExecutionListener 中初始化,该ServletTestExecutionListener隐式添加到测试的TestExecutionListener列表中。 初始化发生在该侦听器的beforeTestMethod()方法中。

但是,正如@MPKorsanje 在评论中正确指出的那样(谢谢),Cucumber 没有测试方法,所以beforeTestMethod()永远不会执行。

我的解决方案是添加ServletTestExecutionListener的自定义子类作为TestExecutionListener ,它将beforeTestClass()调用委托给beforeTestMethod()

public class ClassLevelServletTestExecutionListener extends ServletTestExecutionListener {

  @Override
  public void beforeTestClass(TestContext testContext) throws Exception {
    super.beforeTestMethod(testContext);
  }

  @Override
  public void afterTestClass(TestContext testContext) throws Exception {
    super.afterTestMethod(testContext);
  }
}

ExampleServiceCucumberTest中:

@ContextConfiguration(classes = {AppConfig.class})
@CucumberContextConfiguration
@WebAppConfiguration
@TestExecutionListeners(ClassLevelServletTestExecutionListener.class)
// extend the Spring class to get the default TestExecutionListeners
public class TestBase extends AbstractJUnit4SpringContextTests {

  @Autowired
  public ExampleService underTest;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM