title: How to ignore @PreAuthorize("isAuthenticated()") annotation in Unit Test?
body: <p>I wrote test class:</p>
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @SpringBootTest
  @WebAppConfiguration
  @Transactional
  public class ProfileTest {
  
  @Autowired
  private UserService userService;
  
  @Autowired
  private ProfileService profileService;
  
  @Autowired
  private InterestService interestService;
  
  private SiteUser[] users = {
          new SiteUser(&quot;test1@testing.com&quot;, &quot;testPass&quot;),
          new SiteUser(&quot;test2@testing.com&quot;, &quot;testPass&quot;),
          new SiteUser(&quot;test3@testing.com&quot;, &quot;testPass&quot;),
  };
  
  private String[][] interests = {
          {&quot;music&quot;, &quot;guitar_xxxxxx&quot;, &quot;plants&quot;},
          {&quot;music&quot;, &quot;music&quot;, &quot;philosophy_19&quot;},
          {&quot;random&quot;, &quot;football&quot;},
  };
  
  @Test
  public void testInterests() {
      
      for(int i=0; i&lt;users.length; i++) {
          SiteUser user = users[i];
          String[] interestArray = interests[i];
          
          userService.register(user);
          
          HashSet&lt;Interest&gt; interestSet = new HashSet&lt;&gt;();
          
          for(String interestText : interestArray) {
              Interest interest = interestService.createIfNotExists(interestText);
              interestSet.add(interest);
              
              assertNotNull(&quot;interest should not be null&quot;, interest);
              assertNotNull(&quot;Interest should have ID&quot;, interest.getId());
              assertEquals(&quot;Text should match&quot;, interestText, interest.getName());
          }
          
          Profile profile = new Profile(user);
          profile.setInterests(interestSet);
          profileService.save(profile); // ERROR 1
          
          Profile retrievedProfile = profileService.getUserProfile(user); // ERROR 2
          
          assertEquals(&quot;Interest sets should match&quot;, interestSet, retrievedProfile.getInterests());
      }
  }
  
  }
  </code></pre>
  <p>But this error happens on lines that have comments <strong>ERROR 1</strong> and <strong>ERROR 2</strong> and I found out why is that happening however I don't know how to fix it or if there's any workaround.</p>
  <p>Problem is in here:</p>
  <pre><code>@Service
  public class ProfileService {
  
  @Autowired
  ProfileDao profileDao;
  
  @PreAuthorize(&quot;isAuthenticated()&quot;) // ERROR 1
  public void save(Profile profile) {
      profileDao.save(profile);
  }
  
  @PreAuthorize(&quot;isAuthenticated()&quot;) // ERROR 2
  public Profile getUserProfile(SiteUser user) {
      return profileDao.findByUser(user);
  }
  }
  </code></pre>
  <p>These '@PreAuthorize(&quot;isAuthenticated()&quot;)' annotations are causing the problem, because when I try to use those two methods in my UnitTest, I'm getting this exception: <em>(AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext)</em>:</p>
  <pre><code>org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext
  at org.springframework.security.access.intercept.AbstractSecurityInterceptor.credentialsNotFound(AbstractSecurityInterceptor.java:379)
  at org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:223)
  at org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:65)
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
  at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:747)
  at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689)
  at com.socialnetwork.service.ProfileService$$EnhancerBySpringCGLIB$$22c09751.save(&lt;generated&gt;)
  at com.socialnetwork.tests.ProfileTest.testInterests(ProfileTest.java:72)
  at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  </code></pre>
  <p>And if I remove these annotations, my test passes. So do you know how can I fix my tests?</p>