title: Getting NotAMockException on the below Test Case
body: <p>I have tried to run the below test and am facing <code>NotAMock</code> Exception and not sure how to resolve it. I have been trying to read the concept that methods of class under test cannot be mocked but I am unable to come clear on the subject. If someone could explain me the Why on my own example, I am hopeful of understanding it better.</p>
  <p>I tried various ways of changing <code>@RunWith</code> runners for Unit or Integration test setup or using <code>@Spy</code> instead of <code>@Mock</code> or not have <code>@Autowired</code> etc but either was facing dao Null Pointer or Not a Mock Exception variably.</p>
  <p>Am I supposed to Use another class and inject the <code>Listener</code> in that class and mock the listener to achieve the functionality of being able to mock the methods and capture the arguments dynamically. Will this work because it is no more the class under test and therefore the methods could be mocked? If so, how is this realized. If not, what is the right way. My sense is moving the listener to another class will only extend my current set of issues of not being able to mock but does not resolve it. However, I am not sure what is the right outcome.</p>
  <pre><code>@Component
  public class FileEventListener implements ApplicationListener&lt;FileEvent&gt; {
  
      @Autowired private FetchFileDetailsDAO fileDao;//Dao is annotated with @Transactional
  
      @Override
      public void onApplicationEvent(FileEvent event) {
          fileDao.getDetailsForFile(event.fileName())
      }     
  }
  -----------------------------------------------------------------------------------------
  
  import org.junit.Before;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.mockito.ArgumentCaptor;
  import org.mockito.Captor;
  import org.mockito.InjectMocks;
  import org.mockito.Mock;
  import org.mockito.Mockito;
  import org.mockito.MockitoAnnotations;
  import org.springframework.boot.test.context.SpringBootTest;
  import org.springframework.test.annotation.Rollback;
  import org.springframework.test.context.junit4.SpringRunner;
  import org.springframework.transaction.annotation.Transactional;
  
  @SpringBootTest(classes = TestApp.class)
  @RunWith(SpringRunner.class)
  public class TestClass {  
    
  @Captor private ArgumentCaptor&lt;Object&gt; captor;
  @Mock @Autowired private FetchFileDetailsDAO dao;
  @InjectMocks @Autowired private FileEventListener listener;
  
  @Before
  public void setup() throws IOException {
      MockitoAnnotations.initMocks(this);
  }
  
  @Test
  @Transactional
  @Rollback(true)
  public void test() throws Exception {
      FileEvent ev = new FileEvent();
      ...
      listener.onApplicationEvent(ev);
      verify(dao, times(1)).getDetailsForFile((String)captor.capture())
  }
  </code></pre>