title: Cannot use Autowired in a test class but able to use it in an implementation class?
body: <p>I'm trying to testing a @Controller class where there is a List from a pojo's.
  I can use @Autowired in another @RestController class to wire the controller, but in the same situation I can't do it into a Test.</p>
  
  <p><strong>User.java</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>@Data //from lombok
  public class User  {
      private String Id;
      private String email;
  
      //constructor + getters and setters
  }
  
  </code></pre>
  
  <p><strong>UserContrell3.java</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>@Controller
  public class UserController {
  
      private List&lt;User&gt; userRepo = new ArrayList&lt;&gt;();
  
      public UserController() {}
  
      public List&lt;User&gt; readAll() {
          return userRepo;
      }
  
      public void add(User user) {
          userRepo.add(user);
      }
  }
  </code></pre>
  
  <p>In the next class, <strong>UserResource.java</strong> <strong>I can do @Autowired</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>@RestController
  @RequestMapping(UserResource.USER)
  public class UserResource {
      public static final String USER = "/users";
  
      private UserController userController;
  
      @Autowired
      public UserResource(UserController userController) {
          this.userController = userController;
      }
  
      @GetMapping
      public List&lt;User&gt; readAll() {
          return userController.readAll();
      }
  
      @PostMapping
      public void addUser(@RequestBody User user) {
          userController.add(user);
      }
  }
  
  </code></pre>
  
  <p>However, into the test class <strong>UserTest.java</strong>. I can't:</p>
  
  <pre class="lang-java prettyprint-override"><code>@SpringBootTest(classes = {cat.jhz.Main.MainApp.class})
  @TestPropertySource("classpath:test.properties")
  public class UserTest {
  
      // @Autowired **//NOT HERE** --&gt; ERROR
      private UserController userRepo;
  
      @Autowired **//AND NOT HERE** --&gt; ERROR
      public UserResource(UserController userController) {
          this.userController = userController;
      }
  
      @Test
      void testAddUserToUserController() {
          ...
      }
  
  }
  </code></pre>
  
  <p>The error message is <strong>Could not autowire. No beans of 'UserController' type found.</strong></p>
  
  <p>Any idea?</p>
  
  <p>Thanks in advance!</p>