Commit 16fe332f authored by Madhura Bhave's avatar Madhura Bhave Committed by Phillip Webb

Fix NoClassDefFoundError when Mockito is missing

Update MockReset class to check for the presence of the MockUtil class
before attempting to use it.

Fixes gh-7065
parent 3326841a
...@@ -82,6 +82,7 @@ ...@@ -82,6 +82,7 @@
<module>spring-boot-sample-session-redis</module> <module>spring-boot-sample-session-redis</module>
<module>spring-boot-sample-simple</module> <module>spring-boot-sample-simple</module>
<module>spring-boot-sample-test</module> <module>spring-boot-sample-test</module>
<module>spring-boot-sample-test-nomockito</module>
<module>spring-boot-sample-testng</module> <module>spring-boot-sample-testng</module>
<module>spring-boot-sample-tomcat</module> <module>spring-boot-sample-tomcat</module>
<module>spring-boot-sample-tomcat-jsp</module> <module>spring-boot-sample-tomcat-jsp</module>
......
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<!-- Your own application should inherit from spring-boot-starter-parent -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-samples</artifactId>
<version>1.4.2.BUILD-SNAPSHOT</version>
</parent>
<artifactId>spring-boot-sample-test-nomockito</artifactId>
<name>Spring Boot Test Sample No Mockito</name>
<description>Spring Boot Test Sample No Mockito</description>
<url>http://projects.spring.io/spring-boot/</url>
<organization>
<name>Pivotal Software, Inc.</name>
<url>http://www.spring.io</url>
</organization>
<properties>
<main.basedir>${basedir}/../..</main.basedir>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
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);
}
}
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();
}
}
...@@ -26,6 +26,7 @@ import org.mockito.listeners.MethodInvocationReport; ...@@ -26,6 +26,7 @@ import org.mockito.listeners.MethodInvocationReport;
import org.mockito.mock.MockCreationSettings; import org.mockito.mock.MockCreationSettings;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/** /**
* Reset strategy used on a mock bean. Usually applied to a mock via the * Reset strategy used on a mock bean. Usually applied to a mock via the
...@@ -53,8 +54,6 @@ public enum MockReset { ...@@ -53,8 +54,6 @@ public enum MockReset {
*/ */
NONE; NONE;
private static final MockUtil util = new MockUtil();
/** /**
* Create {@link MockSettings settings} to be used with mocks where reset should occur * Create {@link MockSettings settings} to be used with mocks where reset should occur
* before each test method runs. * before each test method runs.
...@@ -105,12 +104,15 @@ public enum MockReset { ...@@ -105,12 +104,15 @@ public enum MockReset {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
static MockReset get(Object mock) { static MockReset get(Object mock) {
MockReset reset = MockReset.NONE; MockReset reset = MockReset.NONE;
if (util.isMock(mock)) { if (ClassUtils.isPresent("org.mockito.internal.util.MockUtil", null)) {
MockCreationSettings settings = util.getMockSettings(mock); MockUtil mockUtil = new MockUtil();
List listeners = settings.getInvocationListeners(); if (mockUtil.isMock(mock)) {
for (Object listener : listeners) { MockCreationSettings settings = mockUtil.getMockSettings(mock);
if (listener instanceof ResetInvocationListener) { List listeners = settings.getInvocationListeners();
reset = ((ResetInvocationListener) listener).getReset(); for (Object listener : listeners) {
if (listener instanceof ResetInvocationListener) {
reset = ((ResetInvocationListener) listener).getReset();
}
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment