Skip to content

Instantly share code, notes, and snippets.

@trikitrok
Created September 14, 2024 11:24
Show Gist options
  • Save trikitrok/e0103d0f52e94e22373e9381d7da7a90 to your computer and use it in GitHub Desktop.
Save trikitrok/e0103d0f52e94e22373e9381d7da7a90 to your computer and use it in GitHub Desktop.
course-duration tests in Java
package unit_tests;
import course_duration.Clock;
import course_duration.Configuration;
import course_duration.Course;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.stream.Stream;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class CourseTest {
private static final Instant START_TIME = Instant.parse("2018-11-30T18:35:24.00Z");
public static final String COURSE_NAME = "Macrame";
private Course course;
private Clock clock;
private Configuration configuration;
@BeforeEach
public void setUp() {
clock = mock(Clock.class);
configuration = mock(Configuration.class);
course = new Course(COURSE_NAME, configuration, clock);
}
@Test
public void detects_a_short_course() {
when(clock.now()).thenReturn(
START_TIME,
START_TIME.plus(9, ChronoUnit.MINUTES)
);
course.start();
course.end();
assertThat(course.isShort(), is(true));
}
@Test
public void detects_a_long_course() {
when(clock.now()).thenReturn(
START_TIME,
START_TIME.plus(10, ChronoUnit.MINUTES)
);
course.start();
course.end();
assertThat(course.isLong(), is(true));
}
@ParameterizedTest
@MethodSource("colleges")
public void knows_course_title(String configurationValue, String collegeName) {
when(configuration.getValue("COLLEGE")).thenReturn(configurationValue);
var title = course.getTitle();
assertThat(title, is(COURSE_NAME + " course in " + collegeName + " college"));
}
private static Stream<Arguments> colleges() {
return Stream.of(
Arguments.of("ACME university", "ACME university"),
Arguments.of(null, "not found")
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment