BAEL2489 Avoid check for null statement in Java
This commit is contained in:
parent
55f7da4948
commit
66146ea761
@ -7,11 +7,12 @@ import java.util.stream.Stream;
|
|||||||
|
|
||||||
public class EmptyCollections {
|
public class EmptyCollections {
|
||||||
|
|
||||||
public List<String> names(){
|
public List<String> names() {
|
||||||
if (userExist())
|
if (userExist()) {
|
||||||
return Stream.of(readName()).collect(Collectors.toList());
|
return Stream.of(readName()).collect(Collectors.toList());
|
||||||
else
|
} else {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean userExist() {
|
private boolean userExist() {
|
||||||
|
@ -16,10 +16,11 @@ public class FindBugsAnnotations {
|
|||||||
@Nonnull
|
@Nonnull
|
||||||
public Object process() throws Exception {
|
public Object process() throws Exception {
|
||||||
Object result = doSomething();
|
Object result = doSomething();
|
||||||
if (result == null)
|
if (result == null) {
|
||||||
throw new Exception("Processing fail. Got a null response");
|
throw new Exception("Processing fail. Got a null response");
|
||||||
else
|
} else {
|
||||||
return result;
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Object doSomething() {
|
private Object doSomething() {
|
||||||
|
@ -3,8 +3,9 @@ package com.baeldung.nulls;
|
|||||||
public class Preconditions {
|
public class Preconditions {
|
||||||
|
|
||||||
public void goodAccept(String one, String two, String three) {
|
public void goodAccept(String one, String two, String three) {
|
||||||
if (one == null || two == null || three == null)
|
if (one == null || two == null || three == null) {
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
|
}
|
||||||
|
|
||||||
process(one);
|
process(one);
|
||||||
process(two);
|
process(two);
|
||||||
@ -12,20 +13,23 @@ public class Preconditions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void badAccept(String one, String two, String three) {
|
public void badAccept(String one, String two, String three) {
|
||||||
if (one == null)
|
if (one == null) {
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
else
|
} else {
|
||||||
process(one);
|
process(one);
|
||||||
|
}
|
||||||
|
|
||||||
if (two == null)
|
if (two == null) {
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
else
|
} else {
|
||||||
process(two);
|
process(two);
|
||||||
|
}
|
||||||
|
|
||||||
if (three == null)
|
if (three == null) {
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
else
|
} else {
|
||||||
process(three);
|
process(three);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,10 +11,11 @@ public class PrimitivesAndWrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Integer goodSum(Integer a, Integer b) {
|
public static Integer goodSum(Integer a, Integer b) {
|
||||||
if (a != null && b != null)
|
if (a != null && b != null) {
|
||||||
return a + b;
|
return a + b;
|
||||||
else
|
} else {
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,13 +4,8 @@ import java.util.Objects;
|
|||||||
|
|
||||||
public class UsingObjects {
|
public class UsingObjects {
|
||||||
|
|
||||||
public void accept(Object param) throws Exception {
|
public void accept(Object param) {
|
||||||
try {
|
Objects.requireNonNull(param);
|
||||||
Objects.requireNonNull(param);
|
// doSomething()
|
||||||
} catch (NullPointerException e) {
|
|
||||||
throw new Exception();
|
|
||||||
}
|
|
||||||
|
|
||||||
//doSomething()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ import org.junit.jupiter.api.Test;
|
|||||||
class PrimitivesAndWrapperUnitTest {
|
class PrimitivesAndWrapperUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenWrappers_whenBothArgsNonNull_thenReturnResult() {
|
public void givenBothArgsNonNull_whenCallingWrapperSum_thenReturnSum() {
|
||||||
|
|
||||||
Integer sum = PrimitivesAndWrapper.wrapperSum(0, 0);
|
Integer sum = PrimitivesAndWrapper.wrapperSum(0, 0);
|
||||||
|
|
||||||
@ -16,17 +16,17 @@ class PrimitivesAndWrapperUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test()
|
@Test()
|
||||||
public void givenWrappers_whenOneArgIsNull_thenThrowNullPointerException() {
|
public void givenOneArgIsNull_whenCallingWrapperSum_thenThrowNullPointerException() {
|
||||||
assertThrows(NullPointerException.class, () -> PrimitivesAndWrapper.wrapperSum(null, 2));
|
assertThrows(NullPointerException.class, () -> PrimitivesAndWrapper.wrapperSum(null, 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test()
|
@Test()
|
||||||
public void givenWrappers_whenBothArgsAreNull_thenThrowNullPointerException() {
|
public void givenBothArgsNull_whenCallingWrapperSum_thenThrowNullPointerException() {
|
||||||
assertThrows(NullPointerException.class, () -> PrimitivesAndWrapper.wrapperSum(null, null));
|
assertThrows(NullPointerException.class, () -> PrimitivesAndWrapper.wrapperSum(null, null));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test()
|
@Test()
|
||||||
public void givenWrappersWithNullCheck_whenAnyArgIsNull_thenThrowIllegalArgumentException() {
|
public void givenOneArgNull_whenCallingGoodSum_thenThrowIllegalArgumentException() {
|
||||||
assertThrows(IllegalArgumentException.class, () -> PrimitivesAndWrapper.goodSum(null, 2));
|
assertThrows(IllegalArgumentException.class, () -> PrimitivesAndWrapper.goodSum(null, 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,18 +20,23 @@ class UsingOptionalUnitTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenArgIsFalse_thenReturnEmptyResponse() {
|
public void whenArgIsFalse_thenReturnEmptyResponse() {
|
||||||
|
|
||||||
Optional<Object> result = classUnderTest.process(false);
|
Optional<Object> result = classUnderTest.process(false);
|
||||||
|
|
||||||
assertFalse(result.isPresent());
|
assertFalse(result.isPresent());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenArgIsTrue_thenReturnValidResponse() {
|
public void whenArgIsTrue_thenReturnValidResponse() {
|
||||||
|
|
||||||
Optional<Object> result = classUnderTest.process(true);
|
Optional<Object> result = classUnderTest.process(true);
|
||||||
|
|
||||||
assertTrue(result.isPresent());
|
assertTrue(result.isPresent());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenArgIsFalse_thenChainResponseAndThrowException() {
|
public void whenArgIsFalse_thenChainResponseAndThrowException() {
|
||||||
|
|
||||||
assertThrows(Exception.class, () -> classUnderTest.process(false).orElseThrow(() -> new Exception()));
|
assertThrows(Exception.class, () -> classUnderTest.process(false).orElseThrow(() -> new Exception()));
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -16,28 +16,28 @@ class UsingStringUtilsUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenAccept_whenArgIsNull_throwIllegalArgumentException() {
|
public void givenArgIsNull_whenCallingAccept_throwIllegalArgumentException() {
|
||||||
Assertions.assertThrows(IllegalArgumentException.class, () -> classUnderTest.accept(null));
|
assertThrows(IllegalArgumentException.class, () -> classUnderTest.accept(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenAccept_whenArgIsEmpty_throwIllegalArgumentException() {
|
public void givenArgIsEmpty_whenCallingAccept_throwIllegalArgumentException() {
|
||||||
Assertions.assertThrows(IllegalArgumentException.class, () -> classUnderTest.accept(""));
|
assertThrows(IllegalArgumentException.class, () -> classUnderTest.accept(""));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenAcceptOnlyNonBlank_whenArgIsNull_throwIllegalArgumentException() {
|
public void givenArgIsNull_whenCallingAcceptOnlyNonBlank_throwIllegalArgumentException() {
|
||||||
Assertions.assertThrows(IllegalArgumentException.class, () -> classUnderTest.acceptOnlyNonBlank(null));
|
assertThrows(IllegalArgumentException.class, () -> classUnderTest.acceptOnlyNonBlank(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenAcceptOnlyNonBlank_whenArgIsEmpty_throwIllegalArgumentException() {
|
public void givenArgIsEmpty_whenCallingAcceptOnlyNonBlank_throwIllegalArgumentException() {
|
||||||
Assertions.assertThrows(IllegalArgumentException.class, () -> classUnderTest.acceptOnlyNonBlank(""));
|
assertThrows(IllegalArgumentException.class, () -> classUnderTest.acceptOnlyNonBlank(""));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenAcceptOnlyNonBlank_whenArgIsBlank_throwIllegalArgumentException() {
|
public void givenArgIsBlank_whenCallingAcceptOnlyNonBlank_throwIllegalArgumentException() {
|
||||||
Assertions.assertThrows(IllegalArgumentException.class, () -> classUnderTest.acceptOnlyNonBlank(" "));
|
assertThrows(IllegalArgumentException.class, () -> classUnderTest.acceptOnlyNonBlank(" "));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user