com.fitbur.mockito.internal.configuration.ClassPathLoader Maven / Gradle / Ivy
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package com.fitbur.mockito.internal.configuration;
import com.fitbur.mockito.configuration.IMockitoConfiguration;
import com.fitbur.mockito.exceptions.misusing.MockitoConfigurationException;
import com.fitbur.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
com.fitbur.mockito.configuration.MockitoConfiguration
that implements
* {@link IMockitoConfiguration}. For example :
*
* package com.fitbur.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
com.fitbur.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 = "com.fitbur.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.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);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy