title: ServletContext in JUnit is missing in PostContruct-Method
body: <p>I'm one of the newbies to spring. So my problem is this, that I have a RestController like this, which is using a third lib, that uses static methods:</p>
  <pre><code>@RestController
  @RequestMapping(&quot;/my/rest/controller&quot;)
  public class MyRestController
  {
      @PostConstruct
      public void initThirdLib()
      {
          ThirdLibClassA.doSomeInitStuff();
      }
      
      // methods with @RequestMapping
  }
  </code></pre>
  <p>This works fine, as I ran the MVC app itself. But when I run the Tests it gives an Exception from my <code>initThirdLib()</code>-Method, which is going back, that the third-lib inside wants a valid object of <code>javax.servlet.ServletContext</code>. I've found the way to provide it to the lib by calling a method <code>ThirdLibClassB.setContext( servletContext )</code>.</p>
  <p>But when I do so, I found out, that the <code>@PostConstruct</code>-method is called before my test mocking-setup is called.</p>
  <p>My test looks like:</p>
  <pre><code>@ExtendWith(SpringExtension.class)
  @SpringBootTest(webEnvironment = WebEnvironment.MOCK)
  class MyTest 
  {
      @Autowired
      private WebApplicationContext wac;
  
      private MockMvc mockMvc;
  
      @BeforeEach
      void setup()
      {
          this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
          ServletContext servletContext = this.wac.getServletContext();
          ThirdLibClassB.setContext( servletContext );
          wac.getBean( MyRestController.class ).initThirdLib();
      }
  }
  </code></pre>
  <p>If I catch the Exception from <code>initThirdLib()</code> it still will be thrown while <code>@PostConstruct</code> and will success within the manualy call withing the test.</p>
  <p>But is there a way to provide the <code>servletContext</code> before the <code>@PostConstruct</code>-call to avoid the Exception-Handling?</p>