Skip to content

Commit

Permalink
Merge branch '6.2.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Dec 8, 2024
2 parents b904753 + aee52b5 commit 2015a93
Show file tree
Hide file tree
Showing 21 changed files with 319 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
* @author Sam Brannen
* @since 6.2.1
* @see <a href="https://github.com/spring-projects/spring-framework/issues/34025">gh-34025</a>
* @see MockitoSpyBeanDuplicateTypeIntegrationTests
* @see MockitoSpyBeanDuplicateTypeAndNameIntegrationTests
*/
@SpringJUnitConfig
public class MockitoBeanDuplicateTypeIntegrationTests {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
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;
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 org.springframework.test.mockito.MockitoAssertions;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -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);

Expand All @@ -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();
Expand All @@ -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);
}
Expand All @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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");

Expand All @@ -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));

Expand All @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ExampleService> 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");
}
}

}
Original file line number Diff line number Diff line change
@@ -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<ExampleService> 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");
}
}

}
Loading

0 comments on commit 2015a93

Please sign in to comment.