diff --git a/spring-test/src/test/java/org/springframework/test/context/aot/TestContextAotGeneratorIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/aot/TestContextAotGeneratorIntegrationTests.java index 7b11ed66b711..e5b0bf2250c5 100644 --- a/spring-test/src/test/java/org/springframework/test/context/aot/TestContextAotGeneratorIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/aot/TestContextAotGeneratorIntegrationTests.java @@ -28,7 +28,6 @@ import org.assertj.core.util.Arrays; import org.easymock.EasyMockSupport; import org.junit.jupiter.api.Test; -import org.mockito.Mockito; import org.springframework.aot.AotDetector; import org.springframework.aot.generate.DefaultGenerationContext; @@ -80,6 +79,7 @@ import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.proxies; import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.reflection; import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.resource; +import static org.springframework.test.mockito.MockitoAssertions.assertIsMock; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -377,8 +377,8 @@ private void assertContextForMockitoBeanOverrideTests(ApplicationContext context GreetingService greetingService = context.getBean(GreetingService.class); MessageService messageService = context.getBean(MessageService.class); - assertThat(Mockito.mockingDetails(greetingService).isMock()).as("Mockito mock").isTrue(); - assertThat(Mockito.mockingDetails(messageService).isMock()).as("Mockito mock").isTrue(); + assertIsMock(greetingService, "greetingService"); + assertIsMock(messageService, "messageService"); } private void assertContextForWebTests(WebApplicationContext wac) throws Exception { diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoBeanDuplicateTypeIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoBeanDuplicateTypeIntegrationTests.java index 4ff6f6248087..d00ead41963e 100644 --- a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoBeanDuplicateTypeIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoBeanDuplicateTypeIntegrationTests.java @@ -33,6 +33,8 @@ * @author Sam Brannen * @since 6.2.1 * @see gh-34025 + * @see MockitoSpyBeanDuplicateTypeIntegrationTests + * @see MockitoSpyBeanDuplicateTypeAndNameIntegrationTests */ @SpringJUnitConfig public class MockitoBeanDuplicateTypeIntegrationTests { diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoBeanForByNameLookupIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoBeanForByNameLookupIntegrationTests.java index 321866d464bc..d7ddafaa43c9 100644 --- a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoBeanForByNameLookupIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoBeanForByNameLookupIntegrationTests.java @@ -19,7 +19,6 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; -import org.mockito.Mockito; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; @@ -27,6 +26,7 @@ import org.springframework.test.context.bean.override.example.ExampleService; import org.springframework.test.context.bean.override.example.RealExampleService; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; +import org.springframework.test.mockito.MockitoAssertions; import static org.assertj.core.api.Assertions.assertThat; @@ -59,7 +59,7 @@ public class MockitoBeanForByNameLookupIntegrationTests { void fieldAndRenamedFieldHaveSameOverride(ApplicationContext ctx) { assertThat(ctx.getBean("field")) .isInstanceOf(ExampleService.class) - .satisfies(o -> assertThat(Mockito.mockingDetails(o).isMock()).as("isMock").isTrue()) + .satisfies(MockitoAssertions::assertIsMock) .isSameAs(this.field) .isSameAs(this.renamed1); @@ -71,7 +71,7 @@ void fieldAndRenamedFieldHaveSameOverride(ApplicationContext ctx) { void fieldIsMockedWhenNoOriginalBean(ApplicationContext ctx) { assertThat(ctx.getBean("nonExistingBean")) .isInstanceOf(ExampleService.class) - .satisfies(o -> assertThat(Mockito.mockingDetails(o).isMock()).as("isMock").isTrue()) + .satisfies(MockitoAssertions::assertIsMock) .isSameAs(this.nonExisting1); assertThat(this.nonExisting1.greeting()).as("mocked greeting").isNull(); @@ -86,7 +86,7 @@ public class MockitoBeanNestedTests { void fieldAndRenamedFieldHaveSameOverride(ApplicationContext ctx) { assertThat(ctx.getBean("nestedField")) .isInstanceOf(ExampleService.class) - .satisfies(o -> assertThat(Mockito.mockingDetails(o).isMock()).as("isMock").isTrue()) + .satisfies(MockitoAssertions::assertIsMock) .isSameAs(nestedField) .isSameAs(renamed2); } @@ -95,7 +95,7 @@ void fieldAndRenamedFieldHaveSameOverride(ApplicationContext ctx) { void fieldIsMockedWhenNoOriginalBean(ApplicationContext ctx) { assertThat(ctx.getBean("nestedNonExistingBean")) .isInstanceOf(ExampleService.class) - .satisfies(o -> assertThat(Mockito.mockingDetails(o).isMock()).as("isMock").isTrue()) + .satisfies(MockitoAssertions::assertIsMock) .isSameAs(nonExisting2); } } diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoBeanForByTypeLookupIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoBeanForByTypeLookupIntegrationTests.java index 0836b4571806..72f2d03435be 100644 --- a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoBeanForByTypeLookupIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoBeanForByTypeLookupIntegrationTests.java @@ -17,7 +17,6 @@ package org.springframework.test.context.bean.override.mockito; import org.junit.jupiter.api.Test; -import org.mockito.Mockito; import org.springframework.beans.factory.NoUniqueBeanDefinitionException; import org.springframework.beans.factory.annotation.Qualifier; @@ -29,6 +28,7 @@ import org.springframework.test.context.bean.override.example.ExampleService; import org.springframework.test.context.bean.override.example.RealExampleService; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; +import org.springframework.test.mockito.MockitoAssertions; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; @@ -37,6 +37,7 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.springframework.test.mockito.MockitoAssertions.assertIsMock; /** * Integration tests for {@link MockitoBean} that use by-type lookup. @@ -64,8 +65,7 @@ public class MockitoBeanForByTypeLookupIntegrationTests { @Test void mockIsCreatedWhenNoCandidateIsFound() { - assertThat(this.serviceIsNotABean) - .satisfies(o -> assertThat(Mockito.mockingDetails(o).isMock()).as("isMock").isTrue()); + assertIsMock(this.serviceIsNotABean); when(this.serviceIsNotABean.hello()).thenReturn("Mocked hello"); @@ -77,7 +77,7 @@ void mockIsCreatedWhenNoCandidateIsFound() { @Test void overrideIsFoundByType(ApplicationContext ctx) { assertThat(this.anyNameForService) - .satisfies(o -> assertThat(Mockito.mockingDetails(o).isMock()).as("isMock").isTrue()) + .satisfies(MockitoAssertions::assertIsMock) .isSameAs(ctx.getBean("example")) .isSameAs(ctx.getBean(ExampleService.class)); @@ -91,7 +91,7 @@ void overrideIsFoundByType(ApplicationContext ctx) { @Test void overrideIsFoundByTypeAndDisambiguatedByQualifier(ApplicationContext ctx) { assertThat(this.ambiguous) - .satisfies(o -> assertThat(Mockito.mockingDetails(o).isMock()).as("isMock").isTrue()) + .satisfies(MockitoAssertions::assertIsMock) .isSameAs(ctx.getBean("ambiguous2")); assertThatExceptionOfType(NoUniqueBeanDefinitionException.class) @@ -108,7 +108,7 @@ void overrideIsFoundByTypeAndDisambiguatedByQualifier(ApplicationContext ctx) { @Test void overrideIsFoundByTypeAndDisambiguatedByMetaQualifier(ApplicationContext ctx) { assertThat(this.ambiguousMeta) - .satisfies(o -> assertThat(Mockito.mockingDetails(o).isMock()).as("isMock").isTrue()) + .satisfies(MockitoAssertions::assertIsMock) .isSameAs(ctx.getBean("ambiguous1")); assertThatExceptionOfType(NoUniqueBeanDefinitionException.class) diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBeanDuplicateTypeAndNameIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBeanDuplicateTypeAndNameIntegrationTests.java new file mode 100644 index 000000000000..66bc7030dcb2 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBeanDuplicateTypeAndNameIntegrationTests.java @@ -0,0 +1,83 @@ +/* + * Copyright 2002-2024 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; + +import java.util.List; + +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.example.ExampleService; +import org.springframework.test.context.bean.override.example.RealExampleService; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.test.mockito.MockitoAssertions.assertIsNotSpy; +import static org.springframework.test.mockito.MockitoAssertions.assertIsSpy; + +/** + * Integration tests for duplicate {@link MockitoSpyBean @MockitoSpyBean} + * declarations for the same target bean, selected by-name. + * + * @author Sam Brannen + * @since 6.2.1 + * @see MockitoBeanDuplicateTypeIntegrationTests + * @see MockitoSpyBeanDuplicateTypeIntegrationTests + */ +@SpringJUnitConfig +public class MockitoSpyBeanDuplicateTypeAndNameIntegrationTests { + + @MockitoSpyBean("exampleService1") + ExampleService service1; + + @MockitoSpyBean("exampleService1") + ExampleService service2; + + @Autowired + ExampleService exampleService2; + + @Autowired + List services; + + + @Test + void duplicateMocksShouldHaveBeenCreated() { + assertThat(service1).isSameAs(service2); + assertThat(services).containsExactly(service1, exampleService2); + + assertIsSpy(service1, "service1"); + assertIsNotSpy(exampleService2, "exampleService2"); + } + + + @Configuration(proxyBeanMethods = false) + static class Config { + + @Bean + ExampleService exampleService1() { + return new RealExampleService("@Bean 1"); + } + + @Bean + ExampleService exampleService2() { + return new RealExampleService("@Bean 2"); + } + } + +} diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBeanDuplicateTypeIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBeanDuplicateTypeIntegrationTests.java new file mode 100644 index 000000000000..35a813847466 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBeanDuplicateTypeIntegrationTests.java @@ -0,0 +1,73 @@ +/* + * Copyright 2002-2024 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; + +import java.util.List; + +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.example.ExampleService; +import org.springframework.test.context.bean.override.example.RealExampleService; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.test.mockito.MockitoAssertions.assertIsSpy; + +/** + * Integration tests for duplicate {@link MockitoSpyBean @MockitoSpyBean} + * declarations for the same target bean, selected by-type. + * + * @author Sam Brannen + * @since 6.2.1 + * @see MockitoBeanDuplicateTypeIntegrationTests + * @see MockitoSpyBeanDuplicateTypeAndNameIntegrationTests + */ +@SpringJUnitConfig +public class MockitoSpyBeanDuplicateTypeIntegrationTests { + + @MockitoSpyBean + ExampleService service1; + + @MockitoSpyBean + ExampleService service2; + + @Autowired + List services; + + + @Test + void test() { + assertThat(service1).isSameAs(service2); + assertThat(services).containsExactly(service1); + + assertIsSpy(service1, "service1"); + } + + + @Configuration(proxyBeanMethods = false) + static class Config { + + @Bean + ExampleService exampleService() { + return new RealExampleService("@Bean"); + } + } + +} diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBeanForByNameLookupIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBeanForByNameLookupIntegrationTests.java index 1310a18d4365..17989e22d143 100644 --- a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBeanForByNameLookupIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBeanForByNameLookupIntegrationTests.java @@ -19,7 +19,6 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; -import org.mockito.Mockito; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; @@ -28,6 +27,7 @@ import org.springframework.test.context.bean.override.example.RealExampleService; import org.springframework.test.context.bean.override.mockito.MockitoSpyBeanForByNameLookupIntegrationTests.Config; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; +import org.springframework.test.mockito.MockitoAssertions; import static org.assertj.core.api.Assertions.assertThat; @@ -35,80 +35,81 @@ * Integration tests for {@link MockitoSpyBean} that use by-name lookup. * * @author Simon Baslé + * @author Sam Brannen * @since 6.2 */ @SpringJUnitConfig(Config.class) public class MockitoSpyBeanForByNameLookupIntegrationTests { - @MockitoSpyBean("field") + @MockitoSpyBean("field1") ExampleService field; - @MockitoSpyBean("nestedField") - ExampleService nestedField; - - @MockitoSpyBean("field") + @MockitoSpyBean("field1") ExampleService renamed1; - @MockitoSpyBean("nestedField") - ExampleService renamed2; - @Test void fieldHasOverride(ApplicationContext ctx) { - assertThat(ctx.getBean("field")) + assertThat(ctx.getBean("field1")) .isInstanceOf(ExampleService.class) - .satisfies(o -> assertThat(Mockito.mockingDetails(o).isSpy()).as("isSpy").isTrue()) - .isSameAs(this.field); + .satisfies(MockitoAssertions::assertIsSpy) + .isSameAs(field); - assertThat(this.field.greeting()).isEqualTo("Hello Field"); + assertThat(field.greeting()).isEqualTo("bean1"); } @Test void renamedFieldHasOverride(ApplicationContext ctx) { - assertThat(ctx.getBean("field")) + assertThat(ctx.getBean("field1")) .isInstanceOf(ExampleService.class) - .satisfies(o -> assertThat(Mockito.mockingDetails(o).isSpy()).as("isSpy").isTrue()) - .isSameAs(this.renamed1); + .satisfies(MockitoAssertions::assertIsSpy) + .isSameAs(renamed1); - assertThat(this.renamed1.greeting()).isEqualTo("Hello Field"); + assertThat(renamed1.greeting()).isEqualTo("bean1"); } @Nested - @DisplayName("With @MockitoSpyBean in enclosing class") + @DisplayName("With @MockitoSpyBean in enclosing class and in @Nested class") public class MockitoSpyBeanNestedTests { + @MockitoSpyBean("field2") + ExampleService nestedField; + + @MockitoSpyBean("field2") + ExampleService renamed2; + @Test void fieldHasOverride(ApplicationContext ctx) { - assertThat(ctx.getBean("nestedField")) + assertThat(ctx.getBean("field2")) .isInstanceOf(ExampleService.class) - .satisfies(o -> assertThat(Mockito.mockingDetails(o).isSpy()).as("isSpy").isTrue()) + .satisfies(MockitoAssertions::assertIsSpy) .isSameAs(nestedField); - assertThat(nestedField.greeting()).isEqualTo("Hello Nested Field"); + assertThat(nestedField.greeting()).isEqualTo("bean2"); } @Test void renamedFieldHasOverride(ApplicationContext ctx) { - assertThat(ctx.getBean("nestedField")) + assertThat(ctx.getBean("field2")) .isInstanceOf(ExampleService.class) - .satisfies(o -> assertThat(Mockito.mockingDetails(o).isSpy()).as("isSpy").isTrue()) + .satisfies(MockitoAssertions::assertIsSpy) .isSameAs(renamed2); - assertThat(renamed2.greeting()).isEqualTo("Hello Nested Field"); + assertThat(renamed2.greeting()).isEqualTo("bean2"); } } @Configuration(proxyBeanMethods = false) static class Config { - @Bean("field") + @Bean("field1") ExampleService bean1() { - return new RealExampleService("Hello Field"); + return new RealExampleService("bean1"); } - @Bean("nestedField") + @Bean("field2") ExampleService bean2() { - return new RealExampleService("Hello Nested Field"); + return new RealExampleService("bean2"); } } diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBeanForByTypeLookupIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBeanForByTypeLookupIntegrationTests.java index 0e01b937e3d0..ed07b8752fc7 100644 --- a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBeanForByTypeLookupIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBeanForByTypeLookupIntegrationTests.java @@ -17,7 +17,6 @@ package org.springframework.test.context.bean.override.mockito; import org.junit.jupiter.api.Test; -import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.ApplicationContext; @@ -28,6 +27,7 @@ import org.springframework.test.context.bean.override.example.ExampleService; import org.springframework.test.context.bean.override.example.RealExampleService; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; +import org.springframework.test.mockito.MockitoAssertions; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatException; @@ -59,7 +59,7 @@ public class MockitoSpyBeanForByTypeLookupIntegrationTests { @Test void overrideIsFoundByType(ApplicationContext ctx) { assertThat(this.anyNameForService) - .satisfies(o -> assertThat(Mockito.mockingDetails(o).isSpy()).as("isSpy").isTrue()) + .satisfies(MockitoAssertions::assertIsSpy) .isSameAs(ctx.getBean("example")) .isSameAs(ctx.getBean(ExampleService.class)); @@ -71,7 +71,7 @@ void overrideIsFoundByType(ApplicationContext ctx) { @Test void overrideIsFoundByTypeAndDisambiguatedByQualifier(ApplicationContext ctx) { assertThat(this.ambiguous) - .satisfies(o -> assertThat(Mockito.mockingDetails(o).isSpy()).as("isSpy").isTrue()) + .satisfies(MockitoAssertions::assertIsSpy) .isSameAs(ctx.getBean("ambiguous2")); assertThatException() @@ -88,7 +88,7 @@ void overrideIsFoundByTypeAndDisambiguatedByQualifier(ApplicationContext ctx) { @Test void overrideIsFoundByTypeAndDisambiguatedByMetaQualifier(ApplicationContext ctx) { assertThat(this.ambiguousMeta) - .satisfies(o -> assertThat(Mockito.mockingDetails(o).isSpy()).as("isSpy").isTrue()) + .satisfies(MockitoAssertions::assertIsSpy) .isSameAs(ctx.getBean("ambiguous1")); assertThatException() diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoBeanAndAsyncInterfaceMethodIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoBeanAndAsyncInterfaceMethodIntegrationTests.java index 0e74d5669517..617238f759a1 100644 --- a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoBeanAndAsyncInterfaceMethodIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoBeanAndAsyncInterfaceMethodIntegrationTests.java @@ -20,7 +20,6 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mockito; import org.springframework.aop.support.AopUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -34,6 +33,7 @@ import static java.util.concurrent.CompletableFuture.completedFuture; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; +import static org.springframework.test.mockito.MockitoAssertions.assertIsMock; /** * Tests for {@link MockitoBean @MockitoBean} where the mocked interface has an @@ -56,7 +56,7 @@ public class MockitoBeanAndAsyncInterfaceMethodIntegrationTests { @Test void mockedMethodsAreNotAsync() throws Exception { assertThat(AopUtils.isAopProxy(transformer)).as("is Spring AOP proxy").isFalse(); - assertThat(Mockito.mockingDetails(transformer).isMock()).as("is Mockito mock").isTrue(); + assertIsMock(transformer); given(transformer.transform("foo")).willReturn(completedFuture("bar")); assertThat(service.transform("foo")).isEqualTo("result: bar"); diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoBeanAndSpringAopProxyIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoBeanAndSpringAopProxyIntegrationTests.java index 11a8c297c671..edfe9c595418 100644 --- a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoBeanAndSpringAopProxyIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoBeanAndSpringAopProxyIntegrationTests.java @@ -18,7 +18,6 @@ import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mockito; import org.springframework.aop.support.AopUtils; import org.springframework.cache.CacheManager; @@ -39,6 +38,7 @@ import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.springframework.test.mockito.MockitoAssertions.assertIsMock; /** * Tests for {@link MockitoBean @MockitoBean} used in combination with Spring AOP. @@ -69,7 +69,7 @@ class MockitoBeanAndSpringAopProxyIntegrationTests { @RepeatedTest(2) void mockShouldNotBeAnAopProxy() { assertThat(AopUtils.isAopProxy(dateService)).as("is Spring AOP proxy").isFalse(); - assertThat(Mockito.mockingDetails(dateService).isMock()).as("is Mockito mock").isTrue(); + assertIsMock(dateService); given(dateService.getDate(false)).willReturn(1L); Long date = dateService.getDate(false); diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoBeanUsedDuringApplicationContextRefreshIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoBeanUsedDuringApplicationContextRefreshIntegrationTests.java index edfe3a817895..611ae207ab35 100644 --- a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoBeanUsedDuringApplicationContextRefreshIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoBeanUsedDuringApplicationContextRefreshIntegrationTests.java @@ -17,7 +17,6 @@ package org.springframework.test.context.bean.override.mockito.integration; import org.junit.jupiter.api.Test; -import org.mockito.Mockito; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.event.EventListener; @@ -26,9 +25,10 @@ import org.springframework.test.context.bean.override.mockito.integration.MockitoBeanUsedDuringApplicationContextRefreshIntegrationTests.ContextRefreshedEventListener; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; -import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.then; +import static org.springframework.test.mockito.MockitoAssertions.assertIsMock; +import static org.springframework.test.mockito.MockitoAssertions.assertIsNotSpy; /** * Integration tests for {@link MockitoBean @MockitoBean} used during @@ -48,8 +48,9 @@ class MockitoBeanUsedDuringApplicationContextRefreshIntegrationTests { @Test void test() { - assertThat(Mockito.mockingDetails(eventProcessor).isMock()).as("isMock").isTrue(); - assertThat(Mockito.mockingDetails(eventProcessor).isSpy()).as("isSpy").isFalse(); + assertIsMock(eventProcessor); + assertIsNotSpy(eventProcessor); + // Ensure that the mock was invoked during ApplicationContext refresh // and has not been reset in the interim. then(eventProcessor).should().process(any(ContextRefreshedEvent.class)); diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoBeanWithMultipleExistingBeansAndExplicitBeanNameIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoBeanWithMultipleExistingBeansAndExplicitBeanNameIntegrationTests.java index 467f699b78bc..922b988b6628 100644 --- a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoBeanWithMultipleExistingBeansAndExplicitBeanNameIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoBeanWithMultipleExistingBeansAndExplicitBeanNameIntegrationTests.java @@ -17,8 +17,6 @@ package org.springframework.test.context.bean.override.mockito.integration; import org.junit.jupiter.api.Test; -import org.mockito.MockingDetails; -import org.mockito.mock.MockCreationSettings; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; @@ -33,7 +31,8 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.then; -import static org.mockito.Mockito.mockingDetails; +import static org.springframework.test.mockito.MockitoAssertions.assertIsMock; +import static org.springframework.test.mockito.MockitoAssertions.assertMockName; /** * Tests that {@link MockitoBean @MockitoBean} can be used to mock a bean when @@ -58,10 +57,8 @@ class MockitoBeanWithMultipleExistingBeansAndExplicitBeanNameIntegrationTests { @Test void test() { - MockingDetails mockingDetails = mockingDetails(mock); - MockCreationSettings mockSettings = mockingDetails.getMockCreationSettings(); - assertThat(mockingDetails.isMock()).as("is mock").isTrue(); - assertThat(mockSettings.getMockName()).as("mock name").hasToString("stringService"); + assertIsMock(mock); + assertMockName(mock, "stringService"); given(mock.greeting()).willReturn("mocked"); assertThat(caller.sayGreeting()).isEqualTo("I say mocked 123"); diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoBeanWithMultipleExistingBeansAndExplicitQualifierIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoBeanWithMultipleExistingBeansAndExplicitQualifierIntegrationTests.java index b73390767a40..b7f0d54315b3 100644 --- a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoBeanWithMultipleExistingBeansAndExplicitQualifierIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoBeanWithMultipleExistingBeansAndExplicitQualifierIntegrationTests.java @@ -17,8 +17,6 @@ package org.springframework.test.context.bean.override.mockito.integration; import org.junit.jupiter.api.Test; -import org.mockito.MockingDetails; -import org.mockito.mock.MockCreationSettings; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; @@ -34,7 +32,8 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.then; -import static org.mockito.Mockito.mockingDetails; +import static org.springframework.test.mockito.MockitoAssertions.assertIsMock; +import static org.springframework.test.mockito.MockitoAssertions.assertMockName; /** * Tests that {@link MockitoBean @MockitoBean} can be used to mock a bean when @@ -60,10 +59,8 @@ class MockitoBeanWithMultipleExistingBeansAndExplicitQualifierIntegrationTests { @Test void test() { - MockingDetails mockingDetails = mockingDetails(mock); - MockCreationSettings mockSettings = mockingDetails.getMockCreationSettings(); - assertThat(mockingDetails.isMock()).as("is mock").isTrue(); - assertThat(mockSettings.getMockName()).as("mock name").hasToString("stringService"); + assertIsMock(mock); + assertMockName(mock, "stringService"); given(mock.greeting()).willReturn("mocked"); assertThat(caller.sayGreeting()).isEqualTo("I say mocked 123"); diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoBeanWithMultipleExistingBeansAndOnePrimaryIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoBeanWithMultipleExistingBeansAndOnePrimaryIntegrationTests.java index 777c22c18309..25d48d64145d 100644 --- a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoBeanWithMultipleExistingBeansAndOnePrimaryIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoBeanWithMultipleExistingBeansAndOnePrimaryIntegrationTests.java @@ -18,8 +18,6 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.MockingDetails; -import org.mockito.mock.MockCreationSettings; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; @@ -35,7 +33,8 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.then; -import static org.mockito.Mockito.mockingDetails; +import static org.springframework.test.mockito.MockitoAssertions.assertIsMock; +import static org.springframework.test.mockito.MockitoAssertions.assertMockName; /** * Tests that {@link MockitoBean @MockitoBean} can be used to mock a bean when @@ -59,10 +58,8 @@ class MockitoBeanWithMultipleExistingBeansAndOnePrimaryIntegrationTests { @Test void test() { - MockingDetails mockingDetails = mockingDetails(mock); - MockCreationSettings mockSettings = mockingDetails.getMockCreationSettings(); - assertThat(mockingDetails.isMock()).as("is mock").isTrue(); - assertThat(mockSettings.getMockName()).as("mock name").hasToString("two"); + assertIsMock(mock); + assertMockName(mock, "two"); given(mock.greeting()).willReturn("mocked"); assertThat(caller.sayGreeting()).isEqualTo("I say mocked 123"); diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanAndSpringAopProxyIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanAndSpringAopProxyIntegrationTests.java index fca180dd59da..80fcb6d08bed 100644 --- a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanAndSpringAopProxyIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanAndSpringAopProxyIntegrationTests.java @@ -20,7 +20,6 @@ import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mockito; import org.springframework.aop.support.AopUtils; import org.springframework.cache.CacheManager; @@ -44,6 +43,7 @@ import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.springframework.test.mockito.MockitoAssertions.assertIsSpy; /** * Tests for {@link MockitoSpyBean @MockitoSpyBean} used in combination with Spring AOP. @@ -79,7 +79,7 @@ void resetCache() { void stubAndVerifyOnUltimateTargetOfSpringAopProxy() { assertThat(AopUtils.isAopProxy(dateService)).as("is Spring AOP proxy").isTrue(); DateService spy = AopTestUtils.getUltimateTargetObject(dateService); - assertThat(Mockito.mockingDetails(spy).isSpy()).as("ultimate target is Mockito spy").isTrue(); + assertIsSpy(dateService, "ultimate target"); given(spy.getDate(false)).willReturn(1L); Long date = dateService.getDate(false); @@ -110,7 +110,7 @@ void stubAndVerifyOnUltimateTargetOfSpringAopProxy() { @RepeatedTest(2) void stubOnUltimateTargetAndVerifyOnSpringAopProxy() { assertThat(AopUtils.isAopProxy(dateService)).as("is Spring AOP proxy").isTrue(); - assertThat(Mockito.mockingDetails(dateService).isSpy()).as("Spring AOP proxy is Mockito spy").isTrue(); + assertIsSpy(dateService, "Spring AOP proxy"); DateService spy = AopTestUtils.getUltimateTargetObject(dateService); given(spy.getDate(false)).willReturn(1L); @@ -141,7 +141,7 @@ void stubOnUltimateTargetAndVerifyOnSpringAopProxy() { @RepeatedTest(2) void stubAndVerifyDirectlyOnSpringAopProxy() throws Exception { assertThat(AopUtils.isCglibProxy(dateService)).as("is Spring AOP CGLIB proxy").isTrue(); - assertThat(Mockito.mockingDetails(dateService).isSpy()).as("is Mockito spy").isTrue(); + assertIsSpy(dateService); doReturn(1L).when(dateService).getDate(false); Long date = dateService.getDate(false); diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanUsedDuringApplicationContextRefreshIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanUsedDuringApplicationContextRefreshIntegrationTests.java index ed36075ad774..1b2483beda02 100644 --- a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanUsedDuringApplicationContextRefreshIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanUsedDuringApplicationContextRefreshIntegrationTests.java @@ -18,7 +18,6 @@ import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Test; -import org.mockito.Mockito; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -29,9 +28,9 @@ import org.springframework.test.context.bean.override.mockito.MockitoSpyBean; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; -import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.same; import static org.mockito.BDDMockito.then; +import static org.springframework.test.mockito.MockitoAssertions.assertIsSpy; /** * Integration tests for {@link MockitoSpyBean @MockitoSpyBean} used during @@ -57,7 +56,8 @@ static void clearStaticField() { @Test void test() { - assertThat(Mockito.mockingDetails(eventProcessor).isSpy()).as("isSpy").isTrue(); + assertIsSpy(eventProcessor); + // Ensure that the spy was invoked during ApplicationContext refresh // and has not been reset in the interim. then(eventProcessor).should().process(same(contextRefreshedEvent)); diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanWithGenericsOnTestFieldForExistingGenericBeanProducedByFactoryBeanIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanWithGenericsOnTestFieldForExistingGenericBeanProducedByFactoryBeanIntegrationTests.java index c413d98cd35c..470479e4e169 100644 --- a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanWithGenericsOnTestFieldForExistingGenericBeanProducedByFactoryBeanIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanWithGenericsOnTestFieldForExistingGenericBeanProducedByFactoryBeanIntegrationTests.java @@ -17,7 +17,6 @@ package org.springframework.test.context.bean.override.mockito.integration; import org.junit.jupiter.api.Test; -import org.mockito.MockingDetails; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.support.BeanDefinitionRegistry; @@ -34,6 +33,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mockingDetails; +import static org.springframework.test.mockito.MockitoAssertions.assertIsSpy; /** * Tests that {@link MockitoSpyBean @MockitoSpyBean} on a field with generics can @@ -55,10 +55,10 @@ class MockitoSpyBeanWithGenericsOnTestFieldForExistingGenericBeanProducedByFacto @Test void testSpying() { - MockingDetails mockingDetails = mockingDetails(this.exampleService); - assertThat(mockingDetails.isSpy()).isTrue(); - assertThat(mockingDetails.getMockCreationSettings().getSpiedInstance()) - .isInstanceOf(StringExampleGenericService.class); + assertIsSpy(exampleService); + + Object spiedInstance = mockingDetails(exampleService).getMockCreationSettings().getSpiedInstance(); + assertThat(spiedInstance).isInstanceOf(StringExampleGenericService.class); } diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanWithMultipleExistingBeansAndExplicitBeanNameIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanWithMultipleExistingBeansAndExplicitBeanNameIntegrationTests.java index 2dd86743ba9c..b9cabfaaea8f 100644 --- a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanWithMultipleExistingBeansAndExplicitBeanNameIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanWithMultipleExistingBeansAndExplicitBeanNameIntegrationTests.java @@ -17,8 +17,6 @@ package org.springframework.test.context.bean.override.mockito.integration; import org.junit.jupiter.api.Test; -import org.mockito.MockingDetails; -import org.mockito.mock.MockCreationSettings; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; @@ -32,7 +30,8 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.then; -import static org.mockito.Mockito.mockingDetails; +import static org.springframework.test.mockito.MockitoAssertions.assertIsSpy; +import static org.springframework.test.mockito.MockitoAssertions.assertMockName; /** * Tests that {@link MockitoSpyBean @MockitoSpyBean} can be used to spy on a bean @@ -57,10 +56,8 @@ class MockitoSpyBeanWithMultipleExistingBeansAndExplicitBeanNameIntegrationTests @Test void test() { - MockingDetails mockingDetails = mockingDetails(spy); - MockCreationSettings mockSettings = mockingDetails.getMockCreationSettings(); - assertThat(mockingDetails.isSpy()).as("is spy").isTrue(); - assertThat(mockSettings.getMockName()).hasToString("stringService"); + assertIsSpy(spy); + assertMockName(spy, "stringService"); assertThat(caller.sayGreeting()).isEqualTo("I say two 123"); then(spy).should().greeting(); diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanWithMultipleExistingBeansAndExplicitQualifierIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanWithMultipleExistingBeansAndExplicitQualifierIntegrationTests.java index 472626f274f1..fe864dd2d799 100644 --- a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanWithMultipleExistingBeansAndExplicitQualifierIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanWithMultipleExistingBeansAndExplicitQualifierIntegrationTests.java @@ -17,8 +17,6 @@ package org.springframework.test.context.bean.override.mockito.integration; import org.junit.jupiter.api.Test; -import org.mockito.MockingDetails; -import org.mockito.mock.MockCreationSettings; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; @@ -33,7 +31,8 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.then; -import static org.mockito.Mockito.mockingDetails; +import static org.springframework.test.mockito.MockitoAssertions.assertIsSpy; +import static org.springframework.test.mockito.MockitoAssertions.assertMockName; /** * Tests that {@link MockitoSpyBean @MockitoSpyBean} can be used to spy on a bean @@ -59,10 +58,8 @@ class MockitoSpyBeanWithMultipleExistingBeansAndExplicitQualifierIntegrationTest @Test void test() { - MockingDetails mockingDetails = mockingDetails(spy); - MockCreationSettings mockSettings = mockingDetails.getMockCreationSettings(); - assertThat(mockingDetails.isSpy()).as("is spy").isTrue(); - assertThat(mockSettings.getMockName()).hasToString("stringService"); + assertIsSpy(spy); + assertMockName(spy, "stringService"); assertThat(caller.sayGreeting()).isEqualTo("I say two 123"); then(spy).should().greeting(); diff --git a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanWithMultipleExistingBeansAndOnePrimaryIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanWithMultipleExistingBeansAndOnePrimaryIntegrationTests.java index b0832e681c19..4084c5f192f7 100644 --- a/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanWithMultipleExistingBeansAndOnePrimaryIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanWithMultipleExistingBeansAndOnePrimaryIntegrationTests.java @@ -18,8 +18,6 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.MockingDetails; -import org.mockito.mock.MockCreationSettings; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; @@ -34,7 +32,8 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.then; -import static org.mockito.Mockito.mockingDetails; +import static org.springframework.test.mockito.MockitoAssertions.assertIsSpy; +import static org.springframework.test.mockito.MockitoAssertions.assertMockName; /** * Tests that {@link MockitoSpyBean @MockitoSpyBean} can be used to spy on a bean @@ -58,10 +57,8 @@ class MockitoSpyBeanWithMultipleExistingBeansAndOnePrimaryIntegrationTests { @Test void testSpying() { - MockingDetails mockingDetails = mockingDetails(spy); - MockCreationSettings mockSettings = mockingDetails.getMockCreationSettings(); - assertThat(mockingDetails.isSpy()).as("is spy").isTrue(); - assertThat(mockSettings.getMockName()).hasToString("two"); + assertIsSpy(spy); + assertMockName(spy, "two"); assertThat(caller.sayGreeting()).isEqualTo("I say two 123"); then(spy).should().greeting(); diff --git a/spring-test/src/test/java/org/springframework/test/mockito/MockitoAssertions.java b/spring-test/src/test/java/org/springframework/test/mockito/MockitoAssertions.java new file mode 100644 index 000000000000..b4a19e11994e --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/mockito/MockitoAssertions.java @@ -0,0 +1,69 @@ +/* + * Copyright 2002-2024 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.mockito; + +import org.mockito.mock.MockName; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mockingDetails; + +/** + * Assertions for Mockito mocks and spies. + * + * @author Sam Brannen + * @since 6.2.1 + */ +public abstract class MockitoAssertions { + + public static void assertIsMock(Object obj) { + assertThat(isMock(obj)).as("is a Mockito mock").isTrue(); + } + + public static void assertIsMock(Object obj, String message) { + assertThat(isMock(obj)).as("%s is a Mockito mock", message).isTrue(); + } + + public static void assertIsSpy(Object obj) { + assertThat(isSpy(obj)).as("is a Mockito spy").isTrue(); + } + + public static void assertIsSpy(Object obj, String message) { + assertThat(isSpy(obj)).as("%s is a Mockito spy", message).isTrue(); + } + + public static void assertIsNotSpy(Object obj) { + assertThat(isSpy(obj)).as("is a Mockito spy").isFalse(); + } + + public static void assertIsNotSpy(Object obj, String message) { + assertThat(isSpy(obj)).as("%s is a Mockito spy", message).isFalse(); + } + + public static void assertMockName(Object mock, String name) { + MockName mockName = mockingDetails(mock).getMockCreationSettings().getMockName(); + assertThat(mockName.toString()).as("mock name").isEqualTo(name); + } + + private static boolean isMock(Object obj) { + return mockingDetails(obj).isMock(); + } + + private static boolean isSpy(Object obj) { + return mockingDetails(obj).isSpy(); + } + +}