Test cases for examples in article "A guide to Java 8 forEach" (#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"
This commit is contained in:
Alex Vargas 2016-11-01 00:33:13 -07:00 committed by Grzegorz Piwowarek
parent 3e7e70eb33
commit ddf41d61a0
2 changed files with 103 additions and 0 deletions

View File

@ -0,0 +1,52 @@
package com.baeldung.java8;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import org.junit.Test;
public class Java8ForEachTest {
@Test
public void compareForEachMethods_thenPrintResults() {
List<String> names = new ArrayList<>();
names.add("Larry");
names.add("Steve");
names.add("James");
names.add("Conan");
names.add("Ellen");
// Java 5 - for-loop
System.out.println("--- Enhanced for-loop ---");
for (String name : names) {
System.out.println(name);
}
// Java 8 - forEach
System.out.println("--- forEach method ---");
names.forEach(name -> System.out.println(name));
// Anonymous inner class that implements Consumer interface
System.out.println("--- Anonymous inner class ---");
names.forEach(new Consumer<String>() {
public void accept(String name) {
System.out.println(name);
}
});
// Create a Consumer implementation to then use in a forEach method
Consumer<String> consumerNames = name -> {
System.out.println(name);
};
System.out.println("--- Implementation of Consumer interface ---");
names.forEach(consumerNames);
// Print elements using a Method Reference
System.out.println("--- Method Reference ---");
names.forEach(System.out::println);
}
}

View File

@ -0,0 +1,51 @@
package com.baeldung.web.controller;
import org.junit.Before;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import com.baeldung.model.Employee;
import com.baeldung.spring.web.config.WebConfig;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = WebConfig.class)
public class EmployeeTestWithoutMockMvc {
@Autowired
private EmployeeController employeeController;
@Before
public void setup() {
employeeController.initEmployees();
}
@Test
public void whenInitEmployees_thenVerifyValuesInitiation() {
Employee employee1 = employeeController.employeeMap.get(1L);
Employee employee2 = employeeController.employeeMap.get(2L);
Employee employee3 = employeeController.employeeMap.get(3L);
Assert.assertTrue(employee1.getId() == 1L);
Assert.assertTrue(employee1.getName().equals("John"));
Assert.assertTrue(employee1.getContactNumber().equals("223334411"));
Assert.assertTrue(employee1.getWorkingArea().equals("rh"));
Assert.assertTrue(employee2.getId() == 2L);
Assert.assertTrue(employee2.getName().equals("Peter"));
Assert.assertTrue(employee2.getContactNumber().equals("22001543"));
Assert.assertTrue(employee2.getWorkingArea().equals("informatics"));
Assert.assertTrue(employee3.getId() == 3L);
Assert.assertTrue(employee3.getName().equals("Mike"));
Assert.assertTrue(employee3.getContactNumber().equals("223334411"));
Assert.assertTrue(employee3.getWorkingArea().equals("admin"));
}
}