From 26a0093154832f9162df2baabd884d43aabb8884 Mon Sep 17 00:00:00 2001 From: alemoles Date: Mon, 21 Feb 2022 22:13:41 -0500 Subject: [PATCH] BAEL-5391 - Java Error "Missing return statement" (#5) (#11845) * BAEL-5391 - Java Error "Missing return statement" (#5) * BAEL-5391 - Java Error "Missing return statement" --- .../MissingReturnStatement.java | 41 +++++++++++++++++++ .../MissingReturnStatementUnitTest.java | 38 +++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 core-java-modules/core-java-exceptions-4/src/main/java/com/baeldung/exception/missingreturnstatement/MissingReturnStatement.java create mode 100644 core-java-modules/core-java-exceptions-4/src/test/java/com/baeldung/exception/missingreturnstatement/MissingReturnStatementUnitTest.java diff --git a/core-java-modules/core-java-exceptions-4/src/main/java/com/baeldung/exception/missingreturnstatement/MissingReturnStatement.java b/core-java-modules/core-java-exceptions-4/src/main/java/com/baeldung/exception/missingreturnstatement/MissingReturnStatement.java new file mode 100644 index 0000000000..d76ae76a0e --- /dev/null +++ b/core-java-modules/core-java-exceptions-4/src/main/java/com/baeldung/exception/missingreturnstatement/MissingReturnStatement.java @@ -0,0 +1,41 @@ +package com.baeldung.exception.missingreturnstatement; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class MissingReturnStatement { + public static void main(String[] args) { + int a = -12; + int result = pow(a); + System.out.println(result); + Map dictionary = createDictionary(); + dictionary.forEach((s, integer) -> System.out.println(s + " " + integer)); + } + + public static int pow(int number) { + int pow = number * number; + return pow; + } + + public static String checkNumber(int number) { + if (number == 0) { + return "It's equals to zero"; + } + + for (int i = 0; i < number; i++) { + if (i > 100) { + return "It's a big number"; + } + } + return "It's a negative number"; + } + + public static Map createDictionary() { + List words = Arrays.asList("Hello", "World"); + return words.stream() + .collect(Collectors.toMap(s -> s, s -> 1)); + } + +} diff --git a/core-java-modules/core-java-exceptions-4/src/test/java/com/baeldung/exception/missingreturnstatement/MissingReturnStatementUnitTest.java b/core-java-modules/core-java-exceptions-4/src/test/java/com/baeldung/exception/missingreturnstatement/MissingReturnStatementUnitTest.java new file mode 100644 index 0000000000..97a050a3f0 --- /dev/null +++ b/core-java-modules/core-java-exceptions-4/src/test/java/com/baeldung/exception/missingreturnstatement/MissingReturnStatementUnitTest.java @@ -0,0 +1,38 @@ +package com.baeldung.exception.missingreturnstatement; + +import static org.junit.Assert.assertEquals; + +import java.util.Map; + +import org.junit.Test; + +public class MissingReturnStatementUnitTest { + + @Test + public void givenANumber_thenReturnItsPow() { + int number = 10; + int pow = MissingReturnStatement.pow(number); + assertEquals(100, pow); + } + + @Test + public void givenABigNumber_thenReturnItsType() { + int number = 200; + String type = MissingReturnStatement.checkNumber(number); + assertEquals("It's a big number", type); + } + + @Test + public void givenANegativeNumber_thenReturnItsType() { + int number = -10; + String type = MissingReturnStatement.checkNumber(number); + assertEquals("It's a negative number", type); + } + + @Test + public void getStringDictionary_thenPrintValues() { + Map dictionary = MissingReturnStatement.createDictionary(); + assertEquals(2, dictionary.size()); + dictionary.forEach((s, integer) -> System.out.println(s + " - " + integer)); + } +} \ No newline at end of file