All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.skjolber.mockito.rest.spring.RestServiceRule Maven / Gradle / Ivy

There is a newer version: 1.0.1
Show newest version
package com.github.skjolber.mockito.rest.spring;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

/**
 * Rule for mocking remoting endpoints. 
*
* Intended for use when testing service clients. * */ public class RestServiceRule extends org.junit.rules.ExternalResource { public static RestServiceRule newInstance() { return new RestServiceRule(); } public static RestServiceRule newInstance(List> beans) { return new RestServiceRule(beans); } /** beans added to the sprin context */ private List> contextBeans; public RestServiceRule() { this(Arrays.>asList(DefaultSpringWebMvcConfig.class)); } public RestServiceRule(List> beans) { this.contextBeans = beans; } private List servers = new ArrayList(); /** * Create (and start) service endpoint with mock delegates. * * @param serviceInterfaces a list of desired service mocks * @param address base address, i.e. http://localhost:1234 * @return map of mocks * @throws Exception */ public Map, Object> mock(List> serviceInterfaces, String address) throws Exception { // wrap the evaluator mock in proxy URL url = new URL(address); if (!url.getHost().equals("localhost") && !url.getHost().equals("127.0.0.1")) { throw new IllegalArgumentException("Only local mocking is supported"); } WebAppContext webAppContext = new WebAppContext(); webAppContext.setContextPath(url.getPath()); MockitoSpringConfiguration configuration = new MockitoSpringConfiguration(serviceInterfaces, contextBeans); webAppContext.setConfigurations(new org.eclipse.jetty.webapp.Configuration[] { configuration }); webAppContext.setParentLoaderPriority(true); Server server = new Server(url.getPort()); server.setHandler(webAppContext); server.start(); servers.add(server); return configuration.getAll(); } public T mock(Class serviceInterface, String address) throws Exception { List> mockTargetBeans = new ArrayList<>(); mockTargetBeans.add(serviceInterface); Map, Object> mock = mock(mockTargetBeans, address); return (T) mock.get(serviceInterface); } protected void before() throws Throwable { super.before(); } protected void after() { try { destroy(); } catch (Exception e) { // ignore } } /** * * Destroy endpoints. * * @throws Exception * */ public void destroy() throws Exception { for (Server endpointImpl : servers) { endpointImpl.stop(); } } /** * * Stop endpoints. * * @throws Exception * */ public void stop() throws Exception { for (Server endpointImpl : servers) { endpointImpl.stop(); } } /** * * (Re)start endpoints. * * @throws Exception * */ public void start() throws Exception { for (Server endpointImpl : servers) { endpointImpl.start(); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy