Skip to content

Instantly share code, notes, and snippets.

@Gregliest
Created April 24, 2017 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gregliest/c8fc7a7796c9f4567f98f3183c0a139c to your computer and use it in GitHub Desktop.
Save Gregliest/c8fc7a7796c9f4567f98f3183c0a139c to your computer and use it in GitHub Desktop.
Example, making JUnit read a little more like Rspec
// Describe: the TripRequest model
@RunWith(HierarchicalContextRunner.class)
public class TripRequestTest {
static Instant now;
static Instant past;
static Instant future;
TimeSlot timeSlotNow;
TimeSlot timeSlotPast;
TimeSlot timeSlotFuture;
@BeforeClass
public static void setupClass() {
now = Instant.now();
past = now.minus(10, ChronoUnit.MINUTES);
future = now.plus(10, ChronoUnit.MINUTES);
}
// Create time slots
@Before
public void setup() {
timeSlotNow = MockTimeSlot.generateTimeSlot(now);
timeSlotPast = MockTimeSlot.generateTimeSlot(past);
timeSlotFuture = MockTimeSlot.generateTimeSlot(future);
}
// Context getFirstTimeSlot()
public class Method_getFirstTimeSlot {
TripRequest request;
@Test
public void givenNoTimeSlots_returnNull() {
request = new TripRequest();
assertThat(request.getFirstTimeSlot(), Matchers.nullValue());
}
@Test
public void givenOneTimeSlot_returnsTimeSlot() {
request = new TripRequest();
request.addTimeSlot(timeSlotNow);
assertEquals(request.getFirstTimeSlot(), timeSlotNow);
}
@Test
public void givenMultipleTimeSlots_returnsFirst() {
request = new TripRequest();
request.setTimeSlots(Lists.newArrayList(timeSlotNow, timeSlotPast, timeSlotFuture));
assertEquals(request.getFirstTimeSlot(), timeSlotPast);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment