Use String over Object for unit tests
This commit is contained in:
parent
9368968cea
commit
a33bdf8a03
@ -0,0 +1,30 @@
|
|||||||
|
package com.baeldung.returnfirstnonempty;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class ReturnFirstNonEmptyOptionalUnitTest {
|
||||||
|
|
||||||
|
private List<Optional<String>> optionals;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void init() {
|
||||||
|
optionals = Arrays.asList(Optional.<String> empty(), Optional.of("first non null"), Optional.of("second non null"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenListOfOptionals_thenReturnFirstNonEmpty() {
|
||||||
|
Optional<String> object = optionals.stream()
|
||||||
|
.filter(Optional::isPresent)
|
||||||
|
.map(Optional::get)
|
||||||
|
.findFirst();
|
||||||
|
|
||||||
|
assertThat(object).contains("first non null");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,79 @@
|
|||||||
|
package com.baeldung.returnfirstnonnull;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.ObjectUtils;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
import org.mockito.Spy;
|
||||||
|
|
||||||
|
public class ReturnFirstNonNullLazyEvaluateUnitTest {
|
||||||
|
|
||||||
|
@Spy
|
||||||
|
private final LazyEvaluate spy = Mockito.spy(new LazyEvaluate());
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenChainOfMethods_thenLazilyEvaluateMethodsUntilFirstNonNull() {
|
||||||
|
String object = spy.methodA();
|
||||||
|
if (object == null) {
|
||||||
|
object = spy.methodB();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (object == null) {
|
||||||
|
object = spy.methodC();
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals("first non null", object);
|
||||||
|
verify(spy, times(1)).methodA();
|
||||||
|
verify(spy, times(1)).methodB();
|
||||||
|
verify(spy, times(0)).methodC();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenChainOfMethods_whenUsingApacheCommonsLang3_thenReturnFirstNonNull() {
|
||||||
|
String object = ObjectUtils.getFirstNonNull(spy::methodA, spy::methodB, spy::methodC);
|
||||||
|
|
||||||
|
assertEquals("first non null", object);
|
||||||
|
verify(spy, times(1)).methodA();
|
||||||
|
verify(spy, times(1)).methodB();
|
||||||
|
verify(spy, times(0)).methodC();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenChainOfMethods_whenUsingSupplierInterface_thenLazilyEvaluateMethodsUntilFirstNonNull() {
|
||||||
|
Optional<String> object = Stream.<Supplier<String>> of(spy::methodA, spy::methodB, spy::methodC)
|
||||||
|
.map(Supplier::get)
|
||||||
|
.filter(Objects::nonNull)
|
||||||
|
.findFirst();
|
||||||
|
|
||||||
|
assertThat(object).contains("first non null");
|
||||||
|
verify(spy, times(1)).methodA();
|
||||||
|
verify(spy, times(1)).methodB();
|
||||||
|
verify(spy, times(0)).methodC();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenNonNullObjectAndFallbackMethod_whenUsingApacheCommonsLang3_thenReturnFirstNonNull() {
|
||||||
|
String nonNullObject = spy.methodB();
|
||||||
|
String object = ObjectUtils.getIfNull(nonNullObject, spy::methodC);
|
||||||
|
|
||||||
|
assertEquals("first non null", object);
|
||||||
|
verify(spy, times(0)).methodC();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenNullObjectAndFallbackMethod_whenUsingApacheCommonsLang3_thenReturnFirstNonNull() {
|
||||||
|
String nullObject = null;
|
||||||
|
String object = ObjectUtils.getIfNull(nullObject, spy::methodB);
|
||||||
|
|
||||||
|
assertEquals("first non null", object);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,79 @@
|
|||||||
|
package com.baeldung.returnfirstnonnull;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.ObjectUtils;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import com.google.common.base.MoreObjects;
|
||||||
|
import com.google.common.base.Predicates;
|
||||||
|
import com.google.common.collect.Iterables;
|
||||||
|
|
||||||
|
public class ReturnFirstNonNullUnitTest {
|
||||||
|
|
||||||
|
private List<String> objects;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void init() {
|
||||||
|
objects = Arrays.asList(null, "first non null", "second nun null");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenListOfObjects_whenFilterIsLambdaNullCheck_thenReturnFirstNonNull() {
|
||||||
|
Optional<String> object = objects.stream()
|
||||||
|
.filter(o -> o != null)
|
||||||
|
.findFirst();
|
||||||
|
|
||||||
|
assertThat(object).contains("first non null");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenListOfObjects_whenFilterIsMethodRefNullCheck_thenReturnFirstNonNull() {
|
||||||
|
Optional<String> object = objects.stream()
|
||||||
|
.filter(Objects::nonNull)
|
||||||
|
.findFirst();
|
||||||
|
|
||||||
|
assertThat(object).contains("first non null");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenListOfObjects_whenIteratingWithForLoop_thenReturnFirstNonNull() {
|
||||||
|
String object = null;
|
||||||
|
for (int i = 0; i < objects.size(); i++) {
|
||||||
|
if (objects.get(i) != null) {
|
||||||
|
object = objects.get(i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals("first non null", object);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenListOfObjects_whenUsingApacheCommonsLang3_thenReturnFirstNonNull() {
|
||||||
|
String object = ObjectUtils.firstNonNull(objects.toArray(new String[0]));
|
||||||
|
|
||||||
|
assertEquals("first non null", object);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenListOfObjects_whenUsingGoogleGuavaIterables_thenReturnFirstNonNull() {
|
||||||
|
String object = Iterables.find(objects, Predicates.notNull());
|
||||||
|
|
||||||
|
assertEquals("first non null", object);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTwoObjects_whenUsingGoogleGuavaMoreObjects_thenReturnFirstNonNull() {
|
||||||
|
String object = MoreObjects.firstNonNull(null, "first non null");
|
||||||
|
|
||||||
|
assertEquals("first non null", object);
|
||||||
|
}
|
||||||
|
}
|
@ -1,37 +0,0 @@
|
|||||||
package returnfirstnonempty;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
public class ReturnFirstNonEmptyOptionalUnitTest {
|
|
||||||
|
|
||||||
private List<Optional<Object>> optionals;
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void init() {
|
|
||||||
optionals = Arrays.asList(
|
|
||||||
Optional.empty(),
|
|
||||||
Optional.of(new Object()),
|
|
||||||
Optional.empty()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenListOfOptionals_thenReturnFirstNonEmpty() {
|
|
||||||
Object object = optionals
|
|
||||||
.stream()
|
|
||||||
.filter(Optional::isPresent)
|
|
||||||
.map(Optional::get)
|
|
||||||
.findFirst();
|
|
||||||
|
|
||||||
assertTrue(object != null);
|
|
||||||
assertEquals(optionals.get(1), object);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,69 +0,0 @@
|
|||||||
package returnfirstnonull;
|
|
||||||
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.function.Supplier;
|
|
||||||
import java.util.stream.Stream;
|
|
||||||
|
|
||||||
import org.apache.commons.lang3.ObjectUtils;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
public class ReturnFirstNonNullLazyEvaluateUnitTest {
|
|
||||||
|
|
||||||
private static Object METHOD_B_OBJECT = new Object();
|
|
||||||
|
|
||||||
private Object methodA() { return null; }
|
|
||||||
|
|
||||||
private Object methodB() { return METHOD_B_OBJECT; }
|
|
||||||
|
|
||||||
private Object methodC() { return null; }
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenChainOfMethods_thenLazilyEvaluateMethodsUntilFirstNonNull() {
|
|
||||||
Object object = methodA();
|
|
||||||
if(object == null)
|
|
||||||
object = methodB();
|
|
||||||
|
|
||||||
if(object == null)
|
|
||||||
object = methodC();
|
|
||||||
|
|
||||||
assertTrue(object != null);
|
|
||||||
assertEquals(METHOD_B_OBJECT, object);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenChainOfMethods_whenUsingSupplierInterface_thenLazilyEvaluateMethodsUntilFirstNonNull() {
|
|
||||||
Optional<Object> object = Stream
|
|
||||||
.<Supplier<Object>>of(
|
|
||||||
this::methodA,
|
|
||||||
this::methodB,
|
|
||||||
this::methodC)
|
|
||||||
.map(Supplier::get)
|
|
||||||
.filter(Objects::nonNull)
|
|
||||||
.findFirst();
|
|
||||||
|
|
||||||
assertTrue(object.isPresent());
|
|
||||||
assertEquals(METHOD_B_OBJECT, object.get());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenNullableObjectAndFallbackMethod_whenUsingApacheCommonsLang3_thenReturnFirstNonNull() {
|
|
||||||
Object object1 = null;
|
|
||||||
Object object = ObjectUtils.getIfNull(object1, this::methodB);
|
|
||||||
|
|
||||||
assertTrue(object != null);
|
|
||||||
assertEquals(METHOD_B_OBJECT, object);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenChainOfMethods_whenUsingApacheCommonsLang3_thenReturnFirstNonNull() {
|
|
||||||
Object object = ObjectUtils.getFirstNonNull(this::methodA, this::methodB, this::methodC);
|
|
||||||
|
|
||||||
assertTrue(object != null);
|
|
||||||
assertEquals(METHOD_B_OBJECT, object);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,108 +0,0 @@
|
|||||||
package returnfirstnonull;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
import org.apache.commons.lang3.ObjectUtils;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import com.google.common.base.MoreObjects;
|
|
||||||
import com.google.common.base.Predicates;
|
|
||||||
import com.google.common.collect.Iterables;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
public class ReturnFirstNonNullUnitTest {
|
|
||||||
|
|
||||||
private List<Object> objects;
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void init() {
|
|
||||||
objects = Arrays.asList(
|
|
||||||
null,
|
|
||||||
new Object(),
|
|
||||||
new Object()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenListOfObjects_whenIteratingWithForLoop_thenReturnFirstNonNull() {
|
|
||||||
Object object = null;
|
|
||||||
for(int i = 0; i < objects.size(); i++) {
|
|
||||||
if(objects.get(i) != null) {
|
|
||||||
object = objects.get(i);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
assertTrue(object != null);
|
|
||||||
assertEquals(objects.get(1), object);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenListOfObjects_whenIteratingWithEnhancedForLoop_thenReturnFirstNonNull() {
|
|
||||||
Object object = null;
|
|
||||||
for(Object o: objects) {
|
|
||||||
if(o != null) {
|
|
||||||
object = o;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
assertTrue(object != null);
|
|
||||||
assertEquals(objects.get(1), object);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenListOfObjects_whenFilterIsLambdaNullCheck_thenReturnFirstNonNull() {
|
|
||||||
Optional<Object> object = objects
|
|
||||||
.stream()
|
|
||||||
.filter(o -> o != null)
|
|
||||||
.findFirst();
|
|
||||||
|
|
||||||
assertTrue(object.isPresent());
|
|
||||||
assertEquals(objects.get(1), object.get());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenListOfObjects_whenFilterIsMethodRefNullCheck_thenReturnFirstNonNull() {
|
|
||||||
Optional<Object> object = objects
|
|
||||||
.stream()
|
|
||||||
.filter(Objects::nonNull)
|
|
||||||
.findFirst();
|
|
||||||
|
|
||||||
assertTrue(object.isPresent());
|
|
||||||
assertEquals(objects.get(1), object.get());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenListOfObjects_whenUsingApacheCommonsLang3_thenReturnFirstNonNull() {
|
|
||||||
Object object = ObjectUtils.firstNonNull(objects.toArray());
|
|
||||||
|
|
||||||
assertTrue(object != null);
|
|
||||||
assertEquals(objects.get(1), object);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenTwoObjects_whenUsingGoogleGuava_thenReturnFirstNonNull() {
|
|
||||||
Object object1 = null;
|
|
||||||
Object object2 = new Object();
|
|
||||||
Object object = MoreObjects.firstNonNull(object1, object2);
|
|
||||||
|
|
||||||
assertTrue(object != null);
|
|
||||||
assertEquals(object2, object);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenListOfObjects_whenUsingGoogleGuava_thenReturnFirstNonNull() {
|
|
||||||
Object object = Iterables.find(objects, Predicates.notNull());
|
|
||||||
|
|
||||||
assertTrue(object != null);
|
|
||||||
assertEquals(objects.get(1), object);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user