title: Spring: mocking security enhanced service
body: <p>We'are imlementing part of our security at service layer, so I add <code>@PreAuthorize</code> annotation to some methods of <code>MyService</code>. </p>
  
  <p>At <code>MyServiceSecurityTest</code> I want to test only security role-permission matrix, without any business logic. For that reason I have to mock <code>MyService</code>. the problem is that both Mockito and Spring security use CGLIB proxies, and my service is not enhanced with <code>@PreAuthorize</code> after <code>Mockito.mock(MyService.class)</code>.</p>
  
  <p><em>Is there any way to mock service and preserve <code>@PreAuthorize</code> logic?</em></p>
  
  <hr>
  
  <p>Example:</p>
  
  <pre><code>@Service
  public class MyService implements IMyService {
  
      @Override
      @PreAuthorize("hasAuthority('SYSOP')")
      public void someMethod(ComplexDTO dto) {
          // lots of logic and dependencies, require lots of stubbing.
      }
  }
  </code></pre>
  
  <p>In order to avoid initialization of all dependencies of <code>MyService#someMethod</code> and building <code>ComplexDTO</code> at <code>MyServiceSecurityTest</code> I want to mock <code>MyServiceSecurityTest</code> but preserve <code>@PreAuthorize</code> checks.</p>