-
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.
Verify @MockitoBean can be used with generics and parameterized types
See gh-33742
- Loading branch information
Showing
2 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
...ext/bean/override/mockito/integration/AbstractMockitoBeanAndGenericsIntegrationTests.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,78 @@ | ||
/* | ||
* 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.integration; | ||
|
||
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.MockitoBean; | ||
import org.springframework.test.context.bean.override.mockito.integration.AbstractMockitoBeanAndGenericsIntegrationTests.Something; | ||
import org.springframework.test.context.bean.override.mockito.integration.AbstractMockitoBeanAndGenericsIntegrationTests.Thing; | ||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; | ||
|
||
/** | ||
* Abstract base class for tests for {@link MockitoBean @MockitoBean} with generics. | ||
* | ||
* @param <T> type of thing | ||
* @param <S> type of something | ||
* @author Madhura Bhave | ||
* @author Sam Brannen | ||
* @since 6.2 | ||
* @see MockitoBeanAndGenericsIntegrationTests | ||
*/ | ||
@SpringJUnitConfig | ||
abstract class AbstractMockitoBeanAndGenericsIntegrationTests<T extends Thing<S>, S extends Something> { | ||
|
||
@Autowired | ||
T thing; | ||
|
||
@MockitoBean | ||
S something; | ||
|
||
|
||
static class Something { | ||
String speak() { | ||
return "Hi"; | ||
} | ||
} | ||
|
||
static class SomethingImpl extends Something { | ||
} | ||
|
||
abstract static class Thing<S extends Something> { | ||
|
||
@Autowired | ||
private S something; | ||
|
||
S getSomething() { | ||
return this.something; | ||
} | ||
} | ||
|
||
static class ThingImpl extends Thing<SomethingImpl> { | ||
} | ||
|
||
@Configuration(proxyBeanMethods = false) | ||
static class Config { | ||
|
||
@Bean | ||
ThingImpl thing() { | ||
return new ThingImpl(); | ||
} | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...est/context/bean/override/mockito/integration/MockitoBeanAndGenericsIntegrationTests.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,44 @@ | ||
/* | ||
* Copyright 2012-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.integration; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.test.context.bean.override.mockito.integration.AbstractMockitoBeanAndGenericsIntegrationTests.SomethingImpl; | ||
import org.springframework.test.context.bean.override.mockito.integration.AbstractMockitoBeanAndGenericsIntegrationTests.ThingImpl; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.BDDMockito.when; | ||
|
||
/** | ||
* Concrete implementation of {@link AbstractMockitoBeanAndGenericsIntegrationTests}. | ||
* | ||
* @author Madhura Bhave | ||
* @author Sam Brannen | ||
* @since 6.2 | ||
*/ | ||
class MockitoBeanAndGenericsIntegrationTests extends AbstractMockitoBeanAndGenericsIntegrationTests<ThingImpl, SomethingImpl> { | ||
|
||
@Test | ||
void mockitoBeanShouldResolveConcreteType() { | ||
assertThat(something).isExactlyInstanceOf(SomethingImpl.class); | ||
|
||
when(something.speak()).thenReturn("Hola"); | ||
assertThat(thing.getSomething().speak()).isEqualTo("Hola"); | ||
} | ||
|
||
} |