Reset mocks produced by FactoryBeans

An unwanted side-effect of the changes made in c6bdd136 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 c6bdd136, however the call is now
performed in a defensive manner falling back to getSingleton(…) when
it fails.

Closes gh-33830
This commit is contained in:
Andy Wilkinson
2023-02-01 17:22:36 +00:00
parent 4aa82a3dc0
commit 9940fcfe77
2 changed files with 38 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2023 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.
@@ -78,7 +78,7 @@ public class ResetMocksTestExecutionListener extends AbstractTestExecutionListen
for (String name : names) {
BeanDefinition definition = beanFactory.getBeanDefinition(name);
if (definition.isSingleton() && instantiatedSingletons.contains(name)) {
Object bean = beanFactory.getSingleton(name);
Object bean = getBean(beanFactory, name);
if (reset.equals(MockReset.get(bean))) {
Mockito.reset(bean);
}
@@ -100,4 +100,13 @@ public class ResetMocksTestExecutionListener extends AbstractTestExecutionListen
}
}
private Object getBean(ConfigurableListableBeanFactory beanFactory, String name) {
try {
return beanFactory.getBean(name);
}
catch (Exception ex) {
return beanFactory.getSingleton(name);
}
}
}