Skip to content

Commit ddf41d6

Browse files
Alex Vargaspivovarit
authored andcommitted
Test cases for examples in article "A guide to Java 8 forEach" (eugenp#769)
* An example of a test using a WebAppConfiguration annotation * Giving the test the appropriate formatting * A example of a test that uses WebAppConfiguration without the need of a MockMvc object * Adding model class and test cases class to show the use of the forEach loop. For article "A guide to Java 8 forEach" * Examples used in article "Guide to Java 8 forEach"
1 parent 3e7e70e commit ddf41d6

2 files changed

Lines changed: 103 additions & 0 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.java8;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.function.Consumer;
6+
7+
import org.junit.Test;
8+
9+
public class Java8ForEachTest {
10+
11+
@Test
12+
public void compareForEachMethods_thenPrintResults() {
13+
14+
List<String> names = new ArrayList<>();
15+
names.add("Larry");
16+
names.add("Steve");
17+
names.add("James");
18+
names.add("Conan");
19+
names.add("Ellen");
20+
21+
// Java 5 - for-loop
22+
System.out.println("--- Enhanced for-loop ---");
23+
for (String name : names) {
24+
System.out.println(name);
25+
}
26+
27+
// Java 8 - forEach
28+
System.out.println("--- forEach method ---");
29+
names.forEach(name -> System.out.println(name));
30+
31+
// Anonymous inner class that implements Consumer interface
32+
System.out.println("--- Anonymous inner class ---");
33+
names.forEach(new Consumer<String>() {
34+
public void accept(String name) {
35+
System.out.println(name);
36+
}
37+
});
38+
39+
// Create a Consumer implementation to then use in a forEach method
40+
Consumer<String> consumerNames = name -> {
41+
System.out.println(name);
42+
};
43+
System.out.println("--- Implementation of Consumer interface ---");
44+
names.forEach(consumerNames);
45+
46+
// Print elements using a Method Reference
47+
System.out.println("--- Method Reference ---");
48+
names.forEach(System.out::println);
49+
50+
}
51+
52+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.web.controller;
2+
3+
import org.junit.Before;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.test.context.ContextConfiguration;
9+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10+
import org.springframework.test.context.web.WebAppConfiguration;
11+
12+
import com.baeldung.model.Employee;
13+
import com.baeldung.spring.web.config.WebConfig;
14+
15+
@RunWith(SpringJUnit4ClassRunner.class)
16+
@WebAppConfiguration
17+
@ContextConfiguration(classes = WebConfig.class)
18+
public class EmployeeTestWithoutMockMvc {
19+
20+
@Autowired
21+
private EmployeeController employeeController;
22+
23+
@Before
24+
public void setup() {
25+
employeeController.initEmployees();
26+
}
27+
28+
@Test
29+
public void whenInitEmployees_thenVerifyValuesInitiation() {
30+
31+
Employee employee1 = employeeController.employeeMap.get(1L);
32+
Employee employee2 = employeeController.employeeMap.get(2L);
33+
Employee employee3 = employeeController.employeeMap.get(3L);
34+
35+
Assert.assertTrue(employee1.getId() == 1L);
36+
Assert.assertTrue(employee1.getName().equals("John"));
37+
Assert.assertTrue(employee1.getContactNumber().equals("223334411"));
38+
Assert.assertTrue(employee1.getWorkingArea().equals("rh"));
39+
40+
Assert.assertTrue(employee2.getId() == 2L);
41+
Assert.assertTrue(employee2.getName().equals("Peter"));
42+
Assert.assertTrue(employee2.getContactNumber().equals("22001543"));
43+
Assert.assertTrue(employee2.getWorkingArea().equals("informatics"));
44+
45+
Assert.assertTrue(employee3.getId() == 3L);
46+
Assert.assertTrue(employee3.getName().equals("Mike"));
47+
Assert.assertTrue(employee3.getContactNumber().equals("223334411"));
48+
Assert.assertTrue(employee3.getWorkingArea().equals("admin"));
49+
}
50+
51+
}

0 commit comments

Comments
 (0)