title: How to make Spring's @Autowired to work in JUnit 5 extensions?
body: <p>I have a Spring Boot application, and I am trying to use <code>@Autowired</code> in a JUnit 5 extension. However, I cannot get it to work. (The <code>@Autowired</code> field is null.) Can anybody help?</p>
  
  <p>Below is code that demonstrates the problem I'm having (the important parts are <code>SomeExtension</code> and <code>SomeTest</code>. As written, <code>mvn test</code> causes the test to fail in <code>beforeEach</code>. Sorry if I'm including too much.</p>
  
  <p>src/test/java/somepackage/SomeExtension.java:</p>
  
  <pre><code>package somepackage;
  
  import org.junit.jupiter.api.extension.BeforeEachCallback;
  import org.junit.jupiter.api.extension.ExtendWith;
  import org.junit.jupiter.api.extension.ExtensionContext;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.test.context.SpringBootTest;
  import org.springframework.test.context.junit.jupiter.SpringExtension;
  
  import static org.junit.jupiter.api.Assertions.assertNotNull;
  
  @ExtendWith(SpringExtension.class)
  @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  public class SomeExtension implements BeforeEachCallback {
      @Autowired
      SomeBean bean;
  
      @Override
      public void beforeEach(ExtensionContext context) {
          assertNotNull(bean);
      }
  }
  </code></pre>
  
  <p>src/test/java/somepackage/SomeTest.java:</p>
  
  <pre><code>package somepackage;
  
  import org.junit.jupiter.api.Test;
  import org.junit.jupiter.api.extension.ExtendWith;
  import org.springframework.boot.test.context.SpringBootTest;
  import org.springframework.test.context.junit.jupiter.SpringExtension;
  
  
  @ExtendWith(SpringExtension.class)
  @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  @ExtendWith(SomeExtension.class)
  class SomeTest {
      @Test
      void nothingTest() {
      }
  }
  </code></pre>
  
  <p>src/main/java/somepackage/SomeBean.java</p>
  
  <pre><code>package somepackage;
  
  import org.springframework.stereotype.Component;
  
  @Component
  public class SomeBean {
  }
  </code></pre>
  
  <p>src/main/java/somepackage/MainClass.java</p>
  
  <pre><code>package somepackage;
  
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  
  @SpringBootApplication
  public class MainClass {
      public static void main(String[] args) {
          SpringApplication.run(MainClass.class, args);
      }
  }
  </code></pre>
  
  <p>pom.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;fooGroupId&lt;/groupId&gt;
      &lt;artifactId&gt;barArtifactId&lt;/artifactId&gt;
      &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;2.0.3.RELEASE&lt;/version&gt;
      &lt;/parent&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
              &lt;exclusions&gt;
                  &lt;exclusion&gt;
                      &lt;!-- Don't include Junit 4 --&gt;
                      &lt;groupId&gt;junit&lt;/groupId&gt;
                      &lt;artifactId&gt;junit&lt;/artifactId&gt;
                  &lt;/exclusion&gt;
              &lt;/exclusions&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
              &lt;artifactId&gt;junit-jupiter-engine&lt;/artifactId&gt;
              &lt;version&gt;5.2.0&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;properties&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
      &lt;/properties&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
  
              &lt;plugin&gt;
                  &lt;!--
                  This is copied from https://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven
                  This allows the surefire plugin to be able to find Junit 5 tests, so `mvn test` works.
                  --&gt;
                  &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.21.0&lt;/version&gt;
                  &lt;dependencies&gt;
                      &lt;dependency&gt;
                          &lt;groupId&gt;org.junit.platform&lt;/groupId&gt;
                          &lt;artifactId&gt;junit-platform-surefire-provider&lt;/artifactId&gt;
                          &lt;version&gt;1.2.0&lt;/version&gt;
                      &lt;/dependency&gt;
                  &lt;/dependencies&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
      &lt;repositories&gt;
          &lt;repository&gt;
              &lt;id&gt;spring-releases&lt;/id&gt;
              &lt;url&gt;https://repo.spring.io/libs-release&lt;/url&gt;
          &lt;/repository&gt;
      &lt;/repositories&gt;
      &lt;pluginRepositories&gt;
          &lt;pluginRepository&gt;
              &lt;id&gt;spring-releases&lt;/id&gt;
              &lt;url&gt;https://repo.spring.io/libs-release&lt;/url&gt;
          &lt;/pluginRepository&gt;
      &lt;/pluginRepositories&gt;
  
  &lt;/project&gt;
  </code></pre>
  
  <p>I'm also having similar issues with <code>@Value</code>. If the solution also works for that, it would be great.</p>
  
  <p>Thank you.</p>