org.jmockring.JMockring Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jmockring-core Show documentation
Show all versions of jmockring-core Show documentation
jmockring - Java test MOCKing tool for spRING.
A test harness tool for projects using the following technology stack:
- Java 6+
- Spring 3.1 or greater as a DI/Deployment container
- JUnit 4 and Mockito for testing
- Jetty/Servlet API 3.0 for web app (war) deployment - for testing only
Main features:
1) Partial Spring context deployment with automatic bean mocking for unavailable beans
2) Bootstrapping embedded Jetty server via JUnit runners
3) Configurable web application contexts
4) Automatic injection of Spring beans and mocks in JUnit tests via Java5 annotations
package org.jmockring;
import org.jmockring.configuration.ServerConfiguration;
import org.jmockring.configuration.ServerExecutionConfiguration;
import org.jmockring.configuration.ServerExecutionRegistry;
import org.jmockring.spi.MockProviderSPI;
import org.jmockring.spi.PluggableServiceLoader;
/**
* General utilities for making JMockring more useful.
*
* @author Pavel Lechev
* @since 29/07/13
*/
public final class JMockring {
private static final MockProviderSPI mockingProvider = PluggableServiceLoader.loadMockingProvider(false);
/**
* Convenience method for suspending the test execution indefinitely.
* The call to it will typically be either in the test #setUp() method or at the very beginning of the test method itself.
*
* Once this is called, it suspends the test execution thread and the only way to continue is to forcefully shut down the test.
*
* It may be used for experimenting when we need to connect to the running server from outside our test.
* For example: using tools like SoapUI, Postman, RESTCLient, etc ...
*
* Effectively this gives us a running web server with our application deployed on it and allows us to
* experiment with different request and monitor the responses (headers, status codes, cookies, etc ...) or simply debug the application.
*
*
* !!! WARNING !!!
*
* Be careful when using this method and ensure no calls to it remain after the experimentation is complete.
* If such calls leak into the CI environment, it will cause the build to hang on forever.
*/
public static void halt() {
ServerExecutionConfiguration[] servers = ServerExecutionRegistry.getAllServers();
String hosts = "";
for (ServerExecutionConfiguration server : servers) {
ServerConfiguration serverConfiguration = server.getConfiguration();
hosts += String.format("\n\t%s://%s:%d", serverConfiguration.getScheme(), serverConfiguration.getHost(), serverConfiguration.getPort());
hosts = String.format("\n\t%s://%s:%d",
server.getConfiguration().getScheme(),
server.getConfiguration().getServerConfig().host(),
server.getConfiguration().getPort());
}
System.err.println(String.format("Suspending test execution. You can now connect to the running server on: %s", hosts));
while (true) {
try {
Thread.sleep(1000000000000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* Determine if the supplied instance is a mock.
* The evaluation will be delegated to the available mocking provider (Miockito, etc ...) if one is available on the classpath.
*
* If no provider is available this call will throw {@link IllegalStateException}.
*
* @param instance
*
* @return
*/
public static boolean isMock(Object instance) {
if (mockingProvider == null) {
throw new IllegalStateException("Mocking provider is not available. Can not test `isMock` condition!");
}
return mockingProvider.isMock(instance);
}
}