Skip to content

Commit f8ef908

Browse files
committed
Allow @MockBean to be used with Framework's @repeat
Fixes spring-projectsgh-27693
1 parent 02a988e commit f8ef908

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/MockitoPostProcessor.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -355,9 +355,13 @@ void inject(Field field, Object target, Definition definition) {
355355
private void inject(Field field, Object target, String beanName) {
356356
try {
357357
field.setAccessible(true);
358-
Assert.state(ReflectionUtils.getField(field, target) == null,
359-
() -> "The field " + field + " cannot have an existing value");
358+
Object existingValue = ReflectionUtils.getField(field, target);
360359
Object bean = this.beanFactory.getBean(beanName, field.getType());
360+
if (existingValue == bean) {
361+
return;
362+
}
363+
Assert.state(existingValue == null, () -> "The existing value '" + existingValue + "' of field '" + field
364+
+ "' is not the same as the new value '" + bean + "'");
361365
ReflectionUtils.setField(field, target, bean);
362366
}
363367
catch (Throwable ex) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.mock.mockito;
18+
19+
import org.junit.AfterClass;
20+
import org.junit.Rule;
21+
import org.junit.Test;
22+
23+
import org.springframework.test.annotation.Repeat;
24+
import org.springframework.test.context.junit4.rules.SpringMethodRule;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Tests for {@link MockBean} and {@link Repeat}.
30+
*
31+
* @author Andy Wilkinson
32+
* @see <a href="https://github.com/spring-projects/spring-boot/issues/27693">gh-27693</a>
33+
*/
34+
public class MockBeanWithSpringMethodRuleRepeatJUnit4IntegrationTests {
35+
36+
@Rule
37+
public final SpringMethodRule springMethodRule = new SpringMethodRule();
38+
39+
@MockBean
40+
private FirstService first;
41+
42+
private static int invocations;
43+
44+
@AfterClass
45+
public static void afterClass() {
46+
assertThat(invocations).isEqualTo(2);
47+
}
48+
49+
@Test
50+
@Repeat(2)
51+
public void repeatedTest() {
52+
invocations++;
53+
}
54+
55+
interface FirstService {
56+
57+
String greeting();
58+
59+
}
60+
61+
}

src/checkstyle/checkstyle-suppressions.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<suppress files="SampleLogbackApplication\.java" checks="IllegalImport" />
4141
<suppress files="FlywayAutoConfigurationTests\.java" checks="IllegalImport" />
4242
<suppress files="[\\/]src[\\/]test[\\/]java[\\/]org[\\/]springframework[\\/]boot[\\/]test[\\/]rule[\\/]" checks="SpringJUnit5" />
43+
<suppress files="MockBeanWithSpringMethodRuleRepeatJUnit4IntegrationTests" checks="SpringJUnit5" />
4344
<suppress files="OutputCaptureRuleTests" checks="SpringJUnit5" />
4445
<suppress files="SampleJUnitVintageApplicationTests" checks="SpringJUnit5" />
4546
<suppress files="[\\/]spring-boot-docs[\\/]" checks="SpringJavadoc" message="\@since" />

0 commit comments

Comments
 (0)