CurrencyConverterViewModel extends ViewModel<Void, CurrencyConverterModel> { public void calculate() { String inputString = model.input.get(); try { float input = Float.parseFloat(inputString); DecimalFormat decimalFormat = new DecimalFormat("0.00", DecimalFormatSymbols.getInstance(Locale.US)); model.output.set(decimalFormat.format(input * rateLoader.loadRate())); } catch (NumberFormatException e) { messageManager.showMessage(activityHolder, R.string.conversion_error); } } CurrencyConverterViewModel(RateLoader rateLoader, MessageManager messageManager); @NonNull @Override CurrencyConverterModel createModel(); void calculate();  }
@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)); }