From 2fc86b2d5a2faa6e4f817f2ee67d96ca155b1293 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 16 May 2016 13:03:04 -0700 Subject: [PATCH] Only attempt mock reset on instantiated singletons Update ResetMocksTestExecutionListener to only attempt a mock reset when a singleton has actually been created. Fixes gh-5870 --- .../mock/mockito/ResetMocksTestExecutionListener.java | 11 +++++++---- .../mockito/ResetMocksTestExecutionListenerTests.java | 8 ++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) 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 9d02dc2084..155814906c 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 @@ -16,11 +16,14 @@ package org.springframework.boot.test.mock.mockito; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + import org.mockito.Mockito; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; -import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.test.context.TestContext; @@ -56,11 +59,11 @@ public class ResetMocksTestExecutionListener extends AbstractTestExecutionListen MockReset reset) { ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory(); String[] names = beanFactory.getBeanDefinitionNames(); + Set instantiatedSingletons = new HashSet( + Arrays.asList(beanFactory.getSingletonNames())); for (String name : names) { BeanDefinition definition = beanFactory.getBeanDefinition(name); - String scope = definition.getScope(); - if (AbstractBeanDefinition.SCOPE_DEFAULT.equals(scope) - || BeanDefinition.SCOPE_SINGLETON.equals(scope)) { + if (definition.isSingleton() && instantiatedSingletons.contains(name)) { Object bean = beanFactory.getBean(name); if (reset.equals(MockReset.get(bean))) { Mockito.reset(bean); 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 0c4e1e0b25..24d7dce2a8 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 @@ -26,6 +26,7 @@ import org.springframework.boot.test.mock.mockito.example.ExampleService; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; import org.springframework.test.context.junit4.SpringRunner; import static org.assertj.core.api.Assertions.assertThat; @@ -80,6 +81,13 @@ public class ResetMocksTestExecutionListenerTests { return mock(ExampleService.class); } + @Bean + @Lazy + public ExampleService fail() { + // gh-5870 + throw new RuntimeException(); + } + } }