Skip to content

Commit e0e553f

Browse files
committed
Make cache key include @MockBean on enclosing class if @nested
1 parent 220f8cd commit e0e553f

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/MockitoContextCustomizerFactory.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import java.util.List;
2020

21+
import org.springframework.core.annotation.AnnotatedElementUtils;
2122
import org.springframework.test.context.ContextConfigurationAttributes;
2223
import org.springframework.test.context.ContextCustomizer;
2324
import org.springframework.test.context.ContextCustomizerFactory;
@@ -26,6 +27,7 @@
2627
* A {@link ContextCustomizerFactory} to add Mockito support.
2728
*
2829
* @author Phillip Webb
30+
* @author Andy Wilkinson
2931
*/
3032
class MockitoContextCustomizerFactory implements ContextCustomizerFactory {
3133

@@ -35,8 +37,16 @@ public ContextCustomizer createContextCustomizer(Class<?> testClass,
3537
// We gather the explicit mock definitions here since they form part of the
3638
// MergedContextConfiguration key. Different mocks need to have a different key.
3739
DefinitionsParser parser = new DefinitionsParser();
38-
parser.parse(testClass);
40+
parseDefinitions(testClass, parser);
3941
return new MockitoContextCustomizer(parser.getDefinitions());
4042
}
4143

44+
private void parseDefinitions(Class<?> testClass, DefinitionsParser parser) {
45+
parser.parse(testClass);
46+
if (testClass.getEnclosingClass() != null && AnnotatedElementUtils
47+
.isAnnotated(testClass, "org.junit.jupiter.api.Nested")) {
48+
parseDefinitions(testClass.getEnclosingClass(), parser);
49+
}
50+
}
51+
4252
}

0 commit comments

Comments
 (0)