public class OAuth2ContextSetup extends org.junit.rules.TestWatchman
A rule that sets up an OAuth2 context for tests and makes the access token available
inside a test method. In combination with the OAuth2ContextConfiguration
annotation provides a number of different strategies for configuring an
OAuth2ProtectedResourceDetails
instance that will be used to create the OAuth2
context for tests. Example:
@OAuth2ContextConfiguration(ResourceOwnerPasswordProtectedResourceDetails.class) public class MyIntegrationTests implements RestTemplateHolder { @Rule public OAuth2ContextSetup context = OAuth2ContextSetup.withEnvironment(this, TestEnvironment.instance()); @Test public void testSomethingWithClientCredentials() { // This call will be authenticated with the client credentials in // MyClientDetailsResource getRestTemplate().getForObject("https://myserver/resource", String.class); } // This class is used to initialize the OAuth2 context for the test methods. static class MyClientDetailsResource extends ResourceOwnerPasswordProtectedResourceDetails { public MyClientDetailsResource(Environment environment) { ... do stuff with environment to initialize the password credentials } } }
OAuth2ContextConfiguration
,
BeforeOAuth2Context
failed, succeeded
public static OAuth2ContextSetup withEnvironment(RestTemplateHolder clientHolder, org.springframework.core.env.Environment environment)
clientHolder
- receives an OAuth2RestTemplate with the authenticated client
for the duration of a testenvironment
- a Spring Environment that can be used to initialize the clientpublic static OAuth2ContextSetup withTestAccounts(RestTemplateHolder clientHolder, TestAccounts testAccounts)
TestAccounts
instance into its
protected resource details.clientHolder
- receives an OAuth2RestTemplate with the authenticated client
for the duration of a testtestAccounts
- a test account generator that can be used to initialize the
clientpublic static OAuth2ContextSetup standard(RestTemplateHolder clientHolder)
static class MyClientDetailsResource extends ClientCredentialsProtectedResourceDetails { public MyClientDetailsResource(Object target) { MyIntegrationTests test = (MyIntegrationTests) target; ... do stuff with test instance to initialize the client credentials } }
clientHolder
- receives an OAuth2RestTemplate with the authenticated client
for the duration of a testpublic org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base, org.junit.runners.model.FrameworkMethod method, Object target)
apply
in interface org.junit.rules.MethodRule
apply
in class org.junit.rules.TestWatchman
public void starting(org.junit.runners.model.FrameworkMethod method)
starting
in class org.junit.rules.TestWatchman
public void finished(org.junit.runners.model.FrameworkMethod method)
finished
in class org.junit.rules.TestWatchman
public void setAccessTokenProvider(AccessTokenProvider accessTokenProvider)
public OAuth2AccessToken getAccessToken()
@OAuth2ContextConfiguration
.public OAuth2RestTemplate getRestTemplate()
public OAuth2ProtectedResourceDetails getResource()
public AccessTokenRequest getAccessTokenRequest()
public OAuth2ClientContext getOAuth2ClientContext()
Copyright © 2019. All rights reserved.