* BAEL-1997 state design pattern in Java * BAEL-1997 different example code * BAEL-1997 add additional method to the states * BAEL-1997 clean up in ReceivedState
34 lines
880 B
Java
34 lines
880 B
Java
package com.baeldung.state;
|
|
|
|
import com.baeldung.state.Package;
|
|
|
|
import static org.junit.Assert.assertThat;
|
|
import static org.hamcrest.CoreMatchers.instanceOf;
|
|
|
|
import org.junit.Test;
|
|
|
|
public class StatePatternUnitTest {
|
|
|
|
@Test
|
|
public void givenNewPackage_whenPackageReceived_thenStateReceived() {
|
|
Package pkg = new Package();
|
|
|
|
assertThat(pkg.getState(), instanceOf(OrderedState.class));
|
|
pkg.nextState();
|
|
|
|
assertThat(pkg.getState(), instanceOf(DeliveredState.class));
|
|
pkg.nextState();
|
|
|
|
assertThat(pkg.getState(), instanceOf(ReceivedState.class));
|
|
}
|
|
|
|
@Test
|
|
public void givenDeliveredPackage_whenPrevState_thenStateOrdered() {
|
|
Package pkg = new Package();
|
|
pkg.setState(new DeliveredState());
|
|
pkg.previousState();
|
|
|
|
assertThat(pkg.getState(), instanceOf(OrderedState.class));
|
|
}
|
|
}
|