### Question:
QuantityTextWatcher implements TextWatcher { @Override public void afterTextChanged(Editable editable) { String value = editable.toString(); Log.e("KEYBOARD", value); if (!value.isEmpty()) { int quantity = getIntFromString(value); Log.i("Entered", String.format("%d", quantity)); Log.i("Entered", String.format("%s", value)); int stock_level = stockService.getStockLevelFor(commodityViewModel.getCommodity()); commodityViewModel.setQuantityEntered(quantity); if (quantity > stock_level) { editTextQuantity.setError(String.format("The quantity entered is greater than Stock available (%d)", stock_level)); } else { editTextQuantity.setError(null); } } } QuantityTextWatcher(EditText editTextQuantity, BaseCommodityViewModel commodityViewModel); @Override void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3); @Override void onTextChanged(CharSequence charSequence, int i, int i2, int i3); @Override void afterTextChanged(Editable editable);  }### Answer:
@Test public void shouldShowErrorIfQuantityIsGreaterThanStockAvailable() throws Exception { when(mockStockService.getStockLevelFor(Matchers.<Commodity>anyObject())).thenReturn(2); watcher.afterTextChanged(new Factory().newEditable("12")); verify(editText).setError(anyString()); }
@Test public void shouldShowErrorWithRemainingStockIfQuantityIsGreaterThanStockAvailable() throws Exception { when(mockStockService.getStockLevelFor(Matchers.<Commodity>anyObject())).thenReturn(2); watcher.afterTextChanged(new Factory().newEditable("12")); assertThat(editText.getError().toString(), containsString("(2)")); }
@Test public void shouldNotShowErrorIfQuantityIsLessThanStockAvailable() throws Exception { when(mockStockService.getStockLevelFor(Matchers.<Commodity>anyObject())).thenReturn(77); watcher.afterTextChanged(new Factory().newEditable("4")); verify(editText).setError(null); }
@Test public void shouldNotShowErrorIfQuantityIsEqualToStockAvailable() throws Exception { when(mockStockService.getStockLevelFor(Matchers.<Commodity>anyObject())).thenReturn(4); watcher.afterTextChanged(new Factory().newEditable("4")); verify(editText).setError(null); }