title: Spring wiring NullPointerException in JUnit test class
body: <p>I have written a JUnit class for my Bean <code>Task</code>. The first test, <code>testTaskShouldNotBeNull</code>, passes OK. However the second one, <code>testTaskTitle</code>, throws a NullPointerException. The <code>title</code> field is being set in the JavaConfig for my bean so I am unsure why it is null.</p>
  
  <p>My <code>Task</code> bean:</p>
  
  <pre><code>package com.webapps.ToDoList;
  import org.springframework.stereotype.Component;
  
  @Component
  public class Task {
  
      private String title;
  
      public void setTitle(String title) {
          this.title = title;
      }
  
      public String getTitle() {
          return title;
      }
  }
  </code></pre>
  
  <p>My JavaConfig:</p>
  
  <pre><code>package com.webapps.ToDoList;
  
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  
  @Configuration
  public class TaskConfig {
  
      @Bean
      public Task task() {
          Task task = new Task();
          task.setTitle("A Generic Title!");
          return task;
      }
  }
  </code></pre>
  
  <p>And my JUnit test class:</p>
  
  <pre><code>package com.webapps.ToDoList;
  
  import org.springframework.test.context.ContextConfiguration;
  import junit.framework.TestCase;
  
  @ContextConfiguration(classes = TaskConfig.class)
  public class TaskTest extends TestCase {
  
      private Task task;
  
      public void testTaskShouldNotBeNull() {
          assertNull(task);
      }
  
      public void testTaskTitle() {
          assertNull(task.getTitle());
      }
  }
  </code></pre>
  
  <p>Update:</p>
  
  <p>I have tried adding the <code>@Autowired</code> annotation to the <code>Task</code> variable in the test class, which made no difference.</p>
  
  <p>Perhaps my file structure is causing an issue? </p>
  
  <p><a href="https://i.stack.imgur.com/D99lX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/D99lX.png" alt="enter image description here"></a></p>
  
  <p>After adding <code>@Test</code> and <code>@RunWith(SpringRunner.class)</code> annotations I am getting the following result:</p>
  
  <pre><code>junit.framework.AssertionFailedError
  at junit.framework.Assert.fail(Assert.java:55)
  at junit.framework.Assert.assertTrue(Assert.java:22)
  at junit.framework.Assert.assertNotNull(Assert.java:256)
  at junit.framework.Assert.assertNotNull(Assert.java:248)
  at junit.framework.TestCase.assertNotNull(TestCase.java:417)
  at com.webapps.ToDoList.TaskTest.testTaskShouldNotBeNull(TaskTest.java:26)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)
  at junit.framework.TestCase.runTest(TestCase.java:176)
  at junit.framework.TestCase.runBare(TestCase.java:141)
  at junit.framework.TestResult$1.protect(TestResult.java:122)
  at junit.framework.TestResult.runProtected(TestResult.java:142)
  at junit.framework.TestResult.run(TestResult.java:125)
  at junit.framework.TestCase.run(TestCase.java:129)
  at junit.framework.TestSuite.runTest(TestSuite.java:252)
  at junit.framework.TestSuite.run(TestSuite.java:247)
  at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:121)
  at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
  </code></pre>