|
| 1 | +package com.baeldung.derive4j.adt; |
| 2 | + |
| 3 | +import org.assertj.core.api.Assertions; |
| 4 | +import org.junit.Test; |
| 5 | +import org.junit.runner.RunWith; |
| 6 | +import org.mockito.Mockito; |
| 7 | +import org.mockito.junit.MockitoJUnitRunner; |
| 8 | + |
| 9 | +import java.util.Optional; |
| 10 | +import java.util.function.Function; |
| 11 | +@RunWith(MockitoJUnitRunner.class) |
| 12 | +public class EitherUnitTest { |
| 13 | + @Test |
| 14 | + public void testEitherIsCreatedFromRight() { |
| 15 | + Either<Exception, String> either = Eithers.right("Okay"); |
| 16 | + Optional<Exception> leftOptional = Eithers.getLeft(either); |
| 17 | + Optional<String> rightOptional = Eithers.getRight(either); |
| 18 | + Assertions.assertThat(leftOptional).isEmpty(); |
| 19 | + Assertions.assertThat(rightOptional).hasValue("Okay"); |
| 20 | + |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + public void testEitherIsMatchedWithRight() { |
| 25 | + Either<Exception, String> either = Eithers.right("Okay"); |
| 26 | + Function<Exception, String> leftFunction = Mockito.mock(Function.class); |
| 27 | + Function<String, String> rightFunction = Mockito.mock(Function.class); |
| 28 | + either.match(leftFunction, rightFunction); |
| 29 | + Mockito.verify(rightFunction, Mockito.times(1)).apply("Okay"); |
| 30 | + Mockito.verify(leftFunction, Mockito.times(0)).apply(Mockito.any(Exception.class)); |
| 31 | + } |
| 32 | + |
| 33 | +} |
0 commit comments