### Question:
HexString implements Serializable { public static byte[] toBytes(String hexString) throws HexFormatException { Assert.notNull(hexString, "HexString must not be null"); if (hexString.length() % 2 != 0) { throw new HexFormatException("Hexadecimal strings must contain an even number of characters", hexString); } byte[] out = new byte[hexString.length() / 2]; Arrays.fill(out, (byte) 0x00); for (int i = 0; i < hexString.length(); i++) { int digit = Character.digit(hexString.charAt(i), 16); if (digit == -1) { throw new HexFormatException("Illegal character '" + hexString.charAt(i) + "' in hexadecimal string at position " + i, hexString, i); } out[i / 2] = (byte) ((out[i / 2] | digit << (i % 2 == 0 ? 4 : 0)) & 0xFF); } return out; } HexString(String hexString); private  HexString(byte[] bytes); byte[] getBytes(); @Override String toString(); @Override boolean equals(Object obj); @Override int hashCode(); static HexString valueOf(byte[] bytes); static String toString(byte[] bytes); static String toString(byte[] bytes, int offset, int length); static char[] toChars(byte[] bytes); static char[] toChars(byte[] bytes, int offset, int length); static byte[] toBytes(String hexString);  }### Answer:
@Test public void shouldConvertToBytes() throws Exception { byte[] b = HexString.toBytes(STRING); assertThat(b, is(equalTo(BYTES))); }
@Test public void shouldNotAllowNullStringOnToBytes() throws Exception { this.thrown.expect(IllegalArgumentException.class); this.thrown.expectMessage("HexString must not be null"); HexString.toBytes(null); }
@Test public void shouldOnlySuportEvenString() throws Exception { this.thrown.expect(HexFormatException.class); this.thrown.expectMessage("Hexadecimal strings must contain an even number of characters"); HexString.toBytes("AAA"); }
@Test public void shouldThrowOnNonHexChar() throws Exception { this.thrown.expect(HexFormatException.class); this.thrown.expectMessage("Illegal character 'G' in hexadecimal string at position 3"); HexString.toBytes("AAEGBB"); }