Skip to content

Commit 7b82597

Browse files
author
nigel.zheng
committed
feat: verify @MockTokenServices on test class
1 parent fbbe360 commit 7b82597

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.github.ahunigel.test.security.oauth2;
2+
3+
import org.springframework.boot.test.mock.mockito.MockBean;
4+
import org.springframework.boot.test.mock.mockito.MockReset;
5+
import org.springframework.security.oauth2.provider.token.ResourceServerTokenServices;
6+
7+
/**
8+
* Created by Nigel Zheng on 8/7/2018.
9+
*/
10+
@MockBean(value = {ResourceServerTokenServices.class}, reset = MockReset.NONE)
11+
public @interface MockTokenServices {
12+
}

src/main/java/com/github/ahunigel/test/security/oauth2/WithToken.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.github.ahunigel.test.security.oauth2;
22

3-
import org.springframework.boot.test.mock.mockito.MockBean;
4-
import org.springframework.boot.test.mock.mockito.MockReset;
53
import org.springframework.security.oauth2.provider.token.ResourceServerTokenServices;
64
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;
75

@@ -12,10 +10,11 @@
1210
* Created by Nigel Zheng on 2018/8/7.
1311
* <p>
1412
* Emulate an OAuth2 token request, would extract an {@link PreAuthenticatedAuthenticationToken}
13+
* <p>
14+
* require annotation {@link MockTokenServices} on test class level or {@link ResourceServerTokenServices} @MockBean exists
1515
*
1616
* @author nigel
1717
*/
18-
@MockBean(value = {ResourceServerTokenServices.class}, reset = MockReset.NONE)
1918
@Retention(RetentionPolicy.RUNTIME)
2019
public @interface WithToken {
2120

src/main/java/com/github/ahunigel/test/security/oauth2/WithTokenTestExecutionListener.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public class WithTokenTestExecutionListener extends AbstractTestExecutionListene
4646
public void beforeTestClass(TestContext testContext) throws Exception {
4747
Annotation annotation = AnnotatedElementUtils.findMergedAnnotation(testContext.getTestClass(), WithToken.class);
4848
if (annotation != null) {
49+
verifyTokenServicesMocked(testContext.getTestClass());
4950
addAuthHeader(testContext.getTestClass(), testContext, (WithToken) annotation);
5051
}
5152
}
@@ -54,6 +55,7 @@ public void beforeTestClass(TestContext testContext) throws Exception {
5455
public void beforeTestMethod(TestContext testContext) throws Exception {
5556
Annotation annotation = AnnotatedElementUtils.findMergedAnnotation(testContext.getTestMethod(), WithToken.class);
5657
if (annotation != null) {
58+
verifyTokenServicesMocked(testContext.getTestClass());
5759
addAuthHeader(testContext.getTestMethod(), testContext, (WithToken) annotation);
5860
}
5961
}
@@ -74,6 +76,11 @@ public void afterTestClass(TestContext testContext) throws Exception {
7476
}
7577
}
7678

79+
private void verifyTokenServicesMocked(Class<?> testClass) {
80+
MockTokenServices annotation = AnnotatedElementUtils.findMergedAnnotation(testClass, MockTokenServices.class);
81+
Assert.state(annotation != null, "Missing @MockTokenServices on class level");
82+
}
83+
7784
private void addAuthHeader(AnnotatedElement annotated, TestContext testContext, WithToken withToken) {
7885
Assert.state(withToken != null, "No @WithToken exists!!!");
7986
MockMvc mockMvc = testContext.getApplicationContext().getBean(MockMvc.class);

0 commit comments

Comments
 (0)