org.mockito.internal.configuration.ClassPathLoader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mockito-core Show documentation
Show all versions of mockito-core Show documentation
Mockito mock objects library core API and implementation
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.configuration;
import org.mockito.configuration.IMockitoConfiguration;
import org.mockito.exceptions.misusing.MockitoConfigurationException;
import org.mockito.plugins.MockMaker;
/**
* Loads configuration or extension points available in the classpath.
*
*
*
* -
* Can load the mockito configuration. The user who want to provide his own mockito configuration
* should write the class
org.mockito.configuration.MockitoConfiguration
that implements
* {@link IMockitoConfiguration}. For example :
*
* package org.mockito.configuration;
*
* //...
*
* public class MockitoConfiguration implements IMockitoConfiguration {
* boolean enableClassCache() { return false; }
*
* // ...
* }
*
*
* -
* Can load available mockito extensions. Currently Mockito only have one extension point the
* {@link MockMaker}. This extension point allows a user to provide his own bytecode engine to build mocks.
*
Suppose you wrote an extension to create mocks with some Awesome library, in order to tell
* Mockito to use it you need to put in your classpath
*
* - The implementation itself, for example
org.awesome.mockito.AwesomeMockMaker
.
* - A file named
org.mockito.plugins.MockMaker
in a folder named
* mockito-extensions
, the content of this file need to have one line with
* the qualified name org.awesome.mockito.AwesomeMockMaker
.
*
*
*
*
*/
public class ClassPathLoader {
public static final String MOCKITO_CONFIGURATION_CLASS_NAME =
"org.mockito.configuration.MockitoConfiguration";
/**
* @return configuration loaded from classpath or null
*/
@SuppressWarnings({"unchecked"})
public IMockitoConfiguration loadConfiguration() {
// Trying to get config from classpath
Class configClass;
try {
configClass = Class.forName(MOCKITO_CONFIGURATION_CLASS_NAME);
} catch (ClassNotFoundException e) {
// that's ok, it means there is no global config, using default one.
return null;
}
try {
return (IMockitoConfiguration) configClass.getDeclaredConstructor().newInstance();
} catch (ClassCastException e) {
throw new MockitoConfigurationException(
"MockitoConfiguration class must implement "
+ IMockitoConfiguration.class.getName()
+ " interface.",
e);
} catch (Exception e) {
throw new MockitoConfigurationException(
"Unable to instantiate "
+ MOCKITO_CONFIGURATION_CLASS_NAME
+ " class. Does it have a safe, no-arg constructor?",
e);
}
}
}