0

Spring REST Docs reference guides to setting up MockMvc using autowired ApplicationContext:

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

and in this blog it is described how to use static @Configuration classes. However using such a static class the autowired context is not the context created from this class. It seems this class is ignored.

Is there a way to combine these two?

user1622058
  • 453
  • 3
  • 7
  • 16
  • You'll have to show us your class-level configuration (i.e., annotations) in order for us to assist you further. – Sam Brannen Jan 24 '19 at 13:49

2 Answers2

0

I have updated my configuration and it is working as expected now.

@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...
  }
}

Now the MockMvc is configured with application context created from static config class as expected.

user1622058
  • 453
  • 3
  • 7
  • 16
0

Or you can just use @AutoConfigureRestDocs

Fazli Zekiqi
  • 531
  • 2
  • 7
  • 14