title: Why @MockBean does not mock my target repository class (returns null)?
body: <p>Here is the class I want to test</p>
  <pre><code>@Component
  public class PermissionCheck {
  
      @Autowired
      private MyEntityRepository myEntityRepository;
  
      public boolean hasPermission(int myEntityID) {
          MyEntity myEntity = myEntityRepository.findById(myEntityId);
          return myEntity != null;
      }
  }
  </code></pre>
  <p>Here is the test class</p>
  <pre><code>@RunWith(SpringRunner.class)
  public class PermissionCheckTests {
  
      @MockBean
      private MyEntityRepository myEntityRepository;
  
      private PermissionCheck permissionCheck;
  
      @Before
      public void before() {
          this.permissionCheck = new PermissionCheck();
      }
  
      @Test
      public void shouldHasPermission() {
          MyEntity myEntity = new MyEntity();
  
          when(this.myEntityRepository.findById(any())).thenReturn(myEntity);
          assertTrue(this.permissionCheck.hasPermission(0));
      }
  }
  </code></pre>
  <p>And when I run this test I got</p>
  <pre><code>java.lang.NullPointerException
      at PermissionCheck.hasPermission(PermissionCheck.java:line1)
      at PermissionCheckTests.shouldHasPermission(PermissionCheckTests.java:line2)
  </code></pre>
  <p>In the above, line1 and line2 refer to these two lines</p>
  <pre><code>        MyEntity myEntity = myEntityRepository.findById(myEntityId);
  </code></pre>
  <pre><code>        assertTrue(this.permissionCheck.hasPermission(0));
  </code></pre>
  <p>And using debugger I see that when entering <code>PermissionCheck.hasPermission</code> from <code>PermissionCheckTests.shouldHasPermission</code>, the repository field</p>
  <pre><code>    @Autowired
      private MyEntityRepository myEntityRepository;
  </code></pre>
  <p>is null.</p>
  <p>I created these classes by referring to others existing codes, from different places, and without really understanding how to the annotations (partially due to running out of time), so if someone can tell me not only how to fix, but also <em>why</em> I'm wrong, I would really appreciate it!</p>
  <p>Edit:</p>
  <p>I made the change suggested by @Nikolas Charalambidis (thank you!), so my <code>PermissionCheck</code> class now looks exactly like</p>
  <pre><code>@RunWith(SpringRunner.class)
  public class PermissionCheckTests {
  
      @Autowired                                     // you need to autowire
      private PermissionCheck permissionCheck;       // and it uses @MockBean dependency
  
      @MockBean                                      // if no such @MockBean exists
      private MyEntityRepository myEntityRepository; // the real implementation is used
  
      @Test
      public void shouldHasPermission() {
          MyEntity myEntity = new MyEntity();
  
          when(this.myEntityRepository.findById(any())).thenReturn(myEntity);
          assertTrue(this.permissionCheck.hasPermission(0));
      }
  }
  </code></pre>
  <p>But I then got the following exception</p>
  <pre><code>org.springframework.beans.factory.UnsatisfiedDependencyException: 
  Error creating bean with name 'PermissionCheckTests': 
  Unsatisfied dependency expressed through field 'permissionCheck'; 
  nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
  No qualifying bean of type 'PermissionCheck' available: 
  expected at least 1 bean which qualifies as autowire candidate. 
  Dependency annotations: 
  {@org.springframework.beans.factory.annotation.Autowired(required=true), 
  @org.springframework.beans.factory.annotation.Qualifier(value=&quot;&quot;)}
  </code></pre>
  <p>With a little bit search, from <a href="https://stackoverflow.com/a/20333975/10349064">this SO answer</a>, I feel like I should not @Autowired <code>PermissionCheck</code>, since it is a class, not an interface.</p>
  <p>Does that mean I have to create an interface, @Autowired it and let <code>PermissionCheck</code> implement it? It seems redundant to me, since I don't see why I need such an interface. Is there a way to make it work without creating a new interface which I will solely use for this purpose?</p>