diff --git a/spring-boot-samples/pom.xml b/spring-boot-samples/pom.xml index 221d4d9929..07e1f2fe34 100644 --- a/spring-boot-samples/pom.xml +++ b/spring-boot-samples/pom.xml @@ -82,6 +82,7 @@ spring-boot-sample-session-redis spring-boot-sample-simple spring-boot-sample-test + spring-boot-sample-test-nomockito spring-boot-sample-testng spring-boot-sample-tomcat spring-boot-sample-tomcat-jsp diff --git a/spring-boot-samples/spring-boot-sample-test-nomockito/pom.xml b/spring-boot-samples/spring-boot-sample-test-nomockito/pom.xml new file mode 100644 index 0000000000..289e01c32b --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-test-nomockito/pom.xml @@ -0,0 +1,54 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-samples + 1.4.2.BUILD-SNAPSHOT + + spring-boot-sample-test-nomockito + Spring Boot Test Sample No Mockito + Spring Boot Test Sample No Mockito + http://projects.spring.io/spring-boot/ + + Pivotal Software, Inc. + http://www.spring.io + + + ${basedir}/../.. + + + + org.springframework.boot + spring-boot-starter + + + junit + junit + test + + + org.springframework.boot + spring-boot-test + test + + + org.springframework + spring-test + test + + + org.assertj + assertj-core + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/spring-boot-samples/spring-boot-sample-test-nomockito/src/main/java/sample/testnomockito/SampleTestNoMockitoApplication.java b/spring-boot-samples/spring-boot-sample-test-nomockito/src/main/java/sample/testnomockito/SampleTestNoMockitoApplication.java new file mode 100644 index 0000000000..550aa84d6e --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-test-nomockito/src/main/java/sample/testnomockito/SampleTestNoMockitoApplication.java @@ -0,0 +1,13 @@ +package sample.testnomockito; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SampleTestNoMockitoApplication { + + public static void main(String[] args) { + SpringApplication.run(SampleTestNoMockitoApplication.class); + } + +} diff --git a/spring-boot-samples/spring-boot-sample-test-nomockito/src/test/java/sample/testnomockito/SampleTestNoMockitoApplicationTest.java b/spring-boot-samples/spring-boot-sample-test-nomockito/src/test/java/sample/testnomockito/SampleTestNoMockitoApplicationTest.java new file mode 100644 index 0000000000..589bb0c4f1 --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-test-nomockito/src/test/java/sample/testnomockito/SampleTestNoMockitoApplicationTest.java @@ -0,0 +1,32 @@ +package sample.testnomockito; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests that {code ResetMocksTestExecutionListener} and + * {@code MockitoTestExecutionListener} gracefully degrade when Mockito is not on the + * classpath. + * + * @author Madhura Bhave + */ +@RunWith(SpringRunner.class) +public class SampleTestNoMockitoApplicationTest { + + // gh-7065 + + @Autowired + private ApplicationContext context; + + @Test + public void contextLoads() throws Exception { + assertThat(this.context).isNotNull(); + } + +} diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/MockReset.java b/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/MockReset.java index f0042116d5..5cef1c2cc5 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/MockReset.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/MockReset.java @@ -26,6 +26,7 @@ import org.mockito.listeners.MethodInvocationReport; import org.mockito.mock.MockCreationSettings; import org.springframework.util.Assert; +import org.springframework.util.ClassUtils; /** * Reset strategy used on a mock bean. Usually applied to a mock via the @@ -53,8 +54,6 @@ public enum MockReset { */ NONE; - private static final MockUtil util = new MockUtil(); - /** * Create {@link MockSettings settings} to be used with mocks where reset should occur * before each test method runs. @@ -105,12 +104,15 @@ public enum MockReset { @SuppressWarnings("rawtypes") static MockReset get(Object mock) { MockReset reset = MockReset.NONE; - if (util.isMock(mock)) { - MockCreationSettings settings = util.getMockSettings(mock); - List listeners = settings.getInvocationListeners(); - for (Object listener : listeners) { - if (listener instanceof ResetInvocationListener) { - reset = ((ResetInvocationListener) listener).getReset(); + if (ClassUtils.isPresent("org.mockito.internal.util.MockUtil", null)) { + MockUtil mockUtil = new MockUtil(); + if (mockUtil.isMock(mock)) { + MockCreationSettings settings = mockUtil.getMockSettings(mock); + List listeners = settings.getInvocationListeners(); + for (Object listener : listeners) { + if (listener instanceof ResetInvocationListener) { + reset = ((ResetInvocationListener) listener).getReset(); + } } } }