### Question:
CurrencyConverterViewModel extends ViewModel<Void, CurrencyConverterModel> { public void calculate() { String inputString = model.input.get(); try { final float input = Float.parseFloat(inputString); loading.set(true); ioExecutor.execute(new Runnable() { @Override public void run() { try { DecimalFormat decimalFormat = new DecimalFormat("0.00", DecimalFormatSymbols.getInstance(Locale.US)); model.output.set(decimalFormat.format(input * rateLoader.loadRate())); } catch (Exception e) { uiExecutor.execute(new Runnable() { @Override public void run() { messageManager.showMessage(activityHolder, R.string.error_loading_rate); } }); } finally { loading.set(false); } } }); } catch (NumberFormatException e) { messageManager.showMessage(activityHolder, R.string.conversion_error); } } CurrencyConverterViewModel(RateLoader rateLoader, MessageManager messageManager, Executor ioExecutor, Executor uiExecutor); @NonNull @Override CurrencyConverterModel createModel(); void calculate(); final ObservableBoolean loading; }### Answer:
@Test public void testConvertCurrency() { when(rateLoader.loadRate()).thenReturn(2f); CurrencyConverterModel model = viewModel.initAndResume(); model.input.set("123"); viewModel.calculate(); assertThat(model.output.get()).isEqualTo("246.00"); }
@Test public void testConvertCurrencyOnError() { when(rateLoader.loadRate()).thenReturn(2f); CurrencyConverterModel model = viewModel.initAndResume(); model.input.set("abc"); viewModel.calculate(); verify(messageManager).showMessage(any(ActivityHolder.class), eq(R.string.conversion_error)); }