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
This commit is contained in:
		
							parent
							
								
									3326841a97
								
							
						
					
					
						commit
						16fe332f51
					
				| 
						 | 
				
			
			@ -82,6 +82,7 @@
 | 
			
		|||
		<module>spring-boot-sample-session-redis</module>
 | 
			
		||||
		<module>spring-boot-sample-simple</module>
 | 
			
		||||
		<module>spring-boot-sample-test</module>
 | 
			
		||||
		<module>spring-boot-sample-test-nomockito</module>
 | 
			
		||||
		<module>spring-boot-sample-testng</module>
 | 
			
		||||
		<module>spring-boot-sample-tomcat</module>
 | 
			
		||||
		<module>spring-boot-sample-tomcat-jsp</module>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,54 @@
 | 
			
		|||
	<?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>
 | 
			
		||||
| 
						 | 
				
			
			@ -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);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -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();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -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,8 +104,10 @@ public enum MockReset {
 | 
			
		|||
	@SuppressWarnings("rawtypes")
 | 
			
		||||
	static MockReset get(Object mock) {
 | 
			
		||||
		MockReset reset = MockReset.NONE;
 | 
			
		||||
		if (util.isMock(mock)) {
 | 
			
		||||
			MockCreationSettings settings = util.getMockSettings(mock);
 | 
			
		||||
		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) {
 | 
			
		||||
| 
						 | 
				
			
			@ -114,6 +115,7 @@ public enum MockReset {
 | 
			
		|||
					}
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		return reset;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue