Skip to content

Commit 4900ca1

Browse files
committed
Fix resetting of spied FactoryBean output
Fixes gh-31204
1 parent e089b90 commit 4900ca1

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ protected final Object createSpyIfNecessary(Object bean, String beanName) throws
330330
SpyDefinition definition = this.spies.get(beanName);
331331
if (definition != null) {
332332
bean = definition.createSpy(beanName, bean);
333+
this.mockitoBeans.add(bean);
333334
}
334335
return bean;
335336
}

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 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.
@@ -47,13 +47,17 @@ class ResetMocksTestExecutionListenerTests {
4747
@Autowired
4848
private ApplicationContext context;
4949

50+
@SpyBean
51+
ToSpy spied;
52+
5053
@Test
5154
void test001() {
5255
given(getMock("none").greeting()).willReturn("none");
5356
given(getMock("before").greeting()).willReturn("before");
5457
given(getMock("after").greeting()).willReturn("after");
5558
given(getMock("fromFactoryBean").greeting()).willReturn("fromFactoryBean");
5659
assertThat(this.context.getBean(NonSingletonFactoryBean.class).getObjectInvocations).isEqualTo(0);
60+
given(this.spied.action()).willReturn("spied");
5761
}
5862

5963
@Test
@@ -63,6 +67,7 @@ void test002() {
6367
assertThat(getMock("after").greeting()).isNull();
6468
assertThat(getMock("fromFactoryBean").greeting()).isNull();
6569
assertThat(this.context.getBean(NonSingletonFactoryBean.class).getObjectInvocations).isEqualTo(0);
70+
assertThat(this.spied.action()).isNull();
6671
}
6772

6873
ExampleService getMock(String name) {
@@ -116,6 +121,11 @@ NonSingletonFactoryBean nonSingletonFactoryBean() {
116121
return new NonSingletonFactoryBean();
117122
}
118123

124+
@Bean
125+
ToSpyFactoryBean toSpyFactoryBean() {
126+
return new ToSpyFactoryBean();
127+
}
128+
119129
}
120130

121131
static class BrokenFactoryBean implements FactoryBean<String> {
@@ -158,6 +168,14 @@ public boolean isSingleton() {
158168

159169
}
160170

171+
static class ToSpy {
172+
173+
String action() {
174+
return null;
175+
}
176+
177+
}
178+
161179
static class NonSingletonFactoryBean implements FactoryBean<ExampleService> {
162180

163181
private int getObjectInvocations = 0;
@@ -180,4 +198,18 @@ public boolean isSingleton() {
180198

181199
}
182200

201+
static class ToSpyFactoryBean implements FactoryBean<ToSpy> {
202+
203+
@Override
204+
public ToSpy getObject() throws Exception {
205+
return new ToSpy();
206+
}
207+
208+
@Override
209+
public Class<?> getObjectType() {
210+
return ToSpy.class;
211+
}
212+
213+
}
214+
183215
}

0 commit comments

Comments
 (0)