Fix broken PreferencesTest mocks (revealed by strict Mockito stubbing)

Fix the broken stubbing of 'PersistenceManager', which had gone stale as
a result of the conversion of 'Preferences' to asynchronous persistence
in commit 3f4d6e6 (2020/10/12). This caused the assertions in the
'readPersisted' continuation blocks of 3 of the 4 tests not to be
reached. Fix by stubbing the async 'persistenceManager::readPersisted'
method with a callback, instead of stubbing 'getPersisted'.

NOTE: Alternatively, we could add a testing-only 'readPersistedSync'
method to 'Preferences' for consistency, as this is how the other broken
(failing) tests resulting from 3f4d6e6 were fixed (in commit 68583d8).
This commit is contained in:
Steven Barclay 2024-03-09 20:38:34 +08:00
parent c94fa98417
commit 7d2e050474
No known key found for this signature in database
GPG Key ID: 9FED6BF1176D500B

View File

@ -34,11 +34,10 @@ import java.util.ArrayList;
import java.util.Currency;
import java.util.List;
import java.util.Locale;
import java.util.function.Consumer;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ -46,12 +45,13 @@ import org.junit.jupiter.api.extension.ExtendWith;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT) // FIXME: Broken tests: stale persistenceManager stubbing
public class PreferencesTest {
private Preferences preferences;
@Mock
@ -59,7 +59,7 @@ public class PreferencesTest {
@BeforeEach
public void setUp() {
final Locale en_US = new Locale("en", "US");
Locale en_US = new Locale("en", "US");
Locale.setDefault(en_US);
GlobalSettings.setLocale(en_US);
Res.setBaseCurrencyCode("BTC");
@ -72,11 +72,19 @@ public class PreferencesTest {
false, null, null, Config.UNSPECIFIED_PORT);
}
@SuppressWarnings("unchecked")
private void addReadPersistedStub(PreferencesPayload payload) {
doAnswer(invocation -> {
((Consumer<PreferencesPayload>) invocation.getArgument(1)).accept(payload);
return null;
}).when(persistenceManager).readPersisted(anyString(), any(), any());
}
@Test
public void testAddFiatCurrency() {
final FiatCurrency usd = new FiatCurrency("USD");
final FiatCurrency usd2 = new FiatCurrency("USD");
final ObservableList<FiatCurrency> fiatCurrencies = preferences.getFiatCurrenciesAsObservable();
FiatCurrency usd = new FiatCurrency("USD");
FiatCurrency usd2 = new FiatCurrency("USD");
ObservableList<FiatCurrency> fiatCurrencies = preferences.getFiatCurrenciesAsObservable();
preferences.addFiatCurrency(usd);
@ -92,17 +100,18 @@ public class PreferencesTest {
PreferencesPayload payload = mock(PreferencesPayload.class);
List<FiatCurrency> fiatCurrencies = CurrencyUtil.getMainFiatCurrencies();
final FiatCurrency usd = new FiatCurrency("USD");
int numMainFiatCurrencies = fiatCurrencies.size();
FiatCurrency usd = new FiatCurrency("USD");
fiatCurrencies.add(usd);
when(persistenceManager.getPersisted(anyString())).thenReturn(payload);
addReadPersistedStub(payload);
when(payload.getUserLanguage()).thenReturn("en");
when(payload.getUserCountry()).thenReturn(CountryUtil.getDefaultCountry());
when(payload.getPreferredTradeCurrency()).thenReturn(usd);
when(payload.getFiatCurrencies()).thenReturn(fiatCurrencies);
preferences.readPersisted(() -> {
assertEquals(7, preferences.getFiatCurrenciesAsObservable().size());
assertEquals(numMainFiatCurrencies, preferences.getFiatCurrenciesAsObservable().size());
assertTrue(preferences.getFiatCurrenciesAsObservable().contains(usd));
});
}
@ -112,16 +121,20 @@ public class PreferencesTest {
PreferencesPayload payload = mock(PreferencesPayload.class);
List<CryptoCurrency> cryptoCurrencies = CurrencyUtil.getMainCryptoCurrencies();
final CryptoCurrency dash = new CryptoCurrency("DASH", "Dash");
int numMainCryptoCurrencies = cryptoCurrencies.size();
CryptoCurrency dash = new CryptoCurrency("DASH", "Dash");
cryptoCurrencies.add(dash);
when(persistenceManager.getPersisted(anyString())).thenReturn(payload);
addReadPersistedStub(payload);
when(payload.getUserLanguage()).thenReturn("en");
when(payload.getUserCountry()).thenReturn(CountryUtil.getDefaultCountry());
when(payload.getPreferredTradeCurrency()).thenReturn(new FiatCurrency("USD"));
when(payload.getCryptoCurrencies()).thenReturn(cryptoCurrencies);
preferences.readPersisted(() -> assertTrue(preferences.getCryptoCurrenciesAsObservable().contains(dash)));
preferences.readPersisted(() -> {
assertEquals(numMainCryptoCurrencies, preferences.getCryptoCurrenciesAsObservable().size());
assertTrue(preferences.getCryptoCurrenciesAsObservable().contains(dash));
});
}
@Test
@ -129,12 +142,12 @@ public class PreferencesTest {
PreferencesPayload payload = mock(PreferencesPayload.class);
List<FiatCurrency> fiatCurrencies = new ArrayList<>();
final FiatCurrency usd = new FiatCurrency(Currency.getInstance("USD"), new Locale("de", "AT"));
FiatCurrency usd = new FiatCurrency(Currency.getInstance("USD"), new Locale("de", "AT"));
fiatCurrencies.add(usd);
assertEquals("US-Dollar (USD)", usd.getNameAndCode());
when(persistenceManager.getPersisted(anyString())).thenReturn(payload);
addReadPersistedStub(payload);
when(payload.getUserLanguage()).thenReturn("en");
when(payload.getUserCountry()).thenReturn(CountryUtil.getDefaultCountry());
when(payload.getPreferredTradeCurrency()).thenReturn(usd);