1
0
mirror of synced 2026-08-05 17:57:15 +00:00

Fix mockito UnnecessaryStubbingException

This commit is contained in:
Rob Winch
2021-07-08 14:52:45 -05:00
parent 2a62c4d976
commit b6ff4d3674
22 changed files with 252 additions and 42 deletions
@@ -61,6 +61,9 @@ public class DefaultMethodSecurityExpressionHandlerTests {
@BeforeEach
public void setup() {
this.handler = new DefaultMethodSecurityExpressionHandler();
}
private void setupMocks() {
given(this.methodInvocation.getThis()).willReturn(new Foo());
given(this.methodInvocation.getMethod()).willReturn(Foo.class.getMethods()[0]);
}
@@ -77,6 +80,7 @@ public class DefaultMethodSecurityExpressionHandlerTests {
@Test
public void createEvaluationContextCustomTrustResolver() {
setupMocks();
this.handler.setTrustResolver(this.trustResolver);
Expression expression = this.handler.getExpressionParser().parseExpression("anonymous");
EvaluationContext context = this.handler.createEvaluationContext(this.authentication, this.methodInvocation);
@@ -87,6 +91,7 @@ public class DefaultMethodSecurityExpressionHandlerTests {
@Test
@SuppressWarnings("unchecked")
public void filterByKeyWhenUsingMapThenFiltersMap() {
setupMocks();
final Map<String, String> map = new HashMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
@@ -104,6 +109,7 @@ public class DefaultMethodSecurityExpressionHandlerTests {
@Test
@SuppressWarnings("unchecked")
public void filterByValueWhenUsingMapThenFiltersMap() {
setupMocks();
final Map<String, String> map = new HashMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
@@ -121,6 +127,7 @@ public class DefaultMethodSecurityExpressionHandlerTests {
@Test
@SuppressWarnings("unchecked")
public void filterByKeyAndValueWhenUsingMapThenFiltersMap() {
setupMocks();
final Map<String, String> map = new HashMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
@@ -139,6 +146,7 @@ public class DefaultMethodSecurityExpressionHandlerTests {
@Test
@SuppressWarnings("unchecked")
public void filterWhenUsingStreamThenFiltersStream() {
setupMocks();
final Stream<String> stream = Stream.of("1", "2", "3");
Expression expression = this.handler.getExpressionParser().parseExpression("filterObject ne '2'");
EvaluationContext context = this.handler.createEvaluationContext(this.authentication, this.methodInvocation);
@@ -150,6 +158,7 @@ public class DefaultMethodSecurityExpressionHandlerTests {
@Test
public void filterStreamWhenClosedThenUpstreamGetsClosed() {
setupMocks();
final Stream<?> upstream = mock(Stream.class);
doReturn(Stream.<String>empty()).when(upstream).filter(any());
Expression expression = this.handler.getExpressionParser().parseExpression("true");
@@ -78,10 +78,6 @@ public class UserDetailsRepositoryReactiveAuthenticationManagerTests {
@BeforeEach
public void setup() {
this.manager = new UserDetailsRepositoryReactiveAuthenticationManager(this.userDetailsService);
given(this.scheduler.schedule(any())).willAnswer((a) -> {
Runnable r = a.getArgument(0);
return Schedulers.immediate().schedule(r);
});
}
@Test
@@ -91,6 +87,10 @@ public class UserDetailsRepositoryReactiveAuthenticationManagerTests {
@Test
public void authentiateWhenCustomSchedulerThenUsed() {
given(this.scheduler.schedule(any())).willAnswer((a) -> {
Runnable r = a.getArgument(0);
return Schedulers.immediate().schedule(r);
});
given(this.userDetailsService.findByUsername(any())).willReturn(Mono.just(this.user));
given(this.encoder.matches(any(), any())).willReturn(true);
this.manager.setScheduler(this.scheduler);
@@ -64,6 +64,10 @@ public class DelegatingSecurityContextCallableTests {
@SuppressWarnings("serial")
public void setUp() throws Exception {
this.originalSecurityContext = SecurityContextHolder.createEmptyContext();
this.executor = Executors.newFixedThreadPool(1);
}
private void givenDelegateCallWillAnswerWithCurrentSecurityContext() throws Exception {
given(this.delegate.call()).willAnswer(new Returns(this.callableResult) {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
@@ -72,7 +76,6 @@ public class DelegatingSecurityContextCallableTests {
return super.answer(invocation);
}
});
this.executor = Executors.newFixedThreadPool(1);
}
@AfterEach
@@ -104,12 +107,14 @@ public class DelegatingSecurityContextCallableTests {
@Test
public void call() throws Exception {
givenDelegateCallWillAnswerWithCurrentSecurityContext();
this.callable = new DelegatingSecurityContextCallable<>(this.delegate, this.securityContext);
assertWrapped(this.callable);
}
@Test
public void callDefaultSecurityContext() throws Exception {
givenDelegateCallWillAnswerWithCurrentSecurityContext();
SecurityContextHolder.setContext(this.securityContext);
this.callable = new DelegatingSecurityContextCallable<>(this.delegate);
// ensure callable is what sets up the SecurityContextHolder
@@ -120,6 +125,7 @@ public class DelegatingSecurityContextCallableTests {
// SEC-3031
@Test
public void callOnSameThread() throws Exception {
givenDelegateCallWillAnswerWithCurrentSecurityContext();
this.originalSecurityContext = this.securityContext;
SecurityContextHolder.setContext(this.originalSecurityContext);
this.callable = new DelegatingSecurityContextCallable<>(this.delegate, this.securityContext);
@@ -139,6 +145,7 @@ public class DelegatingSecurityContextCallableTests {
@Test
public void createNullSecurityContext() throws Exception {
givenDelegateCallWillAnswerWithCurrentSecurityContext();
SecurityContextHolder.setContext(this.securityContext);
this.callable = DelegatingSecurityContextCallable.create(this.delegate, null);
// ensure callable is what sets up the SecurityContextHolder
@@ -148,6 +155,7 @@ public class DelegatingSecurityContextCallableTests {
@Test
public void create() throws Exception {
givenDelegateCallWillAnswerWithCurrentSecurityContext();
this.callable = DelegatingSecurityContextCallable.create(this.delegate, this.securityContext);
assertWrapped(this.callable);
}
@@ -63,11 +63,14 @@ public class DelegatingSecurityContextRunnableTests {
@BeforeEach
public void setUp() {
this.originalSecurityContext = SecurityContextHolder.createEmptyContext();
this.executor = Executors.newFixedThreadPool(1);
}
private void givenDelegateRunWillAnswerWithCurrentSecurityContext() {
willAnswer((Answer<Object>) (invocation) -> {
assertThat(SecurityContextHolder.getContext()).isEqualTo(this.securityContext);
return null;
}).given(this.delegate).run();
this.executor = Executors.newFixedThreadPool(1);
}
@AfterEach
@@ -99,12 +102,14 @@ public class DelegatingSecurityContextRunnableTests {
@Test
public void call() throws Exception {
givenDelegateRunWillAnswerWithCurrentSecurityContext();
this.runnable = new DelegatingSecurityContextRunnable(this.delegate, this.securityContext);
assertWrapped(this.runnable);
}
@Test
public void callDefaultSecurityContext() throws Exception {
givenDelegateRunWillAnswerWithCurrentSecurityContext();
SecurityContextHolder.setContext(this.securityContext);
this.runnable = new DelegatingSecurityContextRunnable(this.delegate);
SecurityContextHolder.clearContext(); // ensure runnable is what sets up the
@@ -115,6 +120,7 @@ public class DelegatingSecurityContextRunnableTests {
// SEC-3031
@Test
public void callOnSameThread() throws Exception {
givenDelegateRunWillAnswerWithCurrentSecurityContext();
this.originalSecurityContext = this.securityContext;
SecurityContextHolder.setContext(this.originalSecurityContext);
this.executor = synchronousExecutor();
@@ -135,6 +141,7 @@ public class DelegatingSecurityContextRunnableTests {
@Test
public void createNullSecurityContext() throws Exception {
givenDelegateRunWillAnswerWithCurrentSecurityContext();
SecurityContextHolder.setContext(this.securityContext);
this.runnable = DelegatingSecurityContextRunnable.create(this.delegate, null);
SecurityContextHolder.clearContext(); // ensure runnable is what sets up the
@@ -144,6 +151,7 @@ public class DelegatingSecurityContextRunnableTests {
@Test
public void create() throws Exception {
givenDelegateRunWillAnswerWithCurrentSecurityContext();
this.runnable = DelegatingSecurityContextRunnable.create(this.delegate, this.securityContext);
assertWrapped(this.runnable);
}