BAEL-1039: Introduction to Derive4J (#5845)
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
8e18c03c40
commit
22d22c7c4e
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.derive4j.adt;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class EitherUnitTest {
|
||||
@Test
|
||||
public void testEitherIsCreatedFromRight() {
|
||||
Either<Exception, String> either = Eithers.right("Okay");
|
||||
Optional<Exception> leftOptional = Eithers.getLeft(either);
|
||||
Optional<String> rightOptional = Eithers.getRight(either);
|
||||
Assertions.assertThat(leftOptional).isEmpty();
|
||||
Assertions.assertThat(rightOptional).hasValue("Okay");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEitherIsMatchedWithRight() {
|
||||
Either<Exception, String> either = Eithers.right("Okay");
|
||||
Function<Exception, String> leftFunction = Mockito.mock(Function.class);
|
||||
Function<String, String> rightFunction = Mockito.mock(Function.class);
|
||||
either.match(leftFunction, rightFunction);
|
||||
Mockito.verify(rightFunction, Mockito.times(1)).apply("Okay");
|
||||
Mockito.verify(leftFunction, Mockito.times(0)).apply(Mockito.any(Exception.class));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.derive4j.lazy;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class LazyRequestUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenLazyContstructedRequest_whenRequestIsReferenced_thenRequestIsLazilyContructed() {
|
||||
LazyRequestSupplier mockSupplier = Mockito.spy(new LazyRequestSupplier());
|
||||
|
||||
LazyRequest request = LazyRequestImpl.lazy(() -> mockSupplier.get());
|
||||
Mockito.verify(mockSupplier, Mockito.times(0)).get();
|
||||
Assert.assertEquals(LazyRequestImpl.getPath(request), "http://test.com/get");
|
||||
Mockito.verify(mockSupplier, Mockito.times(1)).get();
|
||||
|
||||
}
|
||||
|
||||
class LazyRequestSupplier implements Supplier<LazyRequest> {
|
||||
@Override
|
||||
public LazyRequest get() {
|
||||
return LazyRequestImpl.GET("http://test.com/get");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.derive4j.pattern;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class HTTPRequestUnitTest {
|
||||
public static HTTPServer server;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() {
|
||||
server = new HTTPServer();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenHttpGETRequest_whenRequestReachesServer_thenProperResponseIsReturned() {
|
||||
HTTPRequest postRequest = HTTPRequests.POST("http://test.com/post", "Resource");
|
||||
HTTPResponse response = server.acceptRequest(postRequest);
|
||||
Assert.assertEquals(201, response.getStatusCode());
|
||||
Assert.assertEquals(HTTPServer.POST_RESPONSE_BODY, response.getResponseBody());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user