### Question:
JavaBeanAdapter implements TokenAdapter<T> { @Override public Token toToken(T o) { Reject.ifTrue(o == null, "Object must not be null"); String tokenId = (String) idField.read(o); if (tokenId == null && idField.generated) { tokenId = idGenerator.generateTokenId(null); idField.write(tokenId, o); } else if (tokenId == null) { throw new IllegalArgumentException("ID field is not generated and is null"); } Token token = new Token(tokenId, tokenType); for (FieldDetails details : fields) { Object value = details.read(o); if (value != null) { token.setAttribute(details.tokenField, value); } } return token; } @Inject  JavaBeanAdapter(@Assisted Class<T> beanClass, TokenIdGenerator idGenerator); @Override Token toToken(T o); @Override T fromToken(Token token); TokenFilter toTokenQuery(QueryFilter<String> filter);  }### Answer:
@Test public void testGenerateId() throws Exception { DummyBean b = new DummyBean(); Token t = adapter.toToken(b); assertThat(b.getId()).matches("[0-9a-f-]{36}"); assertThat(b.getId()).isEqualTo(t.getTokenId()); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNonGeneratedNullId() throws Exception { JavaBeanAdapter<GuicedBean> guicedBeanAdapter = new JavaBeanAdapter<GuicedBean>(GuicedBean.class, null); GuicedBean b = new GuicedBean("123"); guicedBeanAdapter.toToken(b); }
@Test(expectedExceptions = IllegalArgumentException.class) public void nullFailsToToken() throws Exception { adapter.toToken(null); }