BAEL2489 Avoid check for null statement in Java

This commit is contained in:
Chirag Dewan 2019-03-19 20:40:26 +05:30
parent 55f7da4948
commit 66146ea761
8 changed files with 43 additions and 36 deletions

View File

@ -7,11 +7,12 @@ import java.util.stream.Stream;
public class EmptyCollections {
public List<String> names(){
if (userExist())
public List<String> names() {
if (userExist()) {
return Stream.of(readName()).collect(Collectors.toList());
else
} else {
return Collections.emptyList();
}
}
private boolean userExist() {

View File

@ -16,10 +16,11 @@ public class FindBugsAnnotations {
@Nonnull
public Object process() throws Exception {
Object result = doSomething();
if (result == null)
if (result == null) {
throw new Exception("Processing fail. Got a null response");
else
} else {
return result;
}
}
private Object doSomething() {

View File

@ -3,8 +3,9 @@ package com.baeldung.nulls;
public class Preconditions {
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();
}
process(one);
process(two);
@ -12,20 +13,23 @@ public class Preconditions {
}
public void badAccept(String one, String two, String three) {
if (one == null)
if (one == null) {
throw new IllegalArgumentException();
else
} else {
process(one);
}
if (two == null)
if (two == null) {
throw new IllegalArgumentException();
else
} else {
process(two);
}
if (three == null)
if (three == null) {
throw new IllegalArgumentException();
else
} else {
process(three);
}
}

View File

@ -11,10 +11,11 @@ public class PrimitivesAndWrapper {
}
public static Integer goodSum(Integer a, Integer b) {
if (a != null && b != null)
if (a != null && b != null) {
return a + b;
else
} else {
throw new IllegalArgumentException();
}
}
}

View File

@ -4,13 +4,8 @@ import java.util.Objects;
public class UsingObjects {
public void accept(Object param) throws Exception {
try {
Objects.requireNonNull(param);
} catch (NullPointerException e) {
throw new Exception();
}
//doSomething()
public void accept(Object param) {
Objects.requireNonNull(param);
// doSomething()
}
}

View File

@ -8,7 +8,7 @@ import org.junit.jupiter.api.Test;
class PrimitivesAndWrapperUnitTest {
@Test
public void givenWrappers_whenBothArgsNonNull_thenReturnResult() {
public void givenBothArgsNonNull_whenCallingWrapperSum_thenReturnSum() {
Integer sum = PrimitivesAndWrapper.wrapperSum(0, 0);
@ -16,17 +16,17 @@ class PrimitivesAndWrapperUnitTest {
}
@Test()
public void givenWrappers_whenOneArgIsNull_thenThrowNullPointerException() {
public void givenOneArgIsNull_whenCallingWrapperSum_thenThrowNullPointerException() {
assertThrows(NullPointerException.class, () -> PrimitivesAndWrapper.wrapperSum(null, 2));
}
@Test()
public void givenWrappers_whenBothArgsAreNull_thenThrowNullPointerException() {
public void givenBothArgsNull_whenCallingWrapperSum_thenThrowNullPointerException() {
assertThrows(NullPointerException.class, () -> PrimitivesAndWrapper.wrapperSum(null, null));
}
@Test()
public void givenWrappersWithNullCheck_whenAnyArgIsNull_thenThrowIllegalArgumentException() {
public void givenOneArgNull_whenCallingGoodSum_thenThrowIllegalArgumentException() {
assertThrows(IllegalArgumentException.class, () -> PrimitivesAndWrapper.goodSum(null, 2));
}

View File

@ -20,18 +20,23 @@ class UsingOptionalUnitTest {
@Test
public void whenArgIsFalse_thenReturnEmptyResponse() {
Optional<Object> result = classUnderTest.process(false);
assertFalse(result.isPresent());
}
@Test
public void whenArgIsTrue_thenReturnValidResponse() {
Optional<Object> result = classUnderTest.process(true);
assertTrue(result.isPresent());
}
@Test
public void whenArgIsFalse_thenChainResponseAndThrowException() {
assertThrows(Exception.class, () -> classUnderTest.process(false).orElseThrow(() -> new Exception()));
}
}

View File

@ -16,28 +16,28 @@ class UsingStringUtilsUnitTest {
}
@Test
public void givenAccept_whenArgIsNull_throwIllegalArgumentException() {
Assertions.assertThrows(IllegalArgumentException.class, () -> classUnderTest.accept(null));
public void givenArgIsNull_whenCallingAccept_throwIllegalArgumentException() {
assertThrows(IllegalArgumentException.class, () -> classUnderTest.accept(null));
}
@Test
public void givenAccept_whenArgIsEmpty_throwIllegalArgumentException() {
Assertions.assertThrows(IllegalArgumentException.class, () -> classUnderTest.accept(""));
public void givenArgIsEmpty_whenCallingAccept_throwIllegalArgumentException() {
assertThrows(IllegalArgumentException.class, () -> classUnderTest.accept(""));
}
@Test
public void givenAcceptOnlyNonBlank_whenArgIsNull_throwIllegalArgumentException() {
Assertions.assertThrows(IllegalArgumentException.class, () -> classUnderTest.acceptOnlyNonBlank(null));
public void givenArgIsNull_whenCallingAcceptOnlyNonBlank_throwIllegalArgumentException() {
assertThrows(IllegalArgumentException.class, () -> classUnderTest.acceptOnlyNonBlank(null));
}
@Test
public void givenAcceptOnlyNonBlank_whenArgIsEmpty_throwIllegalArgumentException() {
Assertions.assertThrows(IllegalArgumentException.class, () -> classUnderTest.acceptOnlyNonBlank(""));
public void givenArgIsEmpty_whenCallingAcceptOnlyNonBlank_throwIllegalArgumentException() {
assertThrows(IllegalArgumentException.class, () -> classUnderTest.acceptOnlyNonBlank(""));
}
@Test
public void givenAcceptOnlyNonBlank_whenArgIsBlank_throwIllegalArgumentException() {
Assertions.assertThrows(IllegalArgumentException.class, () -> classUnderTest.acceptOnlyNonBlank(" "));
public void givenArgIsBlank_whenCallingAcceptOnlyNonBlank_throwIllegalArgumentException() {
assertThrows(IllegalArgumentException.class, () -> classUnderTest.acceptOnlyNonBlank(" "));
}
}