From eb927f1b295950d21c9cbaae57c1e6571e0ea36e Mon Sep 17 00:00:00 2001 From: Aleksander Bartnikiewicz Date: Tue, 1 Nov 2016 20:59:10 +0100 Subject: [PATCH 1/2] Prevent a broken factory bean from breaking the resetting of mocks Closes gh-7271 --- .../boot/test/mock/mockito/ResetMocksTestExecutionListener.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/ResetMocksTestExecutionListener.java b/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/ResetMocksTestExecutionListener.java index 987564ae7c..2d9f257d86 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/ResetMocksTestExecutionListener.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/ResetMocksTestExecutionListener.java @@ -65,7 +65,7 @@ public class ResetMocksTestExecutionListener extends AbstractTestExecutionListen for (String name : names) { BeanDefinition definition = beanFactory.getBeanDefinition(name); if (definition.isSingleton() && instantiatedSingletons.contains(name)) { - Object bean = beanFactory.getBean(name); + Object bean = beanFactory.getSingleton(name); if (reset.equals(MockReset.get(bean))) { Mockito.reset(bean); } From 46e8cf4a4326ae87fe54f6ab96c20da69f69a44d Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 25 Nov 2016 10:35:23 +0000 Subject: [PATCH 2/2] Test that a broken factory bean does not break resetting of mocks Previously, ResetMocksTestExecutionListener used getBean(name) to retrieve each instantiated singleton. When the instantiated singleton was a factory bean, this would cause getObject on the factory bean to be called. If the factory bean was unable to produce its object, for example due to test slicing excluding something, an exception would be thrown. The previous commit updated ResetMocksTestsExecutionListener to use getSingleton(name) rather than getBean(name). This will retrieve the factory bean itself rather than causing the factory bean to attempt to create an object. This commit updates the tests to verify the new behaviour. Closes gh-7270 --- .../ResetMocksTestExecutionListenerTests.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/ResetMocksTestExecutionListenerTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/ResetMocksTestExecutionListenerTests.java index 025058aff3..e28ce99623 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/ResetMocksTestExecutionListenerTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/ResetMocksTestExecutionListenerTests.java @@ -21,6 +21,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.MethodSorters; +import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.example.ExampleService; import org.springframework.context.ApplicationContext; @@ -37,6 +38,7 @@ import static org.mockito.Mockito.mock; * Tests for {@link ResetMocksTestExecutionListener}. * * @author Phillip Webb + * @author Andy Wilkinson */ @RunWith(SpringRunner.class) @FixMethodOrder(MethodSorters.NAME_ASCENDING) @@ -94,6 +96,31 @@ public class ResetMocksTestExecutionListenerTests { throw new RuntimeException(); } + @Bean + public BrokenFactoryBean brokenFactoryBean() { + // gh-7270 + return new BrokenFactoryBean(); + } + + } + + static class BrokenFactoryBean implements FactoryBean { + + @Override + public String getObject() throws Exception { + throw new IllegalStateException(); + } + + @Override + public Class getObjectType() { + return String.class; + } + + @Override + public boolean isSingleton() { + return true; + } + } }