fix unit tests (#803)

This commit is contained in:
m2049r 2022-01-02 14:07:23 +01:00 committed by GitHub
parent 22d9173cea
commit ab8fb82c1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 28 additions and 24 deletions

View File

@ -323,6 +323,7 @@ public class NetCipherHelper implements StatusCallback {
} }
private OkHttpClient getClient() { private OkHttpClient getClient() {
if (mockClient != null) return mockClient; // Unit-test mode
final OkHttpClient client = getInstance().client; final OkHttpClient client = getInstance().client;
if ((username != null) && (!username.isEmpty())) { if ((username != null) && (!username.isEmpty())) {
final DigestAuthenticator authenticator = new DigestAuthenticator(new Credentials(username, password)); final DigestAuthenticator authenticator = new DigestAuthenticator(new Credentials(username, password));
@ -349,6 +350,9 @@ public class NetCipherHelper implements StatusCallback {
} }
return builder.build(); return builder.build();
} }
// for unit tests only
static public OkHttpClient mockClient = null;
} }
private static final String PREFS_NAME = "tor"; private static final String PREFS_NAME = "tor";

View File

@ -20,6 +20,7 @@ import com.m2049r.xmrwallet.service.exchange.api.ExchangeApi;
import com.m2049r.xmrwallet.service.exchange.api.ExchangeCallback; import com.m2049r.xmrwallet.service.exchange.api.ExchangeCallback;
import com.m2049r.xmrwallet.service.exchange.api.ExchangeException; import com.m2049r.xmrwallet.service.exchange.api.ExchangeException;
import com.m2049r.xmrwallet.service.exchange.api.ExchangeRate; import com.m2049r.xmrwallet.service.exchange.api.ExchangeRate;
import com.m2049r.xmrwallet.util.NetCipherHelper;
import net.jodah.concurrentunit.Waiter; import net.jodah.concurrentunit.Waiter;
@ -46,7 +47,6 @@ public class ExchangeRateTest {
private ExchangeApi exchangeApi; private ExchangeApi exchangeApi;
private final OkHttpClient okHttpClient = new OkHttpClient();
private Waiter waiter; private Waiter waiter;
@Mock @Mock
@ -60,8 +60,8 @@ public class ExchangeRateTest {
waiter = new Waiter(); waiter = new Waiter();
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
NetCipherHelper.Request.mockClient = new OkHttpClient();
exchangeApi = new ExchangeApiImpl(okHttpClient, mockWebServer.url("/")); exchangeApi = new ExchangeApiImpl( mockWebServer.url("/"));
} }
@After @After

View File

@ -20,6 +20,7 @@ import com.m2049r.xmrwallet.service.exchange.api.ExchangeApi;
import com.m2049r.xmrwallet.service.exchange.api.ExchangeCallback; import com.m2049r.xmrwallet.service.exchange.api.ExchangeCallback;
import com.m2049r.xmrwallet.service.exchange.api.ExchangeException; import com.m2049r.xmrwallet.service.exchange.api.ExchangeException;
import com.m2049r.xmrwallet.service.exchange.api.ExchangeRate; import com.m2049r.xmrwallet.service.exchange.api.ExchangeRate;
import com.m2049r.xmrwallet.util.NetCipherHelper;
import net.jodah.concurrentunit.Waiter; import net.jodah.concurrentunit.Waiter;
@ -46,7 +47,6 @@ public class ExchangeRateTest {
private ExchangeApi exchangeApi; private ExchangeApi exchangeApi;
private final OkHttpClient okHttpClient = new OkHttpClient();
private Waiter waiter; private Waiter waiter;
@Mock @Mock
@ -60,8 +60,8 @@ public class ExchangeRateTest {
waiter = new Waiter(); waiter = new Waiter();
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
NetCipherHelper.Request.mockClient = new OkHttpClient();
exchangeApi = new ExchangeApiImpl(okHttpClient, mockWebServer.url("/")); exchangeApi = new ExchangeApiImpl(mockWebServer.url("/"));
} }
@After @After

View File

@ -16,11 +16,14 @@
package com.m2049r.xmrwallet.service.shift.sideshift; package com.m2049r.xmrwallet.service.shift.sideshift;
import static org.junit.Assert.assertEquals;
import com.m2049r.xmrwallet.service.shift.ShiftCallback; import com.m2049r.xmrwallet.service.shift.ShiftCallback;
import com.m2049r.xmrwallet.service.shift.ShiftException; import com.m2049r.xmrwallet.service.shift.ShiftException;
import com.m2049r.xmrwallet.service.shift.sideshift.api.CreateOrder; import com.m2049r.xmrwallet.service.shift.sideshift.api.CreateOrder;
import com.m2049r.xmrwallet.service.shift.sideshift.api.SideShiftApi; import com.m2049r.xmrwallet.service.shift.sideshift.api.SideShiftApi;
import com.m2049r.xmrwallet.service.shift.sideshift.network.SideShiftApiImpl; import com.m2049r.xmrwallet.service.shift.sideshift.network.SideShiftApiImpl;
import com.m2049r.xmrwallet.util.NetCipherHelper;
import com.m2049r.xmrwallet.util.ServiceHelper; import com.m2049r.xmrwallet.util.ServiceHelper;
import net.jodah.concurrentunit.Waiter; import net.jodah.concurrentunit.Waiter;
@ -38,15 +41,12 @@ import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer; import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest; import okhttp3.mockwebserver.RecordedRequest;
import static org.junit.Assert.assertEquals;
public class SideShiftApiCreateOrderTest { public class SideShiftApiCreateOrderTest {
private MockWebServer mockWebServer; private MockWebServer mockWebServer;
private SideShiftApi xmrToApi; private SideShiftApi xmrToApi;
private final OkHttpClient okHttpClient = new OkHttpClient();
private Waiter waiter; private Waiter waiter;
@Mock @Mock
@ -60,9 +60,9 @@ public class SideShiftApiCreateOrderTest {
waiter = new Waiter(); waiter = new Waiter();
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
NetCipherHelper.Request.mockClient = new OkHttpClient();
xmrToApi = new SideShiftApiImpl(okHttpClient, mockWebServer.url("/")); xmrToApi = new SideShiftApiImpl(mockWebServer.url("/"));
ServiceHelper.ASSET="btc"; // all tests run with BTC ServiceHelper.ASSET = "btc"; // all tests run with BTC
} }
@After @After

View File

@ -21,6 +21,7 @@ import com.m2049r.xmrwallet.service.shift.sideshift.api.QueryOrderParameters;
import com.m2049r.xmrwallet.service.shift.sideshift.api.SideShiftApi; import com.m2049r.xmrwallet.service.shift.sideshift.api.SideShiftApi;
import com.m2049r.xmrwallet.service.shift.ShiftCallback; import com.m2049r.xmrwallet.service.shift.ShiftCallback;
import com.m2049r.xmrwallet.service.shift.sideshift.network.SideShiftApiImpl; import com.m2049r.xmrwallet.service.shift.sideshift.network.SideShiftApiImpl;
import com.m2049r.xmrwallet.util.NetCipherHelper;
import net.jodah.concurrentunit.Waiter; import net.jodah.concurrentunit.Waiter;
@ -45,7 +46,6 @@ public class SideShiftApiOrderParameterTest {
private SideShiftApi xmrToApi; private SideShiftApi xmrToApi;
private final OkHttpClient okHttpClient = new OkHttpClient();
private Waiter waiter; private Waiter waiter;
@Mock @Mock
@ -59,8 +59,8 @@ public class SideShiftApiOrderParameterTest {
waiter = new Waiter(); waiter = new Waiter();
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
NetCipherHelper.Request.mockClient = new OkHttpClient();
xmrToApi = new SideShiftApiImpl(okHttpClient, mockWebServer.url("/")); xmrToApi = new SideShiftApiImpl(mockWebServer.url("/"));
} }
@After @After

View File

@ -16,11 +16,14 @@
package com.m2049r.xmrwallet.service.shift.sideshift; package com.m2049r.xmrwallet.service.shift.sideshift;
import static org.junit.Assert.assertEquals;
import com.m2049r.xmrwallet.service.shift.ShiftCallback;
import com.m2049r.xmrwallet.service.shift.ShiftException; import com.m2049r.xmrwallet.service.shift.ShiftException;
import com.m2049r.xmrwallet.service.shift.sideshift.api.QueryOrderStatus; import com.m2049r.xmrwallet.service.shift.sideshift.api.QueryOrderStatus;
import com.m2049r.xmrwallet.service.shift.sideshift.api.SideShiftApi; import com.m2049r.xmrwallet.service.shift.sideshift.api.SideShiftApi;
import com.m2049r.xmrwallet.service.shift.ShiftCallback;
import com.m2049r.xmrwallet.service.shift.sideshift.network.SideShiftApiImpl; import com.m2049r.xmrwallet.service.shift.sideshift.network.SideShiftApiImpl;
import com.m2049r.xmrwallet.util.NetCipherHelper;
import net.jodah.concurrentunit.Waiter; import net.jodah.concurrentunit.Waiter;
@ -37,14 +40,11 @@ import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer; import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest; import okhttp3.mockwebserver.RecordedRequest;
import static org.junit.Assert.assertEquals;
public class SideShiftApiQueryOrderStatusTest { public class SideShiftApiQueryOrderStatusTest {
private MockWebServer mockWebServer; private MockWebServer mockWebServer;
private SideShiftApi xmrToApi; private SideShiftApi xmrToApi;
private final OkHttpClient okHttpClient = new OkHttpClient();
private Waiter waiter; private Waiter waiter;
@Mock @Mock
@ -58,8 +58,8 @@ public class SideShiftApiQueryOrderStatusTest {
waiter = new Waiter(); waiter = new Waiter();
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
NetCipherHelper.Request.mockClient = new OkHttpClient();
xmrToApi = new SideShiftApiImpl(okHttpClient, mockWebServer.url("/")); xmrToApi = new SideShiftApiImpl(mockWebServer.url("/"));
} }
@After @After

View File

@ -22,6 +22,7 @@ import com.m2049r.xmrwallet.service.shift.ShiftException;
import com.m2049r.xmrwallet.service.shift.sideshift.api.RequestQuote; import com.m2049r.xmrwallet.service.shift.sideshift.api.RequestQuote;
import com.m2049r.xmrwallet.service.shift.sideshift.api.SideShiftApi; import com.m2049r.xmrwallet.service.shift.sideshift.api.SideShiftApi;
import com.m2049r.xmrwallet.service.shift.sideshift.network.SideShiftApiImpl; import com.m2049r.xmrwallet.service.shift.sideshift.network.SideShiftApiImpl;
import com.m2049r.xmrwallet.util.NetCipherHelper;
import com.m2049r.xmrwallet.util.ServiceHelper; import com.m2049r.xmrwallet.util.ServiceHelper;
import net.jodah.concurrentunit.Waiter; import net.jodah.concurrentunit.Waiter;
@ -47,7 +48,6 @@ public class SideShiftApiRequestQuoteTest {
private SideShiftApi xmrToApi; private SideShiftApi xmrToApi;
private final OkHttpClient okHttpClient = new OkHttpClient();
private Waiter waiter; private Waiter waiter;
@Mock @Mock
@ -61,8 +61,8 @@ public class SideShiftApiRequestQuoteTest {
waiter = new Waiter(); waiter = new Waiter();
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
NetCipherHelper.Request.mockClient = new OkHttpClient();
xmrToApi = new SideShiftApiImpl(okHttpClient, mockWebServer.url("/")); xmrToApi = new SideShiftApiImpl(mockWebServer.url("/"));
ServiceHelper.ASSET="btc"; // all tests run with BTC ServiceHelper.ASSET="btc"; // all tests run with BTC
} }