### Question:
StateMachine { public T set(T newState) { T oldState = trySet(newState); checkState(oldState.equals(newState) || !isTerminalState(oldState), "%s cannot transition from %s to %s", name, state, newState); return oldState; } StateMachine(String name, Executor executor, T initialState);  StateMachine(String name, Executor executor, T initialState, Iterable<T> terminalStates); @SuppressWarnings("FieldAccessNotGuarded") T get(); T set(T newState); T trySet(T newState); boolean setIf(T newState, Predicate<T> predicate); boolean compareAndSet(T expectedState, T newState); ListenableFuture<T> getStateChange(T currentState); void addStateChangeListener(StateChangeListener<T> stateChangeListener); @Override String toString();  }### Answer:
@Test public void testSet() throws Exception { StateMachine<State> stateMachine = new StateMachine<>("test", executor, State.BREAKFAST, ImmutableSet.of(State.DINNER)); assertEquals(stateMachine.get(), State.BREAKFAST); assertNoStateChange(stateMachine, () -> assertEquals(stateMachine.set(State.BREAKFAST), State.BREAKFAST)); assertStateChange(stateMachine, () -> assertEquals(stateMachine.set(State.LUNCH), State.BREAKFAST), State.LUNCH); assertStateChange(stateMachine, () -> assertEquals(stateMachine.set(State.BREAKFAST), State.LUNCH), State.BREAKFAST); assertStateChange(stateMachine, () -> assertEquals(stateMachine.set(State.DINNER), State.BREAKFAST), State.DINNER); assertNoStateChange(stateMachine, () -> { try { stateMachine.set(State.LUNCH); fail("expected IllegalStateException"); } catch (IllegalStateException expected) { } }); assertNoStateChange(stateMachine, () -> stateMachine.set(State.DINNER)); }