Skip to content

Instantly share code, notes, and snippets.

@MoriTanosuke
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MoriTanosuke/4f5d84916fae5d2f7f07 to your computer and use it in GitHub Desktop.
Save MoriTanosuke/4f5d84916fae5d2f7f07 to your computer and use it in GitHub Desktop.
A simple junit test with language elements of the Java 8 Stream API.
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import static org.junit.Assert.assertEquals;
public class SortApplesTest {
private List<Apple> apples = new ArrayList<Apple>();
@Before
public void setUp() {
// make sure we always have an empty list for future tests
apples.clear();
// add some colorful apples
apples.add(new Apple("green"));
apples.add(new Apple("green"));
apples.add(new Apple("red"));
apples.add(new Apple("yellow"));
}
@Test
public void sortGreenApples() {
// get only green apples from the list, using a Lambda!
// also, using Collectors.toList() to get all the results into a List at the end
final List<Apple> greenApples = apples.stream().filter(a -> a.color.equals("green")).collect(Collectors.toList());
assertEquals(greenApples.size(), 2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment