Skip to content

Commit 469e36f

Browse files
earth001KevinGilmore
authored andcommitted
BAEL-2444 Extra examples (eugenp#6087)
1 parent b81af29 commit 469e36f

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.time;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.powermock.core.classloader.annotations.PrepareForTest;
6+
import org.powermock.modules.junit4.PowerMockRunner;
7+
8+
import java.time.Clock;
9+
import java.time.Instant;
10+
import java.time.ZoneId;
11+
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
import static org.mockito.Mockito.when;
14+
import static org.powermock.api.mockito.PowerMockito.mockStatic;
15+
16+
@RunWith(PowerMockRunner.class)
17+
@PrepareForTest({ Instant.class })
18+
public class InstantUnitTest {
19+
20+
@Test
21+
public void givenInstantMock_whenNow_thenGetFixedInstant() {
22+
String instantExpected = "2014-12-22T10:15:30Z";
23+
Clock clock = Clock.fixed(Instant.parse(instantExpected), ZoneId.of("UTC"));
24+
Instant instant = Instant.now(clock);
25+
mockStatic(Instant.class);
26+
when(Instant.now()).thenReturn(instant);
27+
28+
Instant now = Instant.now();
29+
30+
assertThat(now.toString()).isEqualTo(instantExpected);
31+
}
32+
33+
@Test
34+
public void givenFixedClock_whenNow_thenGetFixedInstant() {
35+
String instantExpected = "2014-12-22T10:15:30Z";
36+
Clock clock = Clock.fixed(Instant.parse(instantExpected), ZoneId.of("UTC"));
37+
38+
Instant instant = Instant.now(clock);
39+
40+
assertThat(instant.toString()).isEqualTo(instantExpected);
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.time;
2+
3+
import mockit.Expectations;
4+
import mockit.Mock;
5+
import mockit.MockUp;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.time.Clock;
9+
import java.time.Instant;
10+
import java.time.ZoneId;
11+
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
14+
public class InstantWithJMockUnitTest {
15+
16+
@Test
17+
public void givenInstantWithJMock_whenNow_thenGetFixedInstant() {
18+
String instantExpected = "2014-12-21T10:15:30Z";
19+
Clock clock = Clock.fixed(Instant.parse(instantExpected), ZoneId.of("UTC"));
20+
new MockUp<Instant>() {
21+
@Mock
22+
public Instant now() {
23+
return Instant.now(clock);
24+
}
25+
};
26+
27+
Instant now = Instant.now();
28+
29+
assertThat(now.toString()).isEqualTo(instantExpected);
30+
}
31+
32+
@Test
33+
public void givenInstantWithExpectations_whenNow_thenGetFixedInstant() {
34+
Clock clock = Clock.fixed(Instant.parse("2014-12-23T10:15:30.00Z"), ZoneId.of("UTC"));
35+
Instant instantExpected = Instant.now(clock);
36+
new Expectations(Instant.class) {
37+
{
38+
Instant.now();
39+
result = instantExpected;
40+
}
41+
};
42+
43+
Instant now = Instant.now();
44+
45+
assertThat(now).isEqualTo(instantExpected);
46+
}
47+
}

0 commit comments

Comments
 (0)