-
Notifications
You must be signed in to change notification settings - Fork 38.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
175 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...ramework/test/context/bean/override/mockito/MockitoBeanDuplicateTypeIntegrationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* 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.test.context.bean.override.example.ExampleService; | ||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Integration tests for {@link MockitoBean @MockitoBean} where duplicate mocks | ||
* are created for the same nonexistent type. | ||
* | ||
* @author Sam Brannen | ||
* @since 6.2.1 | ||
* @see <a href="https://github.com/spring-projects/spring-framework/issues/34025">gh-34025</a> | ||
*/ | ||
@SpringJUnitConfig | ||
public class MockitoBeanDuplicateTypeIntegrationTests { | ||
|
||
@MockitoBean | ||
ExampleService service1; | ||
|
||
@MockitoBean | ||
ExampleService service2; | ||
|
||
@Autowired | ||
List<ExampleService> services; | ||
|
||
|
||
@Test | ||
void duplicateMocksShouldHaveBeenCreated() { | ||
assertThat(service1).isNotSameAs(service2); | ||
assertThat(services).hasSize(2); | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
...mework/test/context/bean/override/mockito/MockitoBeanSuperAndSubtypeIntegrationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* 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.test.context.junit.jupiter.SpringJUnitConfig; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Integration tests for {@link MockitoBean @MockitoBean} where mocks are created | ||
* for nonexistent beans for a supertype and subtype of that supertype. | ||
* | ||
* <p>This test class is designed to reproduce scenarios that previously failed | ||
* along the lines of the following. | ||
* | ||
* <p>BeanNotOfRequiredTypeException: Bean named 'Subtype#0' is expected to be | ||
* of type 'Subtype' but was actually of type 'Supertype$MockitoMock$XHb7Aspo' | ||
* | ||
* @author Sam Brannen | ||
* @since 6.2.1 | ||
* @see <a href="https://github.com/spring-projects/spring-framework/issues/34025">gh-34025</a> | ||
*/ | ||
@SpringJUnitConfig | ||
public class MockitoBeanSuperAndSubtypeIntegrationTests { | ||
|
||
// The declaration order of the following fields is intentional, and prior | ||
// to fixing gh-34025 this test class consistently failed on JDK 17. | ||
|
||
@MockitoBean | ||
Subtype subtype; | ||
|
||
@MockitoBean | ||
Supertype supertype; | ||
|
||
|
||
@Autowired | ||
List<Supertype> supertypes; | ||
|
||
|
||
@Test | ||
void bothMocksShouldHaveBeenCreated() { | ||
assertThat(supertype).isNotSameAs(subtype); | ||
assertThat(supertypes).hasSize(2); | ||
} | ||
|
||
|
||
interface Supertype { | ||
} | ||
|
||
interface Subtype extends Supertype { | ||
} | ||
|
||
} |