How do you test code that uses Duration?

Testing code that utilizes the Duration class in Java can be approached using various methods, including unit tests. The Duration class provides a way to model time-based amounts of time, which can be crucial in many applications. Below is an example of how to test Duration-related code effectively.

// Example of testing Duration in Java import static org.junit.Assert.*; import org.junit.Test; import java.time.Duration; public class DurationTest { @Test public void testDurationAddition() { Duration duration1 = Duration.ofHours(2); Duration duration2 = Duration.ofMinutes(30); Duration result = duration1.plus(duration2); assertEquals(Duration.ofHours(2).plusMinutes(30), result); } @Test public void testDurationSubtraction() { Duration duration1 = Duration.ofDays(1); Duration duration2 = Duration.ofHours(12); Duration result = duration1.minus(duration2); assertEquals(Duration.ofHours(12), result); } }

Java Duration Unit Testing JUnit Time Management Java Time API