11package com .baeldung .java8 ;
22
3- import com .baeldung .Foo ;
4- import com .baeldung .FooExtended ;
5- import com .baeldung .UseFoo ;
6- import org .junit .Before ;
7- import org .junit .Test ;
3+ import static org .junit .Assert .assertEquals ;
4+ import static org .junit .Assert .assertNotEquals ;
85
96import java .util .function .Function ;
107
11- import static org .junit .Assert .assertEquals ;
12- import static org .junit .Assert .assertNotEquals ;
8+ import org .junit .Before ;
9+ import org .junit .Test ;
10+
11+ import com .baeldung .Foo ;
12+ import com .baeldung .FooExtended ;
13+ import com .baeldung .UseFoo ;
1314
1415public class Java8FunctionalInteracesLambdasTest {
1516
@@ -22,39 +23,35 @@ public void init() {
2223
2324 @ Test
2425 public void functionalInterfaceInstantiation_whenReturnDefiniteString_thenCorrect () {
25-
26- Foo foo = parameter -> parameter + "from lambda" ;
27- String result = useFoo .add ("Message " , foo );
26+ final Foo foo = parameter -> parameter + "from lambda" ;
27+ final String result = useFoo .add ("Message " , foo );
2828
2929 assertEquals ("Message from lambda" , result );
3030 }
3131
3232 @ Test
3333 public void standardFIParameter_whenReturnDefiniteString_thenCorrect () {
34-
35- Function <String , String > fn = parameter -> parameter + "from lambda" ;
36- String result = useFoo .addWithStandardFI ("Message " , fn );
34+ final Function <String , String > fn = parameter -> parameter + "from lambda" ;
35+ final String result = useFoo .addWithStandardFI ("Message " , fn );
3736
3837 assertEquals ("Message from lambda" , result );
3938 }
4039
4140 @ Test
4241 public void defaultMethodFromExtendedInterface_whenReturnDefiniteString_thenCorrect () {
43-
44- FooExtended fooExtended = string -> string ;
45- String result = fooExtended .defaultMethod ();
42+ final FooExtended fooExtended = string -> string ;
43+ final String result = fooExtended .defaultMethod ();
4644
4745 assertEquals ("String from Bar" , result );
4846 }
4947
5048 @ Test
5149 public void lambdaAndInnerClassInstantiation_whenReturnSameString_thenCorrect () {
50+ final Foo foo = parameter -> parameter + "from Foo" ;
5251
53- Foo foo = parameter -> parameter + "from Foo" ;
54-
55- Foo fooByIC = new Foo () {
52+ final Foo fooByIC = new Foo () {
5653 @ Override
57- public String method (String string ) {
54+ public String method (final String string ) {
5855 return string + "from Foo" ;
5956 }
6057 };
@@ -64,35 +61,32 @@ public String method(String string) {
6461
6562 @ Test
6663 public void accessVariablesFromDifferentScopes_whenReturnPredefinedString_thenCorrect () {
67-
68- assertEquals ("Results: resultIC = Inner class value, resultLambda = Enclosing scope value" ,
69- useFoo .scopeExperiment ());
64+ assertEquals ("Results: resultIC = Inner class value, resultLambda = Enclosing scope value" , useFoo .scopeExperiment ());
7065 }
7166
7267 @ Test
7368 public void shorteningLambdas_whenReturnEqualsResults_thenCorrect () {
69+ final Foo foo = parameter -> buildString (parameter );
7470
75- Foo foo = parameter -> buildString (parameter );
76-
77- Foo fooHuge = parameter -> { String result = "Something " + parameter ;
78- //many lines of code
71+ final Foo fooHuge = parameter -> {
72+ final String result = "Something " + parameter ;
73+ // many lines of code
7974 return result ;
8075 };
8176
8277 assertEquals (foo .method ("Something" ), fooHuge .method ("Something" ));
8378 }
8479
85- private String buildString (String parameter ) {
86- String result = "Something " + parameter ;
87- //many lines of code
80+ private String buildString (final String parameter ) {
81+ final String result = "Something " + parameter ;
82+ // many lines of code
8883 return result ;
8984 }
9085
9186 @ Test
9287 public void mutatingOfEffectivelyFinalVariable_whenNotEquals_thenCorrect () {
93-
94- int [] total = new int [1 ];
95- Runnable r = () -> total [0 ]++;
88+ final int [] total = new int [1 ];
89+ final Runnable r = () -> total [0 ]++;
9690 r .run ();
9791
9892 assertNotEquals (0 , total [0 ]);
0 commit comments