Merge remote-tracking branch 'origin/BAEL-16646' into BAEL-16646
# Conflicts: # core-java-modules/core-java-datetime/README.md # core-java-modules/core-java-datetime/pom.xml
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.time;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.powermock.api.mockito.PowerMockito.mockStatic;
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({ Instant.class })
|
||||
public class InstantUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenInstantMock_whenNow_thenGetFixedInstant() {
|
||||
String instantExpected = "2014-12-22T10:15:30Z";
|
||||
Clock clock = Clock.fixed(Instant.parse(instantExpected), ZoneId.of("UTC"));
|
||||
Instant instant = Instant.now(clock);
|
||||
mockStatic(Instant.class);
|
||||
when(Instant.now()).thenReturn(instant);
|
||||
|
||||
Instant now = Instant.now();
|
||||
|
||||
assertThat(now.toString()).isEqualTo(instantExpected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFixedClock_whenNow_thenGetFixedInstant() {
|
||||
String instantExpected = "2014-12-22T10:15:30Z";
|
||||
Clock clock = Clock.fixed(Instant.parse(instantExpected), ZoneId.of("UTC"));
|
||||
|
||||
Instant instant = Instant.now(clock);
|
||||
|
||||
assertThat(instant.toString()).isEqualTo(instantExpected);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.time;
|
||||
|
||||
import mockit.Expectations;
|
||||
import mockit.Mock;
|
||||
import mockit.MockUp;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class InstantWithJMockUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenInstantWithJMock_whenNow_thenGetFixedInstant() {
|
||||
String instantExpected = "2014-12-21T10:15:30Z";
|
||||
Clock clock = Clock.fixed(Instant.parse(instantExpected), ZoneId.of("UTC"));
|
||||
new MockUp<Instant>() {
|
||||
@Mock
|
||||
public Instant now() {
|
||||
return Instant.now(clock);
|
||||
}
|
||||
};
|
||||
|
||||
Instant now = Instant.now();
|
||||
|
||||
assertThat(now.toString()).isEqualTo(instantExpected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInstantWithExpectations_whenNow_thenGetFixedInstant() {
|
||||
Clock clock = Clock.fixed(Instant.parse("2014-12-23T10:15:30.00Z"), ZoneId.of("UTC"));
|
||||
Instant instantExpected = Instant.now(clock);
|
||||
new Expectations(Instant.class) {
|
||||
{
|
||||
Instant.now();
|
||||
result = instantExpected;
|
||||
}
|
||||
};
|
||||
|
||||
Instant now = Instant.now();
|
||||
|
||||
assertThat(now).isEqualTo(instantExpected);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.time;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.powermock.api.mockito.PowerMockito.mockStatic;
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({ LocalDateTime.class })
|
||||
public class LocalDateTimeUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenLocalDateTimeMock_whenNow_thenGetFixedLocalDateTime() {
|
||||
Clock clock = Clock.fixed(Instant.parse("2014-12-22T10:15:30.00Z"), ZoneId.of("UTC"));
|
||||
LocalDateTime dateTime = LocalDateTime.now(clock);
|
||||
mockStatic(LocalDateTime.class);
|
||||
when(LocalDateTime.now()).thenReturn(dateTime);
|
||||
String dateTimeExpected = "2014-12-22T10:15:30";
|
||||
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
|
||||
assertThat(now).isEqualTo(dateTimeExpected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFixedClock_whenNow_thenGetFixedLocalDateTime() {
|
||||
Clock clock = Clock.fixed(Instant.parse("2014-12-22T10:15:30.00Z"), ZoneId.of("UTC"));
|
||||
String dateTimeExpected = "2014-12-22T10:15:30";
|
||||
|
||||
LocalDateTime dateTime = LocalDateTime.now(clock);
|
||||
|
||||
assertThat(dateTime).isEqualTo(dateTimeExpected);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.time;
|
||||
|
||||
import mockit.Expectations;
|
||||
import mockit.Mock;
|
||||
import mockit.MockUp;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class LocalDateTimeWithJMockUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenLocalDateTimeWithJMock_whenNow_thenGetFixedLocalDateTime() {
|
||||
Clock clock = Clock.fixed(Instant.parse("2014-12-21T10:15:30.00Z"), ZoneId.of("UTC"));
|
||||
new MockUp<LocalDateTime>() {
|
||||
@Mock
|
||||
public LocalDateTime now() {
|
||||
return LocalDateTime.now(clock);
|
||||
}
|
||||
};
|
||||
String dateTimeExpected = "2014-12-21T10:15:30";
|
||||
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
|
||||
assertThat(now).isEqualTo(dateTimeExpected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocalDateTimeWithExpectations_whenNow_thenGetFixedLocalDateTime() {
|
||||
Clock clock = Clock.fixed(Instant.parse("2014-12-23T10:15:30.00Z"), ZoneId.of("UTC"));
|
||||
LocalDateTime dateTimeExpected = LocalDateTime.now(clock);
|
||||
new Expectations(LocalDateTime.class) {
|
||||
{
|
||||
LocalDateTime.now();
|
||||
result = dateTimeExpected;
|
||||
}
|
||||
};
|
||||
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
|
||||
assertThat(now).isEqualTo(dateTimeExpected);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user