Skip to content

Commit 9940fcf

Browse files
committed
Reset mocks produced by FactoryBeans
An unwanted side-effect of the changes made in c6bdd13 to fix gh-7271 is that a mock produced by a factory bean is not reset. To allow such a mock to be reset without regressing the fix we now call getBean(…) as we did before c6bdd13, however the call is now performed in a defensive manner falling back to getSingleton(…) when it fails. Closes gh-33830
1 parent 4aa82a3 commit 9940fcf

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2023 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.
@@ -78,7 +78,7 @@ private void resetMocks(ConfigurableApplicationContext applicationContext, MockR
7878
for (String name : names) {
7979
BeanDefinition definition = beanFactory.getBeanDefinition(name);
8080
if (definition.isSingleton() && instantiatedSingletons.contains(name)) {
81-
Object bean = beanFactory.getSingleton(name);
81+
Object bean = getBean(beanFactory, name);
8282
if (reset.equals(MockReset.get(bean))) {
8383
Mockito.reset(bean);
8484
}
@@ -100,4 +100,13 @@ private void resetMocks(ConfigurableApplicationContext applicationContext, MockR
100100
}
101101
}
102102

103+
private Object getBean(ConfigurableListableBeanFactory beanFactory, String name) {
104+
try {
105+
return beanFactory.getBean(name);
106+
}
107+
catch (Exception ex) {
108+
return beanFactory.getSingleton(name);
109+
}
110+
}
111+
103112
}

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

Lines changed: 27 additions & 1 deletion
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-2023 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.
@@ -52,13 +52,15 @@ void test001() {
5252
given(getMock("none").greeting()).willReturn("none");
5353
given(getMock("before").greeting()).willReturn("before");
5454
given(getMock("after").greeting()).willReturn("after");
55+
given(getMock("fromFactoryBean").greeting()).willReturn("fromFactoryBean");
5556
}
5657

5758
@Test
5859
void test002() {
5960
assertThat(getMock("none").greeting()).isEqualTo("none");
6061
assertThat(getMock("before").greeting()).isNull();
6162
assertThat(getMock("after").greeting()).isNull();
63+
assertThat(getMock("fromFactoryBean").greeting()).isNull();
6264
}
6365

6466
ExampleService getMock(String name) {
@@ -102,6 +104,11 @@ BrokenFactoryBean brokenFactoryBean() {
102104
return new BrokenFactoryBean();
103105
}
104106

107+
@Bean
108+
WorkingFactoryBean fromFactoryBean() {
109+
return new WorkingFactoryBean();
110+
}
111+
105112
}
106113

107114
static class BrokenFactoryBean implements FactoryBean<String> {
@@ -123,4 +130,23 @@ public boolean isSingleton() {
123130

124131
}
125132

133+
static class WorkingFactoryBean implements FactoryBean<ExampleService> {
134+
135+
@Override
136+
public ExampleService getObject() {
137+
return mock(ExampleService.class, MockReset.before());
138+
}
139+
140+
@Override
141+
public Class<?> getObjectType() {
142+
return ExampleService.class;
143+
}
144+
145+
@Override
146+
public boolean isSingleton() {
147+
return true;
148+
}
149+
150+
}
151+
126152
}

0 commit comments

Comments
 (0)