title: How to get my test working with JUnit5 using SpringExtension?
body: <p>I'm trying to get my test using JUnit5, but am getting a <code>NullPointerException</code> on the auto-wired restTemplate bean.</p>
  
  <p>TestConfig:</p>
  
  <pre><code>import org.springframework.boot.web.client.RestTemplateBuilder;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.web.client.RestTemplate;
  
  @Configuration
  public class TestConfig {   
      @Bean
      public RestTemplate restTemplate(RestTemplateBuilder builder) {
          return builder.build();
      }
  }
  </code></pre>
  
  <h3>Working using JUnit4 classes:</h3>
  
  <pre><code>import static org.junit.Assert.assertTrue;
  import static org.junit.jupiter.api.Assertions.assertEquals;
  
  import org.apache.commons.lang3.math.NumberUtils;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration;
  import org.springframework.boot.test.context.SpringBootTest;
  import org.springframework.http.HttpStatus;
  import org.springframework.http.ResponseEntity;
  import org.springframework.test.context.ContextConfiguration;
  import org.springframework.test.context.junit4.SpringRunner;
  import org.springframework.web.client.RestTemplate;
  
  import lombok.extern.log4j.Log4j2;
  
  @RunWith(SpringRunner.class)
  @SpringBootTest
  @ContextConfiguration(classes = {TestConfig.class, RestTemplateAutoConfiguration.class})
  @Log4j2
  public class Test {
  
      @Value("${service.url.base}/security-marking/count")
      private String countUrl;
  
      @Autowired
      private RestTemplate restTemplate;
  
      @Test
      public void testCount() throws Exception {
          LOG.info(countUrl);
          ResponseEntity&lt;String&gt; response = restTemplate.getForEntity(countUrl, String.class);
          assertEquals(HttpStatus.OK, response.getStatusCode());
          assertTrue(NumberUtils.isDigits(response.getBody()));
          assertTrue(NumberUtils.toInt(response.getBody()) &gt;= 0);
      }
  }
  </code></pre>
  
  <h3>Fails with NullPointerException when calling restTemplate.getForEntity(...):</h3>
  
  <pre><code>import static org.junit.Assert.assertTrue;
  import static org.junit.jupiter.api.Assertions.assertEquals;
  
  import org.apache.commons.lang3.math.NumberUtils;
  import org.junit.Test;
  import org.junit.jupiter.api.extension.ExtendWith;
  import org.springframework.*.factory.annotation.Autowired;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
  import org.springframework.boot.test.context.SpringBootTest;
  import org.springframework.http.HttpStatus;
  import org.springframework.http.ResponseEntity;
  import org.springframework.test.context.junit.jupiter.SpringExtension;
  import org.springframework.web.client.RestTemplate;
  
  import lombok.extern.log4j.Log4j2;
  
  @ExtendWith(SpringExtension.class)
  @SpringBootTest(classes = {TestConfig.class, RestTemplate.class})
  @Log4j2
  @AutoConfigurationPackage
  public class Test {
  
      @Value("${service.url.base}/bob/count")
      private String countUrl;
  
      @Autowired
      private RestTemplate restTemplate;
  
      @Test
      public void testCount() throws Exception {
          LOG.info(countUrl);
          ResponseEntity&lt;String&gt; response = restTemplate.getForEntity(countUrl, String.class);
          assertEquals(HttpStatus.OK, response.getStatusCode());
          assertTrue(NumberUtils.isDigits(response.getBody()));
          assertTrue(NumberUtils.toInt(response.getBody()) &gt;= 0);
      }
  }
  </code></pre>
  
  <h3>pom.xml:</h3>
  
  <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;parent&gt;
          &lt;!-- Your own application should inherit from spring-boot-starter-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.2.5.RELEASE&lt;/version&gt;
          &lt;relativePath /&gt;
      &lt;/parent&gt;
  
      &lt;artifactId&gt;api-integration&lt;/artifactId&gt;
      &lt;groupId&gt;group.id&lt;/groupId&gt;
      &lt;name&gt;Integration Test&lt;/name&gt;
      &lt;description&gt;Integration Test&lt;/description&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  
      &lt;properties&gt;
          &lt;main.basedir&gt;${basedir}/..&lt;/main.basedir&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-integration&lt;/artifactId&gt;
          &lt;/dependency&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;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
              &lt;artifactId&gt;commons-lang3&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;!-- HELPERS --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
              &lt;artifactId&gt;lombok&lt;/artifactId&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&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;/plugins&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <h3>Stack Trace:</h3>
  
  <pre><code>java.lang.NullPointerException
      at Test.testCount(Test.java:36)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
      at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
  </code></pre>