Java tutorial
/* Copyright 2018 Nationale-Nederlanden Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package nl.nn.adapterframework.http; import static org.junit.Assert.*; import java.io.IOException; import java.io.InputStream; import java.net.URL; import org.apache.http.HttpHost; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.protocol.HttpContext; import org.mockito.Mockito; public abstract class BaseHttpSender<S extends HttpSenderBase> extends Mockito { protected S sender; public abstract S createSender(); public S getSender() throws IOException { sender = createSender(); if (sender == null) fail("sender not initialized"); CloseableHttpClient httpClient = mock(CloseableHttpClient.class); //Mock all requests when(httpClient.execute(any(HttpHost.class), any(HttpRequestBase.class), any(HttpContext.class))) .thenAnswer(new HttpResponseMock()); when(sender.getHttpClient()).thenReturn(httpClient); //Some default settings, url will be mocked. sender.setUrl("http://127.0.0.1/"); sender.setIgnoreRedirects(true); sender.setVerifyHostname(false); sender.setAllowSelfSignedCertificates(true); return sender; } private final String BASEDIR = "/nl/nn/adapterframework/http/response/"; protected InputStream getFile(String file) throws IOException { URL url = this.getClass().getResource(BASEDIR + file); if (url == null) { throw new IOException("file not found"); } return url.openStream(); } }