* BAEL-5391 - Java Error "Missing return statement" (#5) * BAEL-5391 - Java Error "Missing return statement"
This commit is contained in:
parent
5ee332c606
commit
26a0093154
@ -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<String, Integer> 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<String, Integer> createDictionary() {
|
||||||
|
List<String> words = Arrays.asList("Hello", "World");
|
||||||
|
return words.stream()
|
||||||
|
.collect(Collectors.toMap(s -> s, s -> 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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<String, Integer> dictionary = MissingReturnStatement.createDictionary();
|
||||||
|
assertEquals(2, dictionary.size());
|
||||||
|
dictionary.forEach((s, integer) -> System.out.println(s + " - " + integer));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user