File tree Expand file tree Collapse file tree 2 files changed +79
-0
lines changed
core-java-modules/core-java-exceptions-4/src
main/java/com/baeldung/exception/missingreturnstatement
test/java/com/baeldung/exception/missingreturnstatement Expand file tree Collapse file tree 2 files changed +79
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .baeldung .exception .missingreturnstatement ;
2+
3+ import java .util .Arrays ;
4+ import java .util .List ;
5+ import java .util .Map ;
6+ import java .util .stream .Collectors ;
7+
8+ public class MissingReturnStatement {
9+ public static void main (String [] args ) {
10+ int a = -12 ;
11+ int result = pow (a );
12+ System .out .println (result );
13+ Map <String , Integer > dictionary = createDictionary ();
14+ dictionary .forEach ((s , integer ) -> System .out .println (s + " " + integer ));
15+ }
16+
17+ public static int pow (int number ) {
18+ int pow = number * number ;
19+ return pow ;
20+ }
21+
22+ public static String checkNumber (int number ) {
23+ if (number == 0 ) {
24+ return "It's equals to zero" ;
25+ }
26+
27+ for (int i = 0 ; i < number ; i ++) {
28+ if (i > 100 ) {
29+ return "It's a big number" ;
30+ }
31+ }
32+ return "It's a negative number" ;
33+ }
34+
35+ public static Map <String , Integer > createDictionary () {
36+ List <String > words = Arrays .asList ("Hello" , "World" );
37+ return words .stream ()
38+ .collect (Collectors .toMap (s -> s , s -> 1 ));
39+ }
40+
41+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .exception .missingreturnstatement ;
2+
3+ import static org .junit .Assert .assertEquals ;
4+
5+ import java .util .Map ;
6+
7+ import org .junit .Test ;
8+
9+ public class MissingReturnStatementUnitTest {
10+
11+ @ Test
12+ public void givenANumber_thenReturnItsPow () {
13+ int number = 10 ;
14+ int pow = MissingReturnStatement .pow (number );
15+ assertEquals (100 , pow );
16+ }
17+
18+ @ Test
19+ public void givenABigNumber_thenReturnItsType () {
20+ int number = 200 ;
21+ String type = MissingReturnStatement .checkNumber (number );
22+ assertEquals ("It's a big number" , type );
23+ }
24+
25+ @ Test
26+ public void givenANegativeNumber_thenReturnItsType () {
27+ int number = -10 ;
28+ String type = MissingReturnStatement .checkNumber (number );
29+ assertEquals ("It's a negative number" , type );
30+ }
31+
32+ @ Test
33+ public void getStringDictionary_thenPrintValues () {
34+ Map <String , Integer > dictionary = MissingReturnStatement .createDictionary ();
35+ assertEquals (2 , dictionary .size ());
36+ dictionary .forEach ((s , integer ) -> System .out .println (s + " - " + integer ));
37+ }
38+ }
You can’t perform that action at this time.
0 commit comments