Support @⁠MockitoSpyBean at the type level on test classes

Prior to this commit, @⁠MockitoSpyBean could only be declared on fields
within test classes, which prevented developers from being able to
easily reuse spy configuration across a test suite.

With this commit, @⁠MockitoSpyBean is now supported at the type level
on test classes, their superclasses, and interfaces implemented by
those classes. @⁠MockitoSpyBean is also supported on enclosing classes
for @⁠Nested test classes, their superclasses, and interfaces
implemented by those classes, while honoring @⁠NestedTestConfiguration
semantics.

In addition, @⁠MockitoSpyBean:

- has a new `types` attribute that can be used to declare the type or
  types to spy when @⁠MockitoSpyBean is declared at the type level

- can be declared as a repeatable annotation at the type level

- can be declared as a meta-annotation on a custom composed annotation
  which can be reused across a test suite (see the @⁠SharedSpies
  example in the reference manual)

To support these new features, this commit also includes the following
changes.

- MockitoSpyBeanOverrideProcessor has been revised to support
  @⁠MockitoSpyBean at the type level.

- The "Bean Overriding in Tests" and "@⁠MockitoBean and
  @⁠MockitoSpyBean" sections of the reference manual have been fully
  revised.

See gh-34408
Closes gh-33925
This commit is contained in:
Sam Brannen
2025-02-11 17:46:01 +01:00
parent b336bbe539
commit e31ce359a1
37 changed files with 1106 additions and 208 deletions

View File

@@ -107,91 +107,197 @@ class MockitoBeanOverrideProcessorTests {
class CreateHandlersTests {
@Test
void missingTypes() {
Class<?> testClass = MissingTypesTestCase.class;
MockitoBean annotation = testClass.getAnnotation(MockitoBean.class);
void otherAnnotationThrows() {
Annotation annotation = getClass().getAnnotation(Nested.class);
assertThatIllegalStateException()
.isThrownBy(() -> processor.createHandlers(annotation, testClass))
.withMessage("The @MockitoBean 'types' attribute must not be empty when declared on a class");
.isThrownBy(() -> processor.createHandlers(annotation, getClass()))
.withMessage("Invalid annotation passed to MockitoBeanOverrideProcessor: expected either " +
"@MockitoBean or @MockitoSpyBean on test class %s", getClass().getName());
}
@Test
void nameNotSupportedWithMultipleTypes() {
Class<?> testClass = NameNotSupportedWithMultipleTypesTestCase.class;
MockitoBean annotation = testClass.getAnnotation(MockitoBean.class);
@Nested
class MockitoBeanTests {
assertThatIllegalStateException()
.isThrownBy(() -> processor.createHandlers(annotation, testClass))
.withMessage("The @MockitoBean 'name' attribute cannot be used when mocking multiple types");
@Test
void missingTypes() {
Class<?> testClass = MissingTypesTestCase.class;
MockitoBean annotation = testClass.getAnnotation(MockitoBean.class);
assertThatIllegalStateException()
.isThrownBy(() -> processor.createHandlers(annotation, testClass))
.withMessage("The @MockitoBean 'types' attribute must not be empty when declared on a class");
}
@Test
void nameNotSupportedWithMultipleTypes() {
Class<?> testClass = NameNotSupportedWithMultipleTypesTestCase.class;
MockitoBean annotation = testClass.getAnnotation(MockitoBean.class);
assertThatIllegalStateException()
.isThrownBy(() -> processor.createHandlers(annotation, testClass))
.withMessage("The @MockitoBean 'name' attribute cannot be used when mocking multiple types");
}
@Test
void singleMockByType() {
Class<?> testClass = SingleMockByTypeTestCase.class;
MockitoBean annotation = testClass.getAnnotation(MockitoBean.class);
List<BeanOverrideHandler> handlers = processor.createHandlers(annotation, testClass);
assertThat(handlers).singleElement().isInstanceOfSatisfying(MockitoBeanOverrideHandler.class, handler -> {
assertThat(handler.getField()).isNull();
assertThat(handler.getBeanName()).isNull();
assertThat(handler.getBeanType().resolve()).isEqualTo(Integer.class);
});
}
@Test
void singleMockByName() {
Class<?> testClass = SingleMockByNameTestCase.class;
MockitoBean annotation = testClass.getAnnotation(MockitoBean.class);
List<BeanOverrideHandler> handlers = processor.createHandlers(annotation, testClass);
assertThat(handlers).singleElement().isInstanceOfSatisfying(MockitoBeanOverrideHandler.class, handler -> {
assertThat(handler.getField()).isNull();
assertThat(handler.getBeanName()).isEqualTo("enigma");
assertThat(handler.getBeanType().resolve()).isEqualTo(Integer.class);
});
}
@Test
void multipleMocks() {
Class<?> testClass = MultipleMocksTestCase.class;
MockitoBean annotation = testClass.getAnnotation(MockitoBean.class);
List<BeanOverrideHandler> handlers = processor.createHandlers(annotation, testClass);
assertThat(handlers).satisfiesExactly(
handler1 -> {
assertThat(handler1.getField()).isNull();
assertThat(handler1.getBeanName()).isNull();
assertThat(handler1.getBeanType().resolve()).isEqualTo(Integer.class);
},
handler2 -> {
assertThat(handler2.getField()).isNull();
assertThat(handler2.getBeanName()).isNull();
assertThat(handler2.getBeanType().resolve()).isEqualTo(Float.class);
}
);
}
@MockitoBean
static class MissingTypesTestCase {
}
@MockitoBean(name = "bogus", types = { Integer.class, Float.class })
static class NameNotSupportedWithMultipleTypesTestCase {
}
@MockitoBean(types = Integer.class)
static class SingleMockByTypeTestCase {
}
@MockitoBean(name = "enigma", types = Integer.class)
static class SingleMockByNameTestCase {
}
@MockitoBean(types = { Integer.class, Float.class })
static class MultipleMocksTestCase {
}
}
@Test
void singleMockByType() {
Class<?> testClass = SingleMockByTypeTestCase.class;
MockitoBean annotation = testClass.getAnnotation(MockitoBean.class);
List<BeanOverrideHandler> handlers = processor.createHandlers(annotation, testClass);
@Nested
class MockitoSpyBeanTests {
assertThat(handlers).singleElement().isInstanceOfSatisfying(MockitoBeanOverrideHandler.class, handler -> {
assertThat(handler.getField()).isNull();
assertThat(handler.getBeanName()).isNull();
assertThat(handler.getBeanType().resolve()).isEqualTo(Integer.class);
});
@Test
void missingTypes() {
Class<?> testClass = MissingTypesTestCase.class;
MockitoSpyBean annotation = testClass.getAnnotation(MockitoSpyBean.class);
assertThatIllegalStateException()
.isThrownBy(() -> processor.createHandlers(annotation, testClass))
.withMessage("The @MockitoSpyBean 'types' attribute must not be empty when declared on a class");
}
@Test
void nameNotSupportedWithMultipleTypes() {
Class<?> testClass = NameNotSupportedWithMultipleTypesTestCase.class;
MockitoSpyBean annotation = testClass.getAnnotation(MockitoSpyBean.class);
assertThatIllegalStateException()
.isThrownBy(() -> processor.createHandlers(annotation, testClass))
.withMessage("The @MockitoSpyBean 'name' attribute cannot be used when mocking multiple types");
}
@Test
void singleSpyByType() {
Class<?> testClass = SingleSpyByTypeTestCase.class;
MockitoSpyBean annotation = testClass.getAnnotation(MockitoSpyBean.class);
List<BeanOverrideHandler> handlers = processor.createHandlers(annotation, testClass);
assertThat(handlers).singleElement().isInstanceOfSatisfying(MockitoSpyBeanOverrideHandler.class, handler -> {
assertThat(handler.getField()).isNull();
assertThat(handler.getBeanName()).isNull();
assertThat(handler.getBeanType().resolve()).isEqualTo(Integer.class);
});
}
@Test
void singleSpyByName() {
Class<?> testClass = SingleSpyByNameTestCase.class;
MockitoSpyBean annotation = testClass.getAnnotation(MockitoSpyBean.class);
List<BeanOverrideHandler> handlers = processor.createHandlers(annotation, testClass);
assertThat(handlers).singleElement().isInstanceOfSatisfying(MockitoSpyBeanOverrideHandler.class, handler -> {
assertThat(handler.getField()).isNull();
assertThat(handler.getBeanName()).isEqualTo("enigma");
assertThat(handler.getBeanType().resolve()).isEqualTo(Integer.class);
});
}
@Test
void multipleSpies() {
Class<?> testClass = MultipleSpiesTestCase.class;
MockitoSpyBean annotation = testClass.getAnnotation(MockitoSpyBean.class);
List<BeanOverrideHandler> handlers = processor.createHandlers(annotation, testClass);
assertThat(handlers).satisfiesExactly(
handler1 -> {
assertThat(handler1.getField()).isNull();
assertThat(handler1.getBeanName()).isNull();
assertThat(handler1.getBeanType().resolve()).isEqualTo(Integer.class);
},
handler2 -> {
assertThat(handler2.getField()).isNull();
assertThat(handler2.getBeanName()).isNull();
assertThat(handler2.getBeanType().resolve()).isEqualTo(Float.class);
}
);
}
@MockitoSpyBean
static class MissingTypesTestCase {
}
@MockitoSpyBean(name = "bogus", types = { Integer.class, Float.class })
static class NameNotSupportedWithMultipleTypesTestCase {
}
@MockitoSpyBean(types = Integer.class)
static class SingleSpyByTypeTestCase {
}
@MockitoSpyBean(name = "enigma", types = Integer.class)
static class SingleSpyByNameTestCase {
}
@MockitoSpyBean(types = { Integer.class, Float.class })
static class MultipleSpiesTestCase {
}
}
@Test
void singleMockByName() {
Class<?> testClass = SingleMockByNameTestCase.class;
MockitoBean annotation = testClass.getAnnotation(MockitoBean.class);
List<BeanOverrideHandler> handlers = processor.createHandlers(annotation, testClass);
assertThat(handlers).singleElement().isInstanceOfSatisfying(MockitoBeanOverrideHandler.class, handler -> {
assertThat(handler.getField()).isNull();
assertThat(handler.getBeanName()).isEqualTo("enigma");
assertThat(handler.getBeanType().resolve()).isEqualTo(Integer.class);
});
}
@Test
void multipleMocks() {
Class<?> testClass = MultipleMocksTestCase.class;
MockitoBean annotation = testClass.getAnnotation(MockitoBean.class);
List<BeanOverrideHandler> handlers = processor.createHandlers(annotation, testClass);
assertThat(handlers).satisfiesExactly(
handler1 -> {
assertThat(handler1.getField()).isNull();
assertThat(handler1.getBeanName()).isNull();
assertThat(handler1.getBeanType().resolve()).isEqualTo(Integer.class);
},
handler2 -> {
assertThat(handler2.getField()).isNull();
assertThat(handler2.getBeanName()).isNull();
assertThat(handler2.getBeanType().resolve()).isEqualTo(Float.class);
}
);
}
@MockitoBean
static class MissingTypesTestCase {
}
@MockitoBean(name = "bogus", types = { Integer.class, Float.class })
static class NameNotSupportedWithMultipleTypesTestCase {
}
@MockitoBean(types = Integer.class)
static class SingleMockByTypeTestCase {
}
@MockitoBean(name = "enigma", types = Integer.class)
static class SingleMockByNameTestCase {
}
@MockitoBean(types = { Integer.class, Float.class })
static class MultipleMocksTestCase {
}
}
}

View File

@@ -14,10 +14,10 @@
* limitations under the License.
*/
package org.springframework.test.context.bean.override.mockito.mockbeans;
package org.springframework.test.context.bean.override.mockito.typelevel;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
@MockitoBean(types = Service01.class)
interface TestInterface01 {
interface MockTestInterface01 {
}

View File

@@ -14,10 +14,10 @@
* limitations under the License.
*/
package org.springframework.test.context.bean.override.mockito.mockbeans;
package org.springframework.test.context.bean.override.mockito.typelevel;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
@MockitoBean(types = Service08.class)
interface TestInterface08 {
interface MockTestInterface08 {
}

View File

@@ -14,10 +14,10 @@
* limitations under the License.
*/
package org.springframework.test.context.bean.override.mockito.mockbeans;
package org.springframework.test.context.bean.override.mockito.typelevel;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
@MockitoBean(types = Service11.class)
interface TestInterface11 {
interface MockTestInterface11 {
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.bean.override.mockito.mockbeans;
package org.springframework.test.context.bean.override.mockito.typelevel;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -30,6 +30,8 @@ import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.mockito.MockitoAssertions.assertIsMock;
import static org.springframework.test.mockito.MockitoAssertions.assertIsNotMock;
/**
* Integration tests for {@link MockitoBeans @MockitoBeans} and
@@ -69,12 +71,18 @@ class MockitoBeansByNameIntegrationTests {
@Test
void checkMocksAndStandardBean() {
assertIsMock(s1, "s1");
assertIsMock(s2, "s2");
assertIsMock(service3, "service3");
assertIsNotMock(service4, "service4");
assertThat(s1.greeting()).isEqualTo("mock 1");
assertThat(s2.greeting()).isEqualTo("mock 2");
assertThat(service3.greeting()).isEqualTo("mock 3");
assertThat(service4.greeting()).isEqualTo("prod 4");
}
@Configuration
static class Config {

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.bean.override.mockito.mockbeans;
package org.springframework.test.context.bean.override.mockito.typelevel;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
@@ -27,6 +27,7 @@ import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.mockito.MockitoAssertions.assertIsMock;
/**
* Integration tests for {@link MockitoBeans @MockitoBeans} and
@@ -42,7 +43,7 @@ import static org.mockito.BDDMockito.given;
@MockitoBean(types = {Service04.class, Service05.class})
@SharedMocks // Intentionally declared between local @MockitoBean declarations
@MockitoBean(types = Service06.class)
class MockitoBeansByTypeIntegrationTests implements TestInterface01 {
class MockitoBeansByTypeIntegrationTests implements MockTestInterface01 {
@Autowired
Service01 service01;
@@ -79,6 +80,14 @@ class MockitoBeansByTypeIntegrationTests implements TestInterface01 {
@Test
void checkMocks() {
assertIsMock(service01, "service01");
assertIsMock(service02, "service02");
assertIsMock(service03, "service03");
assertIsMock(service04, "service04");
assertIsMock(service05, "service05");
assertIsMock(service06, "service06");
assertIsMock(service07, "service07");
assertThat(service01.greeting()).isEqualTo("mock 01");
assertThat(service02.greeting()).isEqualTo("mock 02");
assertThat(service03.greeting()).isEqualTo("mock 03");
@@ -90,7 +99,7 @@ class MockitoBeansByTypeIntegrationTests implements TestInterface01 {
@MockitoBean(types = Service09.class)
class BaseTestCase implements TestInterface08 {
class BaseTestCase implements MockTestInterface08 {
@Autowired
Service08 service08;
@@ -104,7 +113,7 @@ class MockitoBeansByTypeIntegrationTests implements TestInterface01 {
@Nested
@MockitoBean(types = Service12.class)
class NestedTests extends BaseTestCase implements TestInterface11 {
class NestedTests extends BaseTestCase implements MockTestInterface11 {
@Autowired
Service11 service11;
@@ -128,6 +137,20 @@ class MockitoBeansByTypeIntegrationTests implements TestInterface01 {
@Test
void checkMocks() {
assertIsMock(service01, "service01");
assertIsMock(service02, "service02");
assertIsMock(service03, "service03");
assertIsMock(service04, "service04");
assertIsMock(service05, "service05");
assertIsMock(service06, "service06");
assertIsMock(service07, "service07");
assertIsMock(service08, "service08");
assertIsMock(service09, "service09");
assertIsMock(service10, "service10");
assertIsMock(service11, "service11");
assertIsMock(service12, "service12");
assertIsMock(service13, "service13");
assertThat(service01.greeting()).isEqualTo("mock 01");
assertThat(service02.greeting()).isEqualTo("mock 02");
assertThat(service03.greeting()).isEqualTo("mock 03");

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.bean.override.mockito.mockbeans;
package org.springframework.test.context.bean.override.mockito.typelevel;
import java.util.stream.Stream;

View File

@@ -0,0 +1,127 @@
/*
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.bean.override.mockito.typelevel;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.bean.override.example.ExampleService;
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
import org.springframework.test.context.bean.override.mockito.MockitoSpyBeans;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.mockito.MockitoAssertions.assertIsNotMock;
import static org.springframework.test.mockito.MockitoAssertions.assertIsNotSpy;
import static org.springframework.test.mockito.MockitoAssertions.assertIsSpy;
/**
* Integration tests for {@link MockitoSpyBeans @MockitoSpyBeans} and
* {@link MockitoSpyBean @MockitoSpyBean} declared "by name" at the class level
* as a repeatable annotation.
*
* @author Sam Brannen
* @since 6.2.3
* @see <a href="https://github.com/spring-projects/spring-framework/issues/34408">gh-34408</a>
* @see MockitoSpyBeansByTypeIntegrationTests
*/
@SpringJUnitConfig
@MockitoSpyBean(name = "s1", types = ExampleService.class)
@MockitoSpyBean(name = "s2", types = ExampleService.class)
class MockitoSpyBeansByNameIntegrationTests {
@Autowired
ExampleService s1;
@Autowired
ExampleService s2;
@MockitoSpyBean(name = "s3")
ExampleService service3;
@Autowired
@Qualifier("s4")
ExampleService service4;
@BeforeEach
void configureSpies() {
given(s1.greeting()).willReturn("spy 1");
given(s2.greeting()).willReturn("spy 2");
given(service3.greeting()).willReturn("spy 3");
}
@Test
void checkSpiesAndStandardBean() {
assertIsSpy(s1, "s1");
assertIsSpy(s2, "s2");
assertIsSpy(service3, "service3");
assertIsNotMock(service4, "service4");
assertIsNotSpy(service4, "service4");
assertThat(s1.greeting()).isEqualTo("spy 1");
assertThat(s2.greeting()).isEqualTo("spy 2");
assertThat(service3.greeting()).isEqualTo("spy 3");
assertThat(service4.greeting()).isEqualTo("prod 4");
}
@Configuration
static class Config {
@Bean
ExampleService s1() {
return new ExampleService() {
@Override
public String greeting() {
return "prod 1";
}
};
}
@Bean
ExampleService s2() {
return new ExampleService() {
@Override
public String greeting() {
return "prod 2";
}
};
}
@Bean
ExampleService s3() {
return new ExampleService() {
@Override
public String greeting() {
return "prod 3";
}
};
}
@Bean
ExampleService s4() {
return () -> "prod 4";
}
}
}

View File

@@ -0,0 +1,307 @@
/*
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.bean.override.mockito.typelevel;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
import org.springframework.test.context.bean.override.mockito.MockitoSpyBeans;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.mockito.MockitoAssertions.assertIsSpy;
/**
* Integration tests for {@link MockitoSpyBeans @MockitoSpyBeans} and
* {@link MockitoSpyBean @MockitoSpyBean} declared "by type" at the class level,
* as a repeatable annotation, and via a custom composed annotation.
*
* @author Sam Brannen
* @since 6.2.3
* @see <a href="https://github.com/spring-projects/spring-framework/issues/34408">gh-34408</a>
* @see MockitoSpyBeansByNameIntegrationTests
*/
@SpringJUnitConfig
@MockitoSpyBean(types = {Service04.class, Service05.class})
@SharedSpies // Intentionally declared between local @MockitoSpyBean declarations
@MockitoSpyBean(types = Service06.class)
class MockitoSpyBeansByTypeIntegrationTests implements SpyTestInterface01 {
@Autowired
Service01 service01;
@Autowired
Service02 service02;
@Autowired
Service03 service03;
@Autowired
Service04 service04;
@Autowired
Service05 service05;
@Autowired
Service06 service06;
@MockitoSpyBean
Service07 service07;
@BeforeEach
void configureSpies() {
given(service01.greeting()).willReturn("spy 01");
given(service02.greeting()).willReturn("spy 02");
given(service03.greeting()).willReturn("spy 03");
given(service04.greeting()).willReturn("spy 04");
given(service05.greeting()).willReturn("spy 05");
given(service06.greeting()).willReturn("spy 06");
given(service07.greeting()).willReturn("spy 07");
}
@Test
void checkSpies() {
assertIsSpy(service01, "service01");
assertIsSpy(service02, "service02");
assertIsSpy(service03, "service03");
assertIsSpy(service04, "service04");
assertIsSpy(service05, "service05");
assertIsSpy(service06, "service06");
assertIsSpy(service07, "service07");
assertThat(service01.greeting()).isEqualTo("spy 01");
assertThat(service02.greeting()).isEqualTo("spy 02");
assertThat(service03.greeting()).isEqualTo("spy 03");
assertThat(service04.greeting()).isEqualTo("spy 04");
assertThat(service05.greeting()).isEqualTo("spy 05");
assertThat(service06.greeting()).isEqualTo("spy 06");
assertThat(service07.greeting()).isEqualTo("spy 07");
}
@MockitoSpyBean(types = Service09.class)
class BaseTestCase implements SpyTestInterface08 {
@Autowired
Service08 service08;
@Autowired
Service09 service09;
@MockitoSpyBean
Service10 service10;
}
@Nested
@MockitoSpyBean(types = Service12.class)
class NestedTests extends BaseTestCase implements SpyTestInterface11 {
@Autowired
Service11 service11;
@Autowired
Service12 service12;
@MockitoSpyBean
Service13 service13;
@BeforeEach
void configureSpies() {
given(service08.greeting()).willReturn("spy 08");
given(service09.greeting()).willReturn("spy 09");
given(service10.greeting()).willReturn("spy 10");
given(service11.greeting()).willReturn("spy 11");
given(service12.greeting()).willReturn("spy 12");
given(service13.greeting()).willReturn("spy 13");
}
@Test
void checkSpies() {
assertIsSpy(service01, "service01");
assertIsSpy(service02, "service02");
assertIsSpy(service03, "service03");
assertIsSpy(service04, "service04");
assertIsSpy(service05, "service05");
assertIsSpy(service06, "service06");
assertIsSpy(service07, "service07");
assertIsSpy(service08, "service08");
assertIsSpy(service09, "service09");
assertIsSpy(service10, "service10");
assertIsSpy(service11, "service11");
assertIsSpy(service12, "service12");
assertIsSpy(service13, "service13");
assertThat(service01.greeting()).isEqualTo("spy 01");
assertThat(service02.greeting()).isEqualTo("spy 02");
assertThat(service03.greeting()).isEqualTo("spy 03");
assertThat(service04.greeting()).isEqualTo("spy 04");
assertThat(service05.greeting()).isEqualTo("spy 05");
assertThat(service06.greeting()).isEqualTo("spy 06");
assertThat(service07.greeting()).isEqualTo("spy 07");
assertThat(service08.greeting()).isEqualTo("spy 08");
assertThat(service09.greeting()).isEqualTo("spy 09");
assertThat(service10.greeting()).isEqualTo("spy 10");
assertThat(service11.greeting()).isEqualTo("spy 11");
assertThat(service12.greeting()).isEqualTo("spy 12");
assertThat(service13.greeting()).isEqualTo("spy 13");
}
}
@Configuration
static class Config {
@Bean
Service01 service01() {
return new Service01() {
@Override
public String greeting() {
return "prod 1";
}
};
}
@Bean
Service02 service02() {
return new Service02() {
@Override
public String greeting() {
return "prod 2";
}
};
}
@Bean
Service03 service03() {
return new Service03() {
@Override
public String greeting() {
return "prod 3";
}
};
}
@Bean
Service04 service04() {
return new Service04() {
@Override
public String greeting() {
return "prod 4";
}
};
}
@Bean
Service05 service05() {
return new Service05() {
@Override
public String greeting() {
return "prod 5";
}
};
}
@Bean
Service06 service06() {
return new Service06() {
@Override
public String greeting() {
return "prod 6";
}
};
}
@Bean
Service07 service07() {
return new Service07() {
@Override
public String greeting() {
return "prod 7";
}
};
}
@Bean
Service08 service08() {
return new Service08() {
@Override
public String greeting() {
return "prod 8";
}
};
}
@Bean
Service09 service09() {
return new Service09() {
@Override
public String greeting() {
return "prod 9";
}
};
}
@Bean
Service10 service10() {
return new Service10() {
@Override
public String greeting() {
return "prod 10";
}
};
}
@Bean
Service11 service11() {
return new Service11() {
@Override
public String greeting() {
return "prod 11";
}
};
}
@Bean
Service12 service12() {
return new Service12() {
@Override
public String greeting() {
return "prod 12";
}
};
}
@Bean
Service13 service13() {
return new Service13() {
@Override
public String greeting() {
return "prod 13";
}
};
}
}
}

View File

@@ -0,0 +1,68 @@
/*
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.bean.override.mockito.typelevel;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.springframework.core.ResolvableType;
import org.springframework.test.context.bean.override.BeanOverrideHandler;
import org.springframework.test.context.bean.override.BeanOverrideTestUtils;
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
import org.springframework.test.context.bean.override.mockito.MockitoSpyBeans;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link MockitoSpyBeans @MockitoSpyBeans}: {@link MockitoSpyBean @MockitoSpyBean}
* declared at the class level, as a repeatable annotation, and via a custom composed
* annotation.
*
* @author Sam Brannen
* @since 6.2.3
* @see <a href="https://github.com/spring-projects/spring-framework/issues/34408">gh-34408</a>
*/
class MockitoSpyBeansTests {
@Test
void registrationOrderForTopLevelClass() {
Stream<Class<?>> mockedServices = getRegisteredMockTypes(MockitoSpyBeansByTypeIntegrationTests.class);
assertThat(mockedServices).containsExactly(
Service01.class, Service02.class, Service03.class, Service04.class,
Service05.class, Service06.class, Service07.class);
}
@Test
void registrationOrderForNestedClass() {
Stream<Class<?>> mockedServices = getRegisteredMockTypes(MockitoSpyBeansByTypeIntegrationTests.NestedTests.class);
assertThat(mockedServices).containsExactly(
Service01.class, Service02.class, Service03.class, Service04.class,
Service05.class, Service06.class, Service07.class, Service08.class,
Service09.class, Service10.class, Service11.class, Service12.class,
Service13.class);
}
private static Stream<Class<?>> getRegisteredMockTypes(Class<?> testClass) {
return BeanOverrideTestUtils.findAllHandlers(testClass)
.stream()
.map(BeanOverrideHandler::getBeanType)
.map(ResolvableType::getRawClass);
}
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.bean.override.mockito.mockbeans;
package org.springframework.test.context.bean.override.mockito.typelevel;
interface Service {
String greeting();

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.bean.override.mockito.mockbeans;
package org.springframework.test.context.bean.override.mockito.typelevel;
interface Service01 extends Service {
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.bean.override.mockito.mockbeans;
package org.springframework.test.context.bean.override.mockito.typelevel;
interface Service02 extends Service {
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.bean.override.mockito.mockbeans;
package org.springframework.test.context.bean.override.mockito.typelevel;
interface Service03 extends Service {
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.bean.override.mockito.mockbeans;
package org.springframework.test.context.bean.override.mockito.typelevel;
interface Service04 extends Service {
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.bean.override.mockito.mockbeans;
package org.springframework.test.context.bean.override.mockito.typelevel;
interface Service05 extends Service {
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.bean.override.mockito.mockbeans;
package org.springframework.test.context.bean.override.mockito.typelevel;
interface Service06 extends Service {
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.bean.override.mockito.mockbeans;
package org.springframework.test.context.bean.override.mockito.typelevel;
interface Service07 extends Service {
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.bean.override.mockito.mockbeans;
package org.springframework.test.context.bean.override.mockito.typelevel;
interface Service08 extends Service {
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.bean.override.mockito.mockbeans;
package org.springframework.test.context.bean.override.mockito.typelevel;
interface Service09 extends Service {
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.bean.override.mockito.mockbeans;
package org.springframework.test.context.bean.override.mockito.typelevel;
interface Service10 extends Service {
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.bean.override.mockito.mockbeans;
package org.springframework.test.context.bean.override.mockito.typelevel;
interface Service11 extends Service {
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.bean.override.mockito.mockbeans;
package org.springframework.test.context.bean.override.mockito.typelevel;
interface Service12 extends Service {
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.bean.override.mockito.mockbeans;
package org.springframework.test.context.bean.override.mockito.typelevel;
interface Service13 extends Service {
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.bean.override.mockito.mockbeans;
package org.springframework.test.context.bean.override.mockito.typelevel;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.bean.override.mockito.typelevel;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@MockitoSpyBean(types = Service02.class)
@MockitoSpyBean(types = Service03.class)
@interface SharedSpies {
}

View File

@@ -0,0 +1,23 @@
/*
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.bean.override.mockito.typelevel;
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
@MockitoSpyBean(types = Service01.class)
interface SpyTestInterface01 {
}

View File

@@ -0,0 +1,23 @@
/*
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.bean.override.mockito.typelevel;
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
@MockitoSpyBean(types = Service08.class)
interface SpyTestInterface08 {
}

View File

@@ -0,0 +1,23 @@
/*
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.bean.override.mockito.typelevel;
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
@MockitoSpyBean(types = Service11.class)
interface SpyTestInterface11 {
}

View File

@@ -31,10 +31,12 @@ public abstract class MockitoAssertions {
public static void assertIsMock(Object obj) {
assertThat(isMock(obj)).as("is a Mockito mock").isTrue();
assertIsNotSpy(obj);
}
public static void assertIsMock(Object obj, String message) {
assertThat(isMock(obj)).as("%s is a Mockito mock", message).isTrue();
assertIsNotSpy(obj, message);
}
public static void assertIsNotMock(Object obj) {