Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

Commit 4411ed0

Browse files
committed
Refine Mockito exclude filter to provide meaningful errors
See gh-1343
1 parent 894e683 commit 4411ed0

File tree

3 files changed

+47
-22
lines changed

3 files changed

+47
-22
lines changed

spring-aot-test/src/main/java/org/springframework/aot/test/boot/SpringBootTestInfrastructureExcludeFilter.java

-21
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.springframework.boot.test.mock.mockito;
2+
3+
import java.util.Set;
4+
5+
import org.springframework.aot.context.bootstrap.generator.BeanDefinitionExcludeFilter;
6+
import org.springframework.beans.factory.config.BeanDefinition;
7+
8+
/**
9+
* A {@link BeanDefinitionExcludeFilter} that removes unsupported test infrastructure like Mockito.
10+
*
11+
* @author Sebastien Deleuze
12+
*/
13+
class MockitoExcludeFilter implements BeanDefinitionExcludeFilter {
14+
15+
private static final String MOCKITO_POST_PROCESSOR_BEAN_NAME = "org.springframework.boot.test.mock.mockito.MockitoPostProcessor";
16+
17+
private static final String MOCKITO_SPY_POST_PROCESSOR_BEAN_NAME = MOCKITO_POST_PROCESSOR_BEAN_NAME + "$SpyPostProcessor";
18+
19+
@Override
20+
public boolean isExcluded(String beanName, BeanDefinition beanDefinition) {
21+
boolean isExcluded = false;
22+
if (MOCKITO_POST_PROCESSOR_BEAN_NAME.equals(beanName)) {
23+
new MockitoBeanDefinitionChecker().check(beanDefinition);
24+
isExcluded = true;
25+
}
26+
else if (MOCKITO_SPY_POST_PROCESSOR_BEAN_NAME.equals(beanName)) {
27+
isExcluded = true;
28+
}
29+
return isExcluded;
30+
}
31+
32+
static class MockitoBeanDefinitionChecker {
33+
34+
private void check(BeanDefinition beanDefinition) {
35+
Set<Definition> definitions = (Set) beanDefinition.getConstructorArgumentValues().getArgumentValue(0, Set.class).getValue();
36+
for (Definition definition : definitions) {
37+
if (definition instanceof MockDefinition) {
38+
throw new IllegalStateException("@MockBean is not supported yet by Spring AOT and has been detected on type " + ((MockDefinition)definition).getTypeToMock());
39+
}
40+
if (definition instanceof SpyDefinition) {
41+
throw new IllegalStateException("@SpyBean is not supported yet by Spring AOT and has been detected on type " + ((SpyDefinition)definition).getTypeToSpy());
42+
}
43+
}
44+
}
45+
}
46+
}

spring-aot-test/src/main/resources/META-INF/spring.factories

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
org.springframework.aot.context.bootstrap.generator.BeanDefinitionExcludeFilter=\
2-
org.springframework.aot.test.boot.SpringBootTestInfrastructureExcludeFilter
2+
org.springframework.boot.test.mock.mockito.MockitoExcludeFilter
33

44
org.springframework.aot.test.context.bootstrap.generator.AotTestContextProcessor=\
55
org.springframework.aot.test.boot.SpringBootAotTestContextProcessor,\

0 commit comments

Comments
 (0)