title: Spring Boot junit testing secured application
body: <p>I am using in my application Spring Security OAuth2. When I try to test the service layer with JUnit tests I get this error:</p>
  
  <blockquote>
    <p>org.springframework.security.authentication.AuthenticationCredentialsNotFoundException:
    An Authentication object was not found in the SecurityContext</p>
  </blockquote>
  
  <p>Can you tell me how to fix it?</p>
  
  <p>Here is my Test class:</p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @SpringBootTest
  @Transactional
  public class FooServiceTest {
  
      @Autowired
      private FooService service;
  
      @Test
      public void someTest() {    
          service.findFoo(1L);
      }
  }
  </code></pre>