【问题标题】:Spring REST Docs setup MockMvc with static configuration classSpring REST Docs 使用静态配置类设置 MockMvc
【发布时间】:2019-06-15 22:53:01
【问题描述】:

Spring REST Docs 参考指南使用自动装配的 ApplicationContext 设置 MockMvc:

@Before
public void setUp() {
    this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
        .apply(documentationConfiguration(this.restDocumentation))
        .alwaysDo(document("{method-name}/{step}/")).build();
}

this 博客中描述了如何使用静态@Configuration 类。 然而,使用这样一个静态类,自动装配的上下文不是从这个类创建的上下文。似乎这个类被忽略了。

有没有办法把这两者结合起来?

【问题讨论】:

  • 您必须向我们展示您的班级级别配置(即注释),以便我们进一步为您提供帮助。

标签: spring spring-test mockmvc spring-test-mvc


【解决方案1】:

我已经更新了我的配置,它现在按预期工作。

@ContextConfiguration
@RunWith(SpringRunner.class)
public class SpringTest {

  @Autowired
  private WebApplicationContext webApplicationContext;

  private MockMvc mockMvc;

  @Before
  public void setUp() {
    this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
  }

  ... test methods ...

  @Configuration
  @ComponentScan(basePackages = "my.package")
  @Import(SecurityConfiguration.class)
  public static class Config {

    @Bean
    @Qualifier("myService")
    ... replace implementation of myService with test implementation...
  }
}

现在 MockMvc 按预期配置了从静态配置类创建的应用程序上下文。

【讨论】:

    【解决方案2】:

    或者你可以只使用@AutoConfigureRestDocs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 2018-04-13
      相关资源
      最近更新 更多