### Question:
BytesAndUOMConverter { long toMegabytes(final String unitAndUOM) { Preconditions.checkArgument(!Strings.isNullOrEmpty(unitAndUOM), "unitAndUOM should not be empty"); Preconditions.checkArgument(unitAndUOM.toUpperCase().endsWith("K") || unitAndUOM.toUpperCase().endsWith("M") || unitAndUOM.toUpperCase().endsWith("G") || Character.isDigit(unitAndUOM.charAt(unitAndUOM.length() -1)), "unitAndUOM should end with a proper UoM or with a digit"); try { final long bytesCount; if (unitAndUOM.toUpperCase().endsWith("K")) { bytesCount = getUnit(unitAndUOM) * ONE_KB; } else if (unitAndUOM.toUpperCase().endsWith("M")) { bytesCount = getUnit(unitAndUOM) * ONE_MB; } else if (unitAndUOM.toUpperCase().endsWith("G")) { bytesCount = getUnit(unitAndUOM) * ONE_GB; } else { bytesCount = Long.parseLong(unitAndUOM); } Preconditions.checkArgument(bytesCount > 0L, "unit should be positive"); return bytesCount / ONE_MB; } catch (final NumberFormatException e) { LOG.error("Cannot parse bytes and UoM {0}, cannot convert to megabytes."); throw e; } }    }### Answer:
@Test public void whenEmptyInputIsGivenException() { expectedException.expect(IllegalArgumentException.class); new BytesAndUOMConverter().toMegabytes(null); }
@Test public void whenUOMIsIncorrectException() { expectedException.expect(IllegalArgumentException.class); new BytesAndUOMConverter().toMegabytes("123T"); }
@Test public void whenNoUnitIsGivenException() { expectedException.expect(NumberFormatException.class); new BytesAndUOMConverter().toMegabytes("K"); }
@Test public void whenIncorrectUnitIsGivenException() { expectedException.expect(NumberFormatException.class); new BytesAndUOMConverter().toMegabytes("1aa1K"); }
@Test public void whenNotPositiveUnitIsGivenException() { expectedException.expect(IllegalArgumentException.class); new BytesAndUOMConverter().toMegabytes("0K"); }
@Test public void whenUnitIsGivenAndNoUOMIsPresentConvertedCorrectly() { assertEquals("bytes count should be converted correctly", 1L, new BytesAndUOMConverter().toMegabytes( Integer.toString(new Double(Math.pow(2, 20)).intValue()))); }
@Test public void whenMegabytesAreGivenSameReturned() { assertEquals("megabytes count should remain unchanged", 1L, new BytesAndUOMConverter().toMegabytes("1M")); }
@Test public void whenKilobytesAreGivenConvertedCorrectly() { assertEquals("kilobytes count should be converted correctly", 1L, new BytesAndUOMConverter().toMegabytes("1024K")); assertEquals("kilobytes count should be converted correctly", 0L, new BytesAndUOMConverter().toMegabytes("1023K")); assertEquals("kilobytes count should be converted correctly", 10L, new BytesAndUOMConverter().toMegabytes("10240K")); }
@Test public void whenGigabytesAreGivenConvertedCorrectly() { assertEquals("gigabytes count should be converted correctly", 1024L, new BytesAndUOMConverter().toMegabytes("1G")); }